生命游戏,C 检查生活条件

Game of Life, C checking life conditions

看了一段时间,重写了三遍代码,这是我有的,我不懂的。

我有一种方法可以通过各种打印行和我的 运行 检查邻域,这很有效。

int getLiveCellCount(Generation *currentGeneration, int i, int j)
{
int liveCellCount = 0;
// check top row
if( i > 0 )
{
    if( j > 0 && currentGeneration->generation[i-1][j-1] == 'X' )
    {
        liveCellCount++;
    }
    if( currentGeneration->generation[i-1][j] == 'X' )
    {
        liveCellCount++;
    }
    if( j < currentCols && currentGeneration->generation[i-1][j+1] == 'X' )
    {
        liveCellCount++;
    }

}

// check mid row
if( j > 0 && currentGeneration->generation[i][j-1] == 'X' )
{
    liveCellCount++;
}
if( j < currentCols && currentGeneration->generation[i][j+1] == 'X' )
{
    liveCellCount++;
}

// check bottom row
if( i < currentRows )
{
    if( j > 0 && universe[i+1][j-1] == 'X' )
    {
        liveCellCount++;
    }
    if( currentGeneration->generation[i+1][j] == 'X' )
    {
        liveCellCount++;
    }
    if( j < currentCols && currentGeneration->generation[i+1][j+1] == 'X' )
    {
        liveCellCount++;
    }
}

return liveCellCount;
}

我有特定的细胞生存或死亡条件,活细胞包含 X 而死细胞是空白 space。

If the cell is alive:
   it dies if it has 0, 1, 4 or more living neighbours (starvation), or
   it lives if it has 2 or 3 living neighbours (balance).
If the cell is dead:
   it springs to life if it has exactly 3 neighbours (procreation).

我实现代码如下:

for( i=0; i<currentRows; i++ )
        {
            for( j=0; j<currentCols; j++ )
            {
                int livingCells = 0;
                livingCells = getLiveCellCount(currentGeneration, i,j);
                if(universe[i][j] == 'X' )
                {
                    if( livingCells == 2 || livingCells == 3 )
                    {
                        universe[i][j] = 'X';
                    }
                    else
                    {
                        universe[i][j] = ' ';
                    }
                }
                else
                {
                    if( livingCells == 3 )
                    {
                        universe[i][j] = 'X';
                    }
                }
            }
        }

知道universe[][]是一个文件作用域变量,我用这段代码的想法是在初始条件下读入universe,这行得通。我将这个数组复制到一个结构数组中(存储以备后用,当前已注释掉)。我扫描 universe 并根据上面的规则检查其附近的每个细胞是否有活细胞,并逐个元素地编辑 universe。 我错过了什么?在某个地方,我没有正确读取条件,我看不到它。

感谢大家对我的帮助!就像你们中的许多人提到的那样,我忽略了宇宙中每个细胞必须同时更新的小细节!正如我提到的,我将 Universe 的当前状态复制到结构中的二维数组中,并将其存储在一个数组中供以后使用,使用 Universe 的当前快照来计算单元格计数,然后完美地编辑 Universe!非常感谢!

我认为您的代码有两个问题:

  1. 正如@ogga 在评论中所说,您没有检查 getlivecellcounts 中的上限。当您检查 i 和 j 大于 0 时,您检查了下限,但您也需要为上限执行此操作(只需检查它不超过数组的大小以检查细胞是死的还是活的)。

  2. 在您的 for 循环中,我认为您是在即时计算活细胞计数。这种方法的问题是当你在某个单元格 [x][y] 上并且它是活的并且它有一些邻居时你将它更改为死单元格然后移动到下一个单元格。这会给你单元格 [x] [y+1] 的邻居数量不正确,因为你已经将单元格 x y 的状态更改为新一代,现在已经死了,以前还活着。

*更换电池应在同一代完成。

其他方法可能是制作一个不同的二维数组并在其中保存新版本或新一代,然后在完成后复制到上一个。这样,您将同时改变几代人。

一般来说,

1) 必须始终检查相邻小区的状态 检查被检查的单元格是否在二维数组的范围内。

2) 不能使用同一个数组来更新目标单元格内容,因为当某个相邻单元格成为目标单元格时,这会改变相邻单元格的计数。解决这个问题的方法是保留两个二维数组,并在应用所有更新后使用指针切换到最新版本。