Search code examples
javajappletconways-game-of-life

Conway's game of life detecting extra live cells


I am in the midst of writing Conway's game of life, but my detection of the live cells reveals a few extras the code throws in when I try to set up a blinker (3 live cells in a row) with the cell coordinates of [36][22] till [36][24]. My updating method for iterations is this:

private void nextGeneration() {
    for (int i = 0; i < cell.length; i++) {
        for (int j = 0; j < cell[i].length; j++) {
            if(i>0 && i<79 && j>0 && j<99){
                if(cell[i][j].getAlive()){
                    cell[i][j].calcNeighbors(cell, i, j);
                    if(cell[i][j].getNeighbors() < 2){
                        cell[i][j].setAlive(false);
                    }
                    if(cell[i][j].getNeighbors() == 2 || cell[i][j].getNeighbors() == 3 && cell[i][j].getAlive()){
                        cell[i][j].setAlive(true);
                    }
                    if(cell[i][j].getNeighbors() > 3){
                        cell[i][j].setAlive(false);
                    }
                }
                else {
                    cell[i][j].calcNeighborsForNull(cell, i, j);
                    if (cell[i][j].getNeighborsForNull() == 3) {
                        cell[i][j].setAlive(true);
                    }
                }
            }
        }
    }
}

Where nextGeneration() is called every 1 second, and cell[][] is an array of classes Cell

I detect the cells using this code:

myNeighbors = 0;
        if(cell[i-1][j-1].myAlive){
            myNeighbors++;
            System.out.println("top left");
        }
        if(cell[i-1][j].myAlive){
            myNeighbors++;
            System.out.println("top center");
        }
        if(cell[i-1][j+1].myAlive){
            myNeighbors++;
            System.out.println("top right");
        }
        if(cell[i][j-1].myAlive){
            myNeighbors++;
            System.out.println("mid left");
        }
        if(cell[i][j+1].myAlive){
            myNeighbors++;
            System.out.println("mid right");
        }
        if(cell[i+1][j-1].myAlive){
            myNeighbors++;
            System.out.println("lower left");
        }
        if(cell[i+1][j].myAlive){
            myNeighbors++;
            System.out.println("lower center");
        }
        if(cell[i+1][j+1].myAlive){
            myNeighbors++;
            System.out.println("lower right");
        }
        System.out.println(myNeighbors +" at " + j + "," + -i);

When I run the code, I get the console output of:

top right
mid right
2 at 22,-36
top center
top right
mid left
mid right
4 at 23,-36
top left 
top center
2 at 24,-36
mid right
lower left
lower right
3 at 23,-35
mid left
lower center
2 at 24,-35
top right
1 at 22,-36
top left
top center
mid left
3 at 24,-36
mid right
lower center
lower right
3 at 23,-35

the compiler thinks that the dead cell above the live one is also live. Can someone see what I'm doing wrong? Thanks.


Solution

  • Depending on how your SetAlive method works, you the cell that you think should be dead might actually have been set to alive. I had to write a Game of Life simulation for class this past semester, and the trick is to use two "boards". Since the whole "board" is supposed to be updated all at once, using your method could cause unexpected results. Iterate through each cell, but send the result to the second board so there is no overlap between the cell's state at the current step and the cell's state at the next step. It's kind of hard to explain through text, but let me know if you need any more help, since I'm thinking this is what your problem is.