二维字符数组 (C) 中的邻居数量不正确

Incorrect number of neighbours in a 2d char array (C)

我一直在为 Game of Life 开发一个程序,我 认为 快到了,但是我唯一的挫折是该程序错误地读取了邻居的数量, 因此输出不正确。

#include <stdio.h>
#include <stdbool.h>

int numRows;
int numColumns;
int steps;

void printGrid(char grid[numRows][numColumns]) {

int i, j;
for(i=0; i<numRows; i++) {
    for(j=0;j<numColumns;j++) {
        printf("%c", grid[i][j]);
        if (j == numRows - 1) {
            printf("\n");
        }
    }
}printf("\n");
}

int neighbourValue(int row, int column, char grid[row][column]) {
if(row < 0 ||
   row > numRows -1 ||
   column < 0 ||
   column > numColumns -1 ||
   grid[row][column] == '.') {
    return 0;
} else {
    return 1;
}
}

int getNeighbourCount(int row, int column, char grid[row][column]) {
int neighbour = 0;

neighbour += neighbourValue(row-1, column-1, grid); // Above Left
neighbour += neighbourValue(row-1, column, grid); // Above
neighbour += neighbourValue(row-1, column+1, grid); // Above Right
neighbour += neighbourValue(row, column-1, grid); // Left
neighbour += neighbourValue(row, column+1, grid); // Right
neighbour += neighbourValue(row+1, column-1, grid); // Below Right
neighbour += neighbourValue(row+1, column, grid); // Below
neighbour += neighbourValue(row+1, column+1, grid); //Below Left

return neighbour;
}

void calculations(int row, int column, char grid[row][column]) {
char grid2[row][column];
int neighbour, x, y;

for(x = 0; x < numRows; x++) {
    for(y = 0; y < numColumns; y++) {
        neighbour = getNeighbourCount(x, y, grid);
        if(neighbour == 3) {
            grid2[x][y] = 'X';
        } else if (neighbour == 2 && grid[x][y] == 'X') {
            grid2[x][y] = 'X';
        } else {
            grid2[x][y] = '.';
        }
    }
} printf("\n %d \n", getNeighbourCount(0,1, grid));

我在这里包含了一些调试打印语句,它打印出上面打印的网格在 (0,1) 处的邻居数。

for(x = 0; x < numRows; x++) {
    for(y = 0; y < numColumns; y++) {
        grid[x][y] = grid2[x][y];
    }
}
printGrid(grid);
}

int main() {

int counter = 0;

scanf("%d %d %d", &numRows, &numColumns, &steps); //Setting up the array

char grid[numRows][numColumns];

int i, j;
for(i=0; i<numRows; i++) {
    for(j=0;j<numColumns;j++) {
        scanf(" %c", &grid[i][j]);
    }
}
printGrid(grid);

do{
    calculations(numRows, numColumns, grid);
    counter++;
}while(counter < steps);
}

我已经包括了整个代码,但我认为问题出在 neighbourValue 周围,因为边界可能是错误的。 典型的输入是 3 3 3(行列步长) 接着是 XXX XXX XXX。 任何人都能够测试它并查看它哪里出了问题吗? 我偷偷怀疑 0,1 的位置(在其他位置中,可能是 'wrapping' 和阅读网格另一端的邻居。

在 OP 的代码中,grid 被声明为可变长度二维数组:

int numRows;      // global variables
int numColumns;
// ...
scanf("%d %d %d", &numRows, &numColumns, &steps);
char grid[numRows][numColumns];

但它的尺寸仅作为参数正确传递给 calculation 函数:

void calculations(int row, int column, char grid[row][column])
//      this signature ^^      ^^^              ^^^   ^^^^
// is common to almost all of the OP's functions

calculations(numRows, numColumns, grid);
//           ^^^^^^^   ^^^^^^ correct parameters, the sizes of the grid

而在其他函数的调用中,传递了错误的参数:

// e.g.
int neighbourValue(int row, int column, char grid[row][column])
//                     ^^^      ^^^^^^            ^^^  ^^^^^^
neighbour += neighbourValue(row-1, column-1, grid); // Above Left
//                           ^^^     ^^^^   

在那些情况下,OP 应该传递网格的实际大小:

int neighbourValue(int row, int column, int numColumns, char grid[][numColumns])
//                                           ^^^^^^^^                  ^^^^^^