生命游戏C ++,检查邻居

时间:2014-02-21 04:22:46

标签: c++ conways-game-of-life

我正在尝试撰写康威的生命游戏。

不幸的是,当我去检查块的邻居时,我总是会在阵列中的某些元素上收到错误。特别是在grid[0][11],它给了我一个邻居,但是,我设置了这样,当我周围的一个块不是空格时,我只会为一个名为neighbors的变量添加值,或者在我的代码中' '

我认为,if语句中的条件也会考虑到它不会超出数组。

整个数组都填充了' ',尽管如此,我仍然可以获得邻居值。

我已经在这里工作了几个小时,但没有找到解决办法。我的代码仍然充满了尝试测试,我为它缺乏整洁而道歉。提前感谢您提供解决问题的任何帮助。

int _tmain(int argc, _TCHAR* argv[])
{
    bool infloop = true;
    //Create the playing grid.
    char grid[HEIGHT][WIDTH];
    //Comment out later. Used for testing.
    for(int i=0; i<75; i++)
        for(int j=0; j<22; j++)
            grid[i][j] = ' ';

    //Create initial seed here. grid [x coordinate] [y coordinate].
    //grid [1][1] = '+'; grid [2][1] = '+'; grid [3][1] = '+';

    //Key. * is going to live. + is alive currently. 
    //- is going to die, and negative space is dead.

    //As Conway's Game of Life runs infinitely, create an infinite loop.
    while (infloop)
        generation(grid);


    cout << endl;
    system("pause");
    return 0;
}

void generation(char grid[][WIDTH]) {

    int neighbors;

    /*Check each point on the grid for alive or dead. If it is, check the 
    surrounding neighbors and apply the game's rules.*/
    for(int x=0; x<75; x++) {
        for(int y=0; y<22; y++)
        {

            neighbors = 0;
            /*check all eight neighbors except for when outside of the 
            array.*/
            if((grid[x+1][y] != ' ') && (grid[x+1][y] < grid[HEIGHT][y])){
                neighbors++; cout << "A";
            }

            if((grid[x-1][y] != ' ') && (grid[x-1][y] > grid[-1][y])){
                neighbors++; cout << "E";
            }

            if((grid[x][y+1] != ' ') && (grid[x][y+1] < grid[x][WIDTH])){
                neighbors++; cout << "C";
            }

            if((grid[x][y-1] != ' ') && (grid[x][y-1] > grid[x][-1])){
                neighbors++; cout << "G";
            }

            if((grid[x+1][y+1] != ' ') && (grid[x+1][y+1] < grid[HEIGHT][WIDTH])){
                neighbors++; cout << "B";
            }

            grid[0][11] = ' '; grid[11][0] = ' ';
            if((grid[x-1][y-1] != ' ') && (grid[x-1][y-1] > grid[-1][-1])){
                neighbors++; cout << "F";
            }

            if((grid[x+1][y-1] != ' ') && (grid[x+1][y] < grid[HEIGHT][y]) &&
                (grid[x][y-1] > grid[x][-1])){
                    neighbors++; cout << "H";
            }

            if((grid[x-1][y+1] != ' ') && (grid[x-1][y] > grid[-1][y])
                && (grid[x][y+1] < grid[x][WIDTH])){
                    neighbors++; cout << "D";
            }

            system("pause");

            cout << neighbors;

            //Set a marker for each point according to neighbor amounts and key.
            if(grid[x][y] == '+' && neighbors < 2)
                grid[x][y] = '-';

            if(grid[x][y] == '+' && (neighbors == 2 || neighbors == 3))
                grid[x][y] = '*';

            if(grid[x][y] == '+' && neighbors > 3)
                grid[x][y] = '-';

            if(grid[x][y] == ' ' && (neighbors == 3))
                grid[x][y] = '*';
        }
    }
    for(int x=0; x<75; x++){
        for(int y=0; y<22; y++)
        {
            if(grid[x][y] == '*')
                grid[x][y] = '+';

            if(grid[x][y] == '-')
                grid[x][y] = ' ';
        }
    }
    system("pause");
    display(grid);
}

3 个答案:

答案 0 :(得分:2)

这是一种检查细胞邻居是否存活的简单方法。将以下代码放在函数中,并传入要检查的单元格的行和列。

int live_cell_count = 0;
for (int i = -1; i <= 1; i++)
{
    for (int j = -1; j <= 1; j++)
    {
        //disregard grid[row][col]
        if (!(i == 0 && j == 0) && inBounds(row+i,col+j)
                 && grid[row+i][col+j] == '+')
              live_cell_count++;
    }
}

请注意,bool inBounds(int, int)是一个检查以确保您不会离开阵列的函数。如果您使用40 x 30网格,inBounds()将是这个简单的单行

return ((row >= 0 && row < 40) && (col >= 0 && col < 30));

我将其作为练习留给OP根据live_cell_count的价值应用生活规则

答案 1 :(得分:1)

当您要检查索引时,邻居检查当前正在检查内容。另外,请确保在检查内容之前检查之前的索引,否则您将遇到数组超出范围的问题。

这是第一个修复的:

if ((x+1 < HEIGHT) && (grid[x+1][y] != ' ')) { // Swap and index instead of contents.
   neighbors++; cout << "A";
}

答案 2 :(得分:0)

75最好由HEIGHT替换,22替换为WIDTH

if((grid[x+1][y] != ' ') && (x+1 < HEIGHT)) {
        neighbors++; cout<<"A";
}

应该是

if((x+1 < HEIGHT) && (grid[x+1][y] != ' ')) { // The range check should be done firstly
        neighbors++; cout<<"A";
}