生命游戏,C检查生活状况

时间:2015-06-18 02:40:07

标签: c multidimensional-array conways-game-of-life

我已经看了一段时间并且到目前为止已经重复了三次代码,这就是我所拥有的以及我不理解的内容。

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

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而死细胞是一个空白区域。

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的当前状态复制到结构中的2d数组中,并将其存储在数组中供以后使用,使用Universe的当前快照计算单元格数,然后编辑Universe完美运行!非常感谢你!

2 个答案:

答案 0 :(得分:4)

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

  1. 正如@ogga在评论中所说,你没有检查getlivecellcounts的上限。你在检查i和j大于0时检查了下限,但是你也需要为上限检查它(只需检查它是否超过数组的大小以检查单元格是死还是活)。 / p>

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

  3. *细胞的更换应该在同一代进行。

    其他方式可能是你制作一个不同的二维数组并在那里保存新版本或新一代,然后在完成后复制到上一代。这样你就可以同时改变世代。

答案 1 :(得分:0)

一般来说,

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

2)相同的阵列不能用于更新目标小区内容,因为这可以改变某个相邻小区成为目标小区时的邻居计数。解决这个问题的方法是保留两个2D数组,并在应用所有更新后使用指针切换到最新版本。