我正在研究大学问题的Conway游戏。我已经能够打印第一代和第二代,但是当涉及到第二代时,它们都可以复制。我想知道你们是否能帮助我。
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();
}
}
最佳答案
计算完新板后,您必须将其分配回类字段board
。添加为computeNextGeneration board = newboard
的最后一行。