Conway的生活游戏:检查邻居工作不正常(c ++)

问题描述 投票:0回答:1

几天来,我一直在努力找出背后的问题。我认为这是对邻居的错误计数,因为当我打印计数时,数字大多为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];
        }
    }
}
c++ conways-game-of-life
1个回答
0
投票

您在计数(!(x == 0 || y == 0))期间的检查是错误的。如果x或y为零,则不会检查平方。您不想计数x和y是否均为零。

if (!(x == 0 && y == 0))

if (x != 0 || y != 0)
© www.soinside.com 2019 - 2024. All rights reserved.