康威的人生游戏:检查邻居是否正常工作 (c++)

Conway's game of life: checking neighbours not working properly (c++)

这几天我一直在试图弄清楚这背后的问题。我认为它对邻居的计数不正确,因为当我打印计数时,数字大多是 1 和 2,而我的输出板完全是空白的。 X ('X') 表示活着,' ' 表示死了。

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] = {' '};
        }
    }
    // start from i = 1 and j = 1 to ignore the edge of the board
    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)