坚持写一个c代码,请考虑给我指导

时间:2015-06-02 02:14:46

标签: c arrays loops

我正在写一个C程序。

该代码使用一个函数来返回生物体占据的相邻细胞的数量。

代码以占用空间的初始配置开始,并通过将两个简单规则应用于上一代来创建新空间。

到目前为止,这是我的代码。

#include <stdio.h>
int main()
{
    //creating memory space for a grid
    int row, col, i, j, k, l, m, n;
    char a, b, c, d;
    char grid[10][10];
    row = 10;
    col = 10;

    //setting entire grid to '-'
    a = '-';
    for (i = 0; i< row; i++){
        for(j = 0; j< col; j++){
        grid[i][j] = a;
        }
     }
     return 0;
}

1 个答案:

答案 0 :(得分:2)

我分两步修改了你的代码,两个代码都在下面,注释说明了我的所作所为。我应该提及(并以粗体显示)您的代码超出了数组引用范围,而我发布的代码仍然具有超出范围的引用。在以occx == 0y == 0x == 9以不同的方式检查时,您需要处理y == 9函数中的案例,以避免超出范围的引用< / p>

第一个修改只是为了简化你的代码,你有很多变量(超过你需要的)和一些重复的代码。下面基本上是代码缩减:

#include <stdio.h>

char grid[10][10];

// Moved this function here
int occ(int x, int y) {
    int i, j, count;
    char gridCopy[10][10];

    // Just trying to make a copy of the original grid to work on
    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
            gridCopy[i][j] = grid[i][j];

    // Checking on the value of the neighboring cells
    count = 0;
    if (gridCopy[x-1][y+1] == '*')
        count++;
    if (gridCopy[x][y+1] == '*')
        count++;
    if (gridCopy[x+1][y+1] == '*')
        count++;
    if (gridCopy[x-1][y] == '*')
        count++;
    if (gridCopy[x+1][y] == '*')
        count++;
    if (gridCopy[x-1][y-1] == '*')
        count++;
    if (gridCopy[x][y-1] == '*')
        count++;
    if (gridCopy[x+1][y-1] == '*')
        count++;

    return count;
}

// You probably are going to do this often in the future
// Make a function for it instead of having the same code often
void printGrid() {
    int i, j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++)
            printf("%c", grid[i][j]);
        printf("\n");
    }
}

int main() {
    int i, j;

    // Setting entire grid to '-'
    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
            grid[i][j] = '-'; // No need for a variable, just use '-' directly

    // Printing out grid
    printf("This is the original array\n");
    printGrid();

    // Setting a few random places to '*'
    grid[2][3] = '*'; // No need for a variable, just use '*' directly
    grid[3][4] = '*'; // No need for a variable, just use '*' directly
    grid[7][2] = '*'; // No need for a variable, just use '*' directly
    grid[8][1] = '*'; // No need for a variable, just use '*' directly

    // Printing out grid
    printf("This is the new array\n");
    printGrid();

    printf("The number of neighbors is: %d\n", occ(2, 4));

    return 0;
}

第二个修改是在确定生命,死亡或生命(出生)之后计算阵列的新状态。我修改了occ函数,而是返回'-''*'。新状态基于原始状态计算,然后新状态将覆盖旧状态。这在康威的生命游戏中很重要,因为没有副本,生与死的变化将影响下一次计算生死的迭代:

#include <stdio.h>

char grid[10][10];

// Instead of returning the count this now returns '-' or '*'
char occ(int x, int y) {
    // Calculate based on the old state (grid)
    int count = 0;
    if (grid[x-1][y+1] == '*')
        count++;
    if (grid[x][y+1] == '*')
        count++;
    if (grid[x+1][y+1] == '*')
        count++;
    if (grid[x-1][y] == '*')
        count++;
    if (grid[x+1][y] == '*')
        count++;
    if (grid[x-1][y-1] == '*')
        count++;
    if (grid[x][y-1] == '*')
        count++;
    if (grid[x+1][y-1] == '*')
        count++;

    if (grid[x][y] == '-') { // If the cell was dead...
        if (count == 3)
            return '*'; // The cell came to life
        else
            return '-'; // The cell is still dead
    }
    else { // If the cell was alive...
        if (count == 2 || count == 3)
            return '*'; // The cell is still alive
        else
            return '-'; // The cell died
    }
}

void printGrid() {
    int i, j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++)
            printf("%c", grid[i][j]);
        printf("\n");
    }
}

int main() {
    int i, j;

    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
            grid[i][j] = '-';

    grid[2][3] = '*';
    grid[2][5] = '*'; // Added this so that everything does not die
    grid[3][4] = '*';
    grid[7][2] = '*';
    grid[8][1] = '*';

    printf("Original state\n");
    printGrid();

    // Calculate the new state
    char gridCopy[10][10];
    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
            gridCopy[i][j] = occ(i, j);

    // Copy the new state into the old state
    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
            grid[i][j] = gridCopy[i][j];

    printf("New state\n");
    printGrid();

    return 0;
}

如果您有任何问题我乐意回答,我之前已经完成了康威的生命游戏