//老师说说我这个生命游戏的代码太单调了,需要加点东西,各位大神请帮忙看看
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;
void gotoxy(int x,int y)
{
HANDLE h;
COORD c;
c.X=x;
c.Y=y;
h=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h,c);
}


int main()
{
int dian[100][100]={0};/*分别记录每次迭代的初始和结果状态*/
srand(time(NULL));
for(int u=0;u<=200;++u)
{
int x,y;
x=rand()%78;y=rand()%23;
dian[x][y]=2;
}
int i,j,k;
while(1)
{
_sleep(2000);
system("cls");
for(i=0;i<78;i+=1)
for(j=0;j<23;j+=1)
{
if(dian[i][j]==2)
{
gotoxy(i,j);
cout<<"+";
}
}
for(i=2;i<78;i+=1)/*初始化数据,令每一个细胞为生*/
for(j=2;j<23;j+=1)
{
if(dian[i][j]==2)
{
k=0;
if(dian[i-1][j-1]==1) ++k;
if(dian[i-1][j]==1) ++k;
if(dian[i-1][j+1]==1) ++k;
if(dian[i][j-1]==1) ++k;
if(dian[i][j+1]==1) ++k;
if(dian[i+1][j-1]==1) ++k;
if(dian[i+1][j]==1) ++k;
if(dian[i+1][j+1]==1) ++k;
if(k<=1) dian[i][j]=0;
else if(k>3) dian[i][j]=0;
}
/*计算每一个细胞周围的活的细胞数*/
else
{
k=0;
if(dian[i-1][j-1]==1) ++k;
if(dian[i-1][j]==1) ++k;
if(dian[i-1][j+1]==1) ++k;
if(dian[i][j-1]==1) ++k;
if(dian[i][j+1]==1) ++k;
if(dian[i+1][j-1]==1) ++k;
if(dian[i+1][j]==1) ++k;
if(dian[i+1][j+1]==1) ++k;
if(k>=3) dian[i][j]=2;
}
}
}
}