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

标签 c multidimensional-array conways-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 处的位置(在其他位置中,可能是“环绕”并读取网格另一端的邻居。

最佳答案

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

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

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

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])
//                                           ^^^^^^^^                  ^^^^^^

关于c - 二维字符数组中的邻居数量不正确 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49256540/

相关文章:

javascript - 为什么我不能在 JS 中复制这个二维数组?我怎样才能制作副本?

算法 - 关于 Conways 生命游戏的极端细节

c++ - 使用 fork() 的子和父 pid;

c++ - 如何将 aurioTouch 文件添加到自定义程序?

C - 运行此应用程序时崩溃

c++ - 返回多维数组 C++

javascript - 如何使用基于另一个参数的对象列表中的参数? (JavaScript)

java - 复制二维数组 - 仍然使用引用?

algorithm - 优化 Conway 的 'Game of Life'

c - 文件无法在 C 中打开