J. Guy
J. Guy

Reputation: 21

Conway Game of Life not working count neighbours method not working

The following pice of code is my code for my Game of Life. For some reason it works very strangely. The first few steps of the game are wrong, and then the whole output turns to zeroes. I don't know which method is causing the problem, but I assume it is the count neighbors method.

The board is a 2D array called GOLBoard, and the x and y coordinates are cellRow and cellCol. Alive cells are 1, and dead cells are 0s. The way I avoid out of bounds problems are by making the board 12x12, but I use only 1 to 11 for rows and columns.

<code>    @Override
    public int countNeighbours(int cellRow, int cellCol) {
        int neighbours = 0;
        for (int i = cellRow-1; i < cellRow + 2; i++) {
                for (int j = cellCol - 1; j < cellCol + 2; j++) {
                    if (GOLBoard[i][j] == 1) {
                        neighbours += 1;
                    }
                }
            }
        if (GOLBoard[cellRow][cellCol] == 1) {
            return(neighbours-1);
        } else {
            return(neighbours);
        }

    }



    @Override
    public int applyRules(int cellRow, int cellCol) {
        int alive = 0;
        if (GOLBoard[cellRow][cellCol] == 1) {
            if (countNeighbours(cellRow, cellCol) == 2 || countNeighbours(cellRow, cellCol) == 3) {
                alive = 1;
            } else if (countNeighbours(cellRow, cellCol) < 2 || countNeighbours(cellRow, cellCol) > 3) {
                alive = 0;
            } 
        }


        if (GOLBoard[cellRow][cellCol] == 0) {
            if (countNeighbours(cellRow, cellCol) == 3) {
                alive = 1;
            }
        }
        return (alive);
    }

    @Override
    public void takeStep() {
        for (int row = 1; row < 11; row++) {
            for (int col = 1; col < 11; col++) {
                GOLBoard[row][col] = applyRules(row, col);
            }
        }
    }



    @Override
    public String toString() {
        for (int row = 1; row < 11; row++) {
            for (int col = 1; col < 11; col++) {
                System.out.print("[" + GOLBoard[row][col] + "]");
            }
            System.out.println("");
        }
        return("");
    }
} <code>

Upvotes: 1

Views: 161

Answers (1)

Kevin Anderson
Kevin Anderson

Reputation: 4592

If I am recalling the rules of GOL correctly, the evolution of the board should proceed in a series of "generations", wherein the state of each cell on a "new" board is determined solely by the conditions of the corresponding cell on the "old" board. Your program is trying to continuously evolve a single board, so that changes to previously computed cells are affecting the outcome of cells yet to be computed. Your takeStep routine should be doing something more like this:

newBoard = new int[12][12];
for (int row = 1; row < 11; row++) {
    for (int col = 1; col < 11; col++) {
        newBoard[row][col] = applyRules(row, col);
    }
}
GOLBoard = newBoard;

As far as I can tell, your countNeighbours is OK.

Upvotes: 1

Related Questions