Reputation: 11
I am coding Conway's Game of Life in Java and have encountered a logic error in my code. If you aren't familiar with the game, here are the basic rules:
The Game of Life is a simple simulation that takes place in a grid of cells. Each cell can be either alive or dead, and it interacts with its neighbors (horizontally, vertically, or diagonally). In each iteration, a decision will be made to see if living cells stay alive, or if dead cells become alive. The algorithm is as follows:
If a cell is alive: If it has less than two living neighbors, it dies due to loneliness. If it has two or three living neighbors, it lives to the next generation If it has more than three living neighbors, it dies due to overpopulation.
If a cell is dead: if it has exactly three live neighbors, it becomes alive due to reproduction.
The code I have so far for counting the dead cells and live cells is as follows:
// Go through each dead cell and check all neighboring cells to see if it
// will be revived
// reviveDeadCells()
neighborCount = 0;
for (y = 0; y , 15; y++ ) {
for (x = 0; x < 15; x++) {
if (board[x][y] = 0 ) {
for ( i = x - 1; i = x + 1; i ++ ) {
for (j = y - 1; j = y + 1; j ++) {
if (board[i][j] = 1 ) {
neighborCount = neighborCount + 1;
}
}
if (neighborCount = 4) {
board[i][j] = 1;
}
}
}
}
}
// Go through each live cell and see if it should be executed
// killLiveCell()
for (y = 0; y , 15; y++ ) {
for (x = 0; x < 15; x++) {
if (board[x][y] = 1 ) {
for ( i = x - 1; i = x + 1; i ++ ) {
for (j = y - 1; j = y + 1; j ++) {
if (board[i][j] = 1 ) {
neighborCount = neighborCount + 1;
}
}
if (neighborCount < 3) || (nieghborCount > 4) {
board[x][y] = 0;
}
}
}
I realize now that the problem with this code is that there is a logic error. First, I am counting all of the neighboring cells of the dead cells and then counting how many are alive. Then, I revive them if there are exactly 3 live neighbors. The only problem with this is that it will now affect the counter for the neighboring cells of the live cells. How do I change the live cells of the dead and live neighbors at the same time without affecting the counter of the other? I have a feeling I have all the code, but i probably have to move it around the for loops somewhere. I just don't know exactly where I need to put it to correct this error. Any help would be much appreciated, thanks.
Upvotes: 1
Views: 96
Reputation: 159
Instead of killing/reviving the cell then and there, store the co-ordinates of the cells that will change state as a tuple in a new list. There are a bunch of ways to do this, but as an example;
// Create a simple tuple class for the co-ordinates
Class CoordPair(){
int x;
int y;
CoordPair(int x, int y){
this.x = x;
this.y = y;
}
}
// In your code, use Hash Set to prevent having two copies of the Coordinates
HashSet<CoordPair> changeSet = new HashSet<CoordPair>();
...
if (neighborCount = 4) {
CoordPair changePair = new CoordPair(i,j);
changeSet.add(changePair);
}
...
// After identifying all the changing pairs on the board
for(CoordPair pair : changeSet){
board[pair.x][pair.y] ^= 1; //XOR to flip the value
}
Upvotes: 0