几天来,我一直在努力找出背后的问题。我认为这是对邻居的错误计数,因为当我打印计数时,数字大多为1s和2s,而我的输出板完全空白。我
void NextGen(char lifeBoard[][MAX_ARRAY_SIZE], int numRowsInBoard, int numColsInBoard) {
char nexGenBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];
// initialize nexGenBoard to blanks spaces
for(int i = 0; i < numRowsInBoard; i++) {
for(int j = 0; j < numColsInBoard; j++) {
nexGenBoard[i][j] = {' '};
}
}
for(int i = 1; i < numRowsInBoard-1; i++) {
for(int j = 1; j < numColsInBoard-1; j++) {
int count = 0;
for(int y = -1; y < 2; y++) {
for(int x = -1; x < 2; x++) {
if(!(x == 0 || y == 0)) {
if(lifeBoard[i+y][j+x] == X) //X is a global constant of 'X'.
{
count++;
}
}
}
}
if(lifeBoard[i][j] == X) {
if(count == 2 || count == 3) {
nexGenBoard[i][j] = X;
}
}
else if(lifeBoard[i][j] == ' ') {
if(count == 3) {
nexGenBoard[i][j] = X;
}
}
}
}
for(int i = 0; i < numRowsInBoard; i++) {
for(int j = 0; j < numColsInBoard; j++) {
lifeBoard[i][j] = nexGenBoard[i][j];
}
}
}
您在计数(!(x == 0 || y == 0))
期间的检查是错误的。如果x或y为零,则不会检查平方。您不想计数x和y是否均为零。
if (!(x == 0 && y == 0))
或
if (x != 0 || y != 0)