I am working on Conway game of college problem. I have been able to print the first generation and the second but when it comes to the following ones they all copy the second generation. I was wondering if y'all can help me out.
public void computeNextGeneration(int generation)
{
char[][] newBoard = new char[board.length][board[0].length];
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[0].length; j++)
{
if(board[i][j] == '0' && numOfNeighbors(i,j) == 3)
{
newBoard[i][j] = 'X';
}
else if(board[i][j] == 'X' && numOfNeighbors(i,j) < 2)
{
newBoard[i][j] = '0';
}
else if(board[i][j] == 'X' && numOfNeighbors(i,j) >3)
{
newBoard[i][j] = '0';
}
else if(board[i][j] == 'X' && numOfNeighbors(i,j) == 2 || numOfNeighbors(i,j) == 3)
{
newBoard[i][j] = 'X'; //change x to 0
}
else{
newBoard[i][j] +=board[i][j];
}
}
}
for (int i = 0; i < newBoard.length; i++)
{
for(int j = 0; j < newBoard[0].length; j++)
{
System.out.print(newBoard[i][j]);
}
System.out.println();
}
}
After you calculate the new board you have to assign it back to the class field board
. Add as the last line of computeNextGeneration board = newboard
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments