首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生命游戏

生命游戏
EN

Stack Overflow用户
提问于 2022-01-23 23:00:41
回答 1查看 886关注 0票数 0

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

任何活细胞,如果活的邻居少于两个,就会像underpopulation.

  • Any活细胞一样死亡,两个或三个活的邻居活到下一个活的generation.

  • Any活细胞,有三个以上的活邻居死亡,就像被overpopulation.

  • Any死掉的细胞和三个活的邻居一样,就像通过繁殖一样。

我能够在一行中检查活邻居--我的问题是检查列中的活邻居,查看下一代在检查列时是活的还是死的,下面的代码演示了我得到了什么,其中1表示活的单元格,0表示死的单元格。

代码语言:javascript
复制
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));
                }

我的结果是:

代码语言:javascript
复制
    {{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}
    };

但预期结果应是:

代码语言:javascript
复制
    {{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在我的结果应该是活着,而不是死亡

EN

回答 1

Stack Overflow用户

发布于 2022-01-26 19:37:23

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

代码语言:javascript
复制
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();
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70827342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档