Search code examples
javaalgorithmloopsif-statementconways-game-of-life

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


I am trying to create the Game of Life, but I am having issues with certain aspects of the code. First of all, when I click squares and tell the game of life to start by pushing the start button, all of the squares go blank instead of following the rules that I have described in the update method. Not matter what location or how many squares I push, they all go back to false as if I am resetting the game. I think my for loop is correct, but I think I may be missing something in one of my if statements. Any help would be appreciated.

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;
        }

Solution

  • Where you do:

    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;
        }   
    }
    

    You forgot to close the first if. Also, it should be an else so the second block doesn't undo what the first block did.

    Second, grid = grid2 should be set after you close the for loop. So you do not change grid in the middle of your iteration.

    The Wikipedia article on Conway's Game of Life lists the following rules:

    1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
    2. Any live cell with two or three live neighbours lives on to the next generation.
    3. Any live cell with more than three live neighbours dies, as if by overcrowding.
    4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

    Translating this into code:

    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;
    }
    

    As a side note, the method clickSquare() just inverts the value in grid[row][column], so it could be implemented as simply:

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