如果生命游戏中的语句重置板而不是遵守规则

If statements in Game of Life resetting board instead of adhering to rules

我正在尝试创建生命游戏,但我在代码的某些方面遇到了问题。首先,当我单击方块并通过按下开始按钮告诉生活游戏开始时,所有方块都变为空白,而不是遵循我在更新方法中描述的规则。无论我推到什么位置或多少个方块,它们都会返回 false,就好像我正在重置游戏一样。我认为我的 for 循环是正确的,但我认为我的一个 if 语句中可能遗漏了一些东西。任何帮助,将不胜感激。

public class GameOfLife 
{   
    private int row;
    private int col;
    private boolean[][] grid;
    private LifeBoard board;


        public static void main(String[] args) 
        {   
            GameOfLife game = new GameOfLife();
        }

        public GameOfLife()
        {
            board = new LifeBoard( this );
            grid = new boolean[board.getGridSize()][board.getGridSize()];
        }

        public void clickSquare( int row, int column ) 
        {
             if (grid[row][column] == false)
             {
                 grid[row][column] = true;   
             }
             else
             {
                 grid[row][column] = false; 
             }
            return;  
        } 

        public void resetGrid()
        {
            for (row = 0; row < grid.length; row++)
            {
                for (col = 0; col < grid[row].length; col++)
                {
                     grid[row][col]= false;
                }

            }   
            return;
        }

        public void updateGrid() 
        {
            boolean[][] grid2 = new boolean[grid.length][grid.length];
            //Nested for loop to go through entire array
            for (row = 0; row < grid.length; row++)
            {
                for ( col = 0; col < grid[row].length; col++)
                {
                    int neighbors = 0;
                    /*If statements to determine if cell is alive and whether and then to 
                    add neighbors depending upon position.*/
                    if (row > 0 && col > 0 && grid[row-1][col -1] == true)
                    {
                        neighbors++;
                    }

                    if (col > 0 && grid[row][col-1] == true)
                    {
                        neighbors++;
                    }

                    if (col > 0 && row < grid.length-1 && grid[row+1][col-1] == true)
                    {
                        neighbors++;
                    }

                    if (row > 0 && grid[row-1][col] == true)
                    {
                        neighbors++;
                    }

                    if (col < 0 && row < grid.length-1 && grid[row+1][row-1] == true)
                    {
                        neighbors++;
                    }

                    if (row > 0 && col < grid.length-1 && grid[row-1][col+1] == true)
                    {
                        neighbors++;
                    }

                    if (col < grid.length-1 && grid[row][col+1] == true)
                    {
                        neighbors++;
                    }

                    if (row < grid.length-1 && col < grid.length-1 && grid[row+1][col+1] == true)
                    {
                        neighbors++;
                    }

                    //If there are two or three neighbors than the grid remain true for those values
                    if (grid[row][col] == true)
                    {
                        if (neighbors == 2 || neighbors == 3 )
                        {
                        grid2[row][col] = true;
                        }
                        else 
                        {
                            grid2[row][col] = false;
                        }

                    if (grid[row][col] == false)
                    {
                        if (neighbors > 2)
                        {
                            grid2[row][col] = false;
                        }
                        if (neighbors == 3)
                        {
                            grid2[row][col] = true;
                        }   
                    }
                    }
                }
                grid = grid2;
            }
        }

        public boolean[][] getGrid()
        {
            return grid;
        }

col可以小于0吗?您是不是要使用 row 两次而不是 col 和 row?:

                    if (col < 0 && row < grid.length-1 && grid[row+1][row-1] == true)
                    {
                        neighbors++;
                    }

你在哪里:

if (grid[row][col] == true)
{
    if (neighbors == 2 || neighbors == 3 )
    {
        grid2[row][col] = true;
    }
    else 
    {
        grid2[row][col] = false;
    }

if (grid[row][col] == false)
{
    if (neighbors > 2)
    {
        grid2[row][col] = false;
    }
    if (neighbors == 3)
    {
        grid2[row][col] = true;
    }   
}

您忘记关闭第一个 if。此外,它应该是一个 else,这样第二个块就不会撤消第一个块所做的事情。

其次,关闭for循环后应设置grid = grid2。因此,您不会在迭代过程中更改 grid

维基百科 article on Conway's Game of Life 列出了以下规则:

  1. 任何少于两个活邻居的活细胞都会死亡,好像是由人口不足引起的。
  2. 任何有两个或三个活邻居的活细胞都会存活到下一代。
  3. 任何拥有三个以上活邻居的活细胞都会死亡,就像过度拥挤一样。
  4. 只要有三个活的邻居,任何死细胞都会变成活细胞,就像通过繁殖一样。

将其翻译成代码:

if (grid[row][col]) {
    if (neighbors < 2 || neighbors > 3) {
        // 1. Any live cell with fewer than two live neighbours dies
        // 3. Any live cell with more than three live neighbours dies
        grid2[row][col] = false;
    }
    // 2. Any live cell with two or three live neighbours lives on. Does nothing.
} else if (neighbors == 3) {
    // 4. Any dead cell with exactly three live neighbours becomes a live cell.
    grid2[row][col] = true;
}

作为旁注,方法 clickSquare() 只是反转 grid[row][column] 中的值,因此它可以简单地实现为:

public void clickSquare(int row, int column) {
    grid[row][column] = !grid[row][column];
}