繁体   English   中英

Java 康威人生游戏

[英]Java Conway Game of life

我的节目是康威人生游戏,规则是:

  1. 任何少于两个活邻居的活细胞都会死亡,好像是由于人口不足造成的。
  2. 任何有两三个活邻居的活细胞都可以活到下一代。
  3. 任何有超过三个活邻居的活细胞都会死亡,就像人口过剩一样。
  4. 任何只有三个活邻居的死细胞都会变成活细胞,就像通过繁殖一样。

我能够在一行中检查活着的邻居我的问题是检查列中的活着的邻居,并在检查列时查看下一代是生是死,下面的代码演示了我得到的结果,其中 1 代表一个活细胞0代表死细胞。

public static int[][] nextGeneration(int inputGrid[][]) {
                int height = 10, width = 10;
                int[][] future = new int[height][width];     
    
    for (int x = 1; x < height - 1; x++) {
                    for (int y = 1; y < width - 1; y++) {
                        int aliveNeighbours = 0;
                        for (int i = -1; i <= 1; i++)
                            for (int j = -1; j <= 1; j++)
                                aliveNeighbours += inputGrid[x + i][y + j];
        
                        aliveNeighbours -= inputGrid[x][y];
                        if ((inputGrid[x][y] == 1) && (aliveNeighbours < 2))
                            future[x][y] = 0;
            
                            else if (((inputGrid[x][y] == 1) && ((aliveNeighbours ==2) || (aliveNeighbours ==3))))
                                future[x][y] = 1;
            
                            else if ((inputGrid[x][y] == 1) && (aliveNeighbours > 3))
                                future[x][y] = 0;
            
                            else if ((inputGrid[x][y] == 0) && (aliveNeighbours == 3))
                                future[x][y] = 1;
            
                            else
                                future[x][y] = inputGrid[x][y];
                        }
                    }
                    return future;
                }
            public static void main(String[] args) {
                    int[][] game = 
                           {{0, 0, 1, 1, 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, 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, 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, 0, 1, 0}
                    };
            
                    System.out.println(nextGeneration(game));
                }

我的结果是:

    {{0, 0, 0, 0, 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, 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},
     {0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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, 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},
     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
     {0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
     {0, 0, 0, 0, 0, 0, 0, 0, 0, 1}

我的结果中的单元格 10*10 应该是活的而不是死的

您的循环的主要缺陷之一是 Java arrays (像大多数)具有从零开始的索引。 但是,您的循环从 x 和 y 设置为 1 开始,因此您根本不处理第一行。 此外,您的循环条件在您处理最后一行之前停止。 此代码更正了该错误:

public static int[][] nextGeneration(int inputGrid[][]) {
    int height = 10, width = 10;
    int[][] future = new int[height][width];

    // iterate over each row (start with 0 because an array index is 0 based)
    for (int x = 0; x < height; x++) {
        // iterate over each column (start with 0 as well)
        for (int y = 0; y < width; y++) {
            int aliveNeighbours = 0;
            int rowAbove = Math.max(x -1, 0); // the row above is x-1 but never less than 0 because that row doesn't exist
            int rowBelow = Math.min(x + 1, height - 1); // the row below is never greater than the last row in the array (height - 1)
            int colLeft = Math.max(y -1, 0); // go to the left one column, unless we are at the edge, then don't go past 0
            int colRight = Math.min(y + 1, width - 1); // ... continuing the same logic as above
            for (int rowToCheck = rowAbove; rowToCheck <= rowBelow; rowToCheck++)
                for (int colToCheck = colLeft; colToCheck <= colRight; colToCheck++)
                    aliveNeighbours += inputGrid[rowToCheck][colToCheck];

            // remove the cell being evaluated from the neighbors count
            aliveNeighbours -= inputGrid[x][y];

            // simplified logic to remove unnecessary conditions
            // any cell with three neighbors is alive (past value doesn't matter)
            if (aliveNeighbours == 3)
                future[x][y] = 1;
            // any cell with fewer than two live neighbors is dead (past value doesn't matter)
            else if (aliveNeighbours < 2)
                future[x][y] = 0;
            // any cell with more than three neighbors is dead (past value doesn't matter)
            else if (aliveNeighbours >= 4)
                future[x][y] = 0;
            // any cell with two neighbors remains in its present state (regardless of what the past value was)
            else if (aliveNeighbours == 2)
                future[x][y] = inputGrid[x][y];
            else
                throw new RuntimeException("Unhandled neighbor condition");
        }
    }
    return future;
}

public static void main(String[] args) {
    int[][] game =
            {{0, 0, 1, 1, 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, 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, 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, 0, 1, 0}
            };

    var nextGen = nextGeneration(game);

    for(var row : nextGen)
    {
        for(var cell : row)
        {
            System.out.print(cell);
        }
        System.out.println();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM