<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 康威人生游戲計劃 > 原文: [https://www.geeksforgeeks.org/program-for-conways-game-of-life/](https://www.geeksforgeeks.org/program-for-conways-game-of-life/) 最初,有一個網格,其中包含一些可能存在或死亡的單元格。 我們根據以下規則生成下一代單元的任務: 1. 任何具有少于兩個活鄰居的活細胞都會死亡,好像是由于人口不足所致。 2. 任何有兩個或三個活鄰居的活細胞都可以存活到下一代。 3. 任何具有三個以上活鄰居的活細胞都會死亡,就好像人口過多一樣。 4. 具有正好三個活鄰居的任何死細胞都將變成一個活細胞,就像通過繁殖一樣。 示例: “ *”代表存活的細胞,而“。”代表死亡的細胞。 ``` Input : .......... ...**..... ....*..... .......... .......... Output: .......... ...**..... ...**..... .......... .......... .......... Input : .......... ...**..... ....*..... .......... .......... ...**..... ..**...... .....*.... ....*..... .......... Output: .......... ...**..... ...**..... .......... .......... ..***..... ..**...... ...**..... .......... .......... ``` 這是“生命游戲”的簡單 Java 實現。 初始化為 0 的網格代表死細胞,而 1 的網格則代表活細胞。 generate()函數循環遍歷每個單元并計算其鄰居。 基于該值,實現上述規則。 以下實現忽略了邊緣單元,因為它應該在無限平面上播放。 ## Java ```java // A simple Java program to implement Game of Life public class GameOfLife { ????public static void main(String[] args) ????{ ????????int M = 10, N = 10; ????????// Intiliazing the grid. ????????int[][] grid = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, ????????????{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, ????????????{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, ????????????{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, ????????????{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, ????????????{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, ????????????{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, ????????????{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, ????????????{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, ????????????{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ????????}; ????????// Displaying the grid ????????System.out.println("Original Generation"); ????????for (int i = 0; i < M; i++) ????????{ ????????????for (int j = 0; j < N; j++) ????????????{ ????????????????if (grid[i][j] == 0) ????????????????????System.out.print("."); ????????????????else ????????????????????System.out.print("*"); ????????????} ????????????System.out.println(); ????????} ????????System.out.println(); ????????nextGeneration(grid, M, N); ????} ????// Function to print next generation ????static void nextGeneration(int grid[][], int M, int N) ????{ ????????int[][] future = new int[M][N]; ????????// Loop through every cell ????????for (int l = 1; l < M - 1; l++) ????????{ ????????????for (int m = 1; m < N - 1; m++) ????????????{ ????????????????// finding no Of Neighbours that are alive ????????????????int aliveNeighbours = 0; ????????????????for (int i = -1; i <= 1; i++) ????????????????????for (int j = -1; j <= 1; j++) ????????????????????????aliveNeighbours += grid[l + i][m + j]; ????????????????// The cell needs to be subtracted from ????????????????// its neighbours as it was counted before ????????????????aliveNeighbours -= grid[l][m]; ????????????????// Implementing the Rules of Life ????????????????// Cell is lonely and dies ????????????????if ((grid[l][m] == 1) && (aliveNeighbours < 2)) ????????????????????future[l][m] = 0; ????????????????// Cell dies due to over population ????????????????else if ((grid[l][m] == 1) && (aliveNeighbours > 3)) ????????????????????future[l][m] = 0; ????????????????// A new cell is born ????????????????else if ((grid[l][m] == 0) && (aliveNeighbours == 3)) ????????????????????future[l][m] = 1; ????????????????// Remains the same ????????????????else ????????????????????future[l][m] = grid[l][m]; ????????????} ????????} ????????System.out.println("Next Generation"); ????????for (int i = 0; i < M; i++) ????????{ ????????????for (int j = 0; j < N; j++) ????????????{ ????????????????if (future[i][j] == 0) ????????????????????System.out.print("."); ????????????????else ????????????????????System.out.print("*"); ????????????} ????????????System.out.println(); ????????} ????} } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看