c-为什么我的滑翔机从不删除,而是留在我的《生命游戏》模拟游戏的屏幕底部



我正试图编写一个基于控制台的康威生活游戏模拟器,但由于某种原因,当我的滑翔机到达屏幕底部时,它们会变成一个永远不会消失的固定的2*2正方形,我不知道它们为什么会在那里,也不知道是什么原因造成的。要看到它们,你可能需要等五六代人。

代码:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#ifdef __unix__
# include <unistd.h>
#elif defined _WIN32
# include <windows.h>
#define sleep(x) Sleep(1000 * (x))
#endif
int sizeX = 20;
int sizeY = 20;
int grid[20][20] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
int count_nbr(int grid[sizeX][sizeY], int x, int y){
int n_count = 0;
if (grid[x-1][y-1]==1)
n_count++;
if(grid[x-1][y]==1)
n_count++;
if(grid[x-1][y+1]==1)
n_count++;
if(grid[x][y-1]==1)
n_count++;
if(grid[x][y+1]==1)
n_count++;
if(grid[x+1][y-1]==1)
n_count++;
if(grid[x+1][y]==1)
n_count++;
if(grid[x+1][y+1]==1)
n_count++;
return n_count;
}
int main(void){
int neighbour_count[sizeX][sizeY];
int x,y,iterations;
for(iterations=0;iterations<500;iterations++){
system("cls"); //Clear screen
for(x=0;x<sizeX;x++){
printf("n");
for(y=0;y<sizeY;y++){
if(grid[x][y]==1){
printf("@");
}
else{
printf(" ");
}
neighbour_count[x][y] = count_nbr(grid,x,y);
}
}

for(x=0;x<sizeX;x++){
for(y=0;y<sizeY;y++){
if(neighbour_count[x][y] < 2 || neighbour_count[x][y] > 3)
grid[x][y] = 0;
else if(neighbour_count[x][y] == 3)
grid[x][y]=1;
}
}
printf("n");
}
}

您的代码中可能存在错误,但您观察到的行为是而不是。相反,这正是你在Conway的《生命的游戏》中所期望的在有限网格上玩的游戏,边界条件指定网格边缘之外的所有细胞都将始终被视为死细胞:

1:        2:        3:        4:        5:        6:
......    ......    ......    ......    ......    ......
...X..    ..X...    ...X..    ......    ......    ......
.X.X.. -> ...XX. -> ....X. -> ..X.X. -> ....X. -> ...XX.
..XX..    ..XX..    ..XXX.    ...XX.    ...XX.    ...XX.
######    ######    ######    ######    ######    ######

这里,X表示活细胞,.表示可能变为活细胞的死细胞,而#表示总是被视为死细胞的网格边界之外的细胞。

在上面的步骤3中,用#标记的常死细胞中的一个具有三个活邻居。如果它是一个正常的死细胞,它将在第4步变成活细胞,滑翔机将继续繁殖。但因为它不能变成活的,所以在第4步中实际出现的是一个由滑翔机组成的四细胞模式,其中缺少一个活细胞。然后,这种模式分两步演变为2x2块(并保持这种方式,因为块是一个稳定的静物(。

相关内容

  • 没有找到相关文章

最新更新