这是V3.2的更新
这里放上其他版本的链接
生命游戏代码V1.0_小学生的博客-CSDN博客
生命游戏代码V1.2_小学生的博客-CSDN博客
生命游戏代码V2.0_小学生的博客-CSDN博客
生命游戏代码V3.0_小学生的博客-CSDN博客
生命游戏代码V3.1_小学生的博客-CSDN博客
生命游戏代码V3.2_小学生的博客-CSDN博客
V4.0:更新如下
? ? ? ? 1.可使用 Ctrl 进行单步运行
已知错误:
? ? ? ? 1.调整控制台字体大小后坐标可能错位
? ? ? ? 2.点击窗口外程序可能崩溃
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<cwchar>
using namespace std;
#define SLEEP 50
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0)
int N;
bool a[1010][1010][5];
void color(int x){
HANDLE handle;
handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle,x);
}
void gotoxy(int x,int y){
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
void HideCursor(){
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle,&CursorInfo);
CursorInfo.bVisible=false;
SetConsoleCursorInfo(handle,&CursorInfo);
}
void show(int g){
color(7);
HideCursor();
gotoxy(0,0);
int i,j;
cout<<" Second = "<<g;
for(int i=10;i<=N;i++)
cout<<" ";
cout<<endl;
g%=2;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
color(240);
if(a[i][j][g]==1)
cout<<"█ ";
else if(a[i][j][g]==0)
cout<<" ";
}
cout<<endl;
color(7);
}
Sleep(SLEEP);
}
void update(int k){
int i,j,count=0;
k%=2;
if(k==0){
for(i=0;i<N;i++)
for(j=0;j<N;j++){
count=0;
if(i==0&&j==0){
if(a[i][j+1][k]==1)
count++;
else if(a[i+1][j][k]==1)
count++;
else if(a[i+1][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k+1]=0;
else if(count==2)
a[i][j][k+1]=a[i][j][k];
else if(count==3)
a[i][j][k+1]=1;
}
if(i==0&&j!=0&&j!=N-1){
if(a[i][j-1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(a[i+1][j-1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(a[i+1][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k+1]=0;
if(count==2)
a[i][j][k+1]=a[i][j][k];
if(count==3)
a[i][j][k+1]=1;
}
if(i==0&&j==N-1){
if(a[i][j-1][k]==1)
count++;
if(a[i+1][j-1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k+1]=0;
if(count==2)
a[i][j][k+1]=a[i][j][k];
if(count==3)
a[i][j][k+1]=1;
}
if(i!=0&&i!=N-1&&j==0){
count=0;
if(a[i-1][j][k]==1)
count++;
if(a[i-1][j+1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(a[i+1][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k+1]=0;
if(count==2)
a[i][j][k+1]=a[i][j][k];
if(count==3)
a[i][j][k+1]=1;
}
if(i==N-1&&j==0){
count=0;
if(a[i-1][j][k]==1)
count++;
if(a[i-1][j+1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k+1]=0;
if(count==2)
a[i][j][k+1]=a[i][j][k];
if(count==3)
a[i][j][k+1]=1;
}
if(i==N-1&&j!=0&&j!=N-1){
count=0;
if(a[i-1][j-1][k]==1)
count++;
if(a[i-1][j][k]==1)
count++;
if(a[i-1][j+1][k]==1)
count++;
if(a[i][j-1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k+1]=0;
if(count==2)
a[i][j][k+1]=a[i][j][k];
if(count==3)
a[i][j][k+1]=1;
}
if(j==N-1&&i!=0&&i!=N-1){
count=0;
if(a[i-1][j-1][k]==1)
count++;
if(a[i-1][j][k]==1)
count++;
if(a[i][j-1][k]==1)
count++;
if(a[i+1][j-1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k+1]=0;
if(count==2)
a[i][j][k+1]=a[i][j][k];
if(count==3)
a[i][j][k+1]=1;
}
if(i==N-1&&j==N-1){
count=0;
if(a[i-1][j-1][k]==1)
count++;
if(a[i-1][j][k]==1)
count++;
if(a[i][j-1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k+1]=0;
if(count==2)
a[i][j][k+1]=a[i][j][k];
if(count==3)
a[i][j][k+1]=1;
}
else if(i!=0&&j!=0&&i!=N-1&&j!=N-1){
count=0;
if(a[i-1][j-1][k]==1)
count++;
if(a[i-1][j][k]==1)
count++;
if(a[i-1][j+1][k]==1)
count++;
if(a[i][j-1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(a[i+1][j-1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(a[i+1][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k+1]=0;
if(count==2)
a[i][j][k+1]=a[i][j][k];
if(count==3)
a[i][j][k+1]=1;
}
}
}else{
for(i=0;i<N;i++)
for(j=0;j<N;j++){
count=0;
if(i==0&&j==0){
if(a[i][j+1][k]==1)
count++;
else if(a[i+1][j][k]==1)
count++;
else if(a[i+1][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k-1]=0;
else if(count==2)
a[i][j][k-1]=a[i][j][k];
else if(count==3)
a[i][j][k-1]=1;
}
if(i==0&&j!=0&&j!=N-1){
if(a[i][j-1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(a[i+1][j-1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(a[i+1][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k-1]=0;
if(count==2)
a[i][j][k-1]=a[i][j][k];
if(count==3)
a[i][j][k-1]=1;
}
if(i==0&&j==N-1){
if(a[i][j-1][k]==1)
count++;
if(a[i+1][j-1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k-1]=0;
if(count==2)
a[i][j][k-1]=a[i][j][k];
if(count==3)
a[i][j][k-1]=1;
}
if(i!=0&&i!=N-1&&j==0){
count=0;
if(a[i-1][j][k]==1)
count++;
if(a[i-1][j+1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(a[i+1][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k-1]=0;
if(count==2)
a[i][j][k-1]=a[i][j][k];
if(count==3)
a[i][j][k-1]=1;
}
if(i==N-1&&j==0){
count=0;
if(a[i-1][j][k]==1)
count++;
if(a[i-1][j+1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k-1]=0;
if(count==2)
a[i][j][k-1]=a[i][j][k];
if(count==3)
a[i][j][k-1]=1;
}
if(i==N-1&&j!=0&&j!=N-1){
count=0;
if(a[i-1][j-1][k]==1)
count++;
if(a[i-1][j][k]==1)
count++;
if(a[i-1][j+1][k]==1)
count++;
if(a[i][j-1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k-1]=0;
if(count==2)
a[i][j][k-1]=a[i][j][k];
if(count==3)
a[i][j][k-1]=1;
}
if(j==N-1&&i!=0&&i!=N-1){
count=0;
if(a[i-1][j-1][k]==1)
count++;
if(a[i-1][j][k]==1)
count++;
if(a[i][j-1][k]==1)
count++;
if(a[i+1][j-1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k-1]=0;
if(count==2)
a[i][j][k-1]=a[i][j][k];
if(count==3)
a[i][j][k-1]=1;
}
if(i==N-1&&j==N-1){
count=0;
if(a[i-1][j-1][k]==1)
count++;
if(a[i-1][j][k]==1)
count++;
if(a[i][j-1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k-1]=0;
if(count==2)
a[i][j][k-1]=a[i][j][k];
if(count==3)
a[i][j][k-1]=1;
}else if(i!=0&&j!=0&&i!=N-1&&j!=N-1){
count=0;
if(a[i-1][j-1][k]==1)
count++;
if(a[i-1][j][k]==1)
count++;
if(a[i-1][j+1][k]==1)
count++;
if(a[i][j-1][k]==1)
count++;
if(a[i][j+1][k]==1)
count++;
if(a[i+1][j-1][k]==1)
count++;
if(a[i+1][j][k]==1)
count++;
if(a[i+1][j+1][k]==1)
count++;
if(count<=1||count>=4)
a[i][j][k-1]=0;
if(count==2)
a[i][j][k-1]=a[i][j][k];
if(count==3)
a[i][j][k-1]=1;
}
}
}
}
void one(int g){
update(g);
show(g+1);
cout<<"鼠标左键点击可更改,按 Enter 开始运行,按 Ctrl 运行单步\n";
}
void set(int g){
while(KEY_DOWN(VK_RETURN));
int i=N,j=N;
while(!KEY_DOWN(VK_RETURN)){
if(KEY_DOWN(VK_CONTROL)){
one(g);
g++;
while(KEY_DOWN(VK_CONTROL));
}
if(KEY_DOWN(VK_LBUTTON)){
HWND h=GetForegroundWindow();
CONSOLE_FONT_INFO consoleCurrentFont;
POINT p;
GetCursorPos(&p);
ScreenToClient(h,&p);
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &consoleCurrentFont);
int x=p.x/=consoleCurrentFont.dwFontSize.X;
int y=p.y/=consoleCurrentFont.dwFontSize.Y;
if((y-2)>=0&&(y-2)<N&&((x-2)/2+1)>=0&&((x-2)/2+1)<N)
a[y-2][(x-2)/2+1][g%2]=!a[y-2][(x-2)/2+1][g%2];
while(KEY_DOWN(VK_LBUTTON));
}
show(g);
cout<<"鼠标左键点击可更改,按 Enter 开始运行,按 Ctrl 运行单步\n";
}
while(KEY_DOWN(VK_RETURN));
}
int main(){
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(hStdin, mode);
std::ios::sync_with_stdio(false);
cout<<"输入大小(超过 100 屏幕刷新效果可能不佳):";
cin>>N;
system("cls");
int i=N,j=N,k,count,n,t;
srand(time(NULL));
cout<<"请选择模板:\n";
cout<<"输入 1:空\n";
cout<<"输入 2:稀疏随机\n";
cout<<"输入 3:中等随机\n";
cout<<"输入 4:密集随机\n";
cout<<"请输入:";
cin>>t;
system("cls");
if(t==1){
set(0);
}else if(t==2){
for(int i=1;i<=N*N/8;i++)
a[rand()%N][rand()%N][0]=1;
}else if(t==3){
for(int i=1;i<=N*N/4;i++)
a[rand()%N][rand()%N][0]=1;
}else if(t==4){
for(int i=1;i<=N*N/1.5;i++)
a[rand()%N][rand()%N][0]=1;
}else{
cout<<"错误!退出。";
exit(0);
}
i=0;
while(1){
show(i);
cout<<"鼠标左键点击可更改,按 Enter 可停止运行 \n";
update(i);
i++;
if(KEY_DOWN(VK_LBUTTON)){
HWND h=GetForegroundWindow();
CONSOLE_FONT_INFO consoleCurrentFont;
POINT p;
GetCursorPos(&p);
ScreenToClient(h,&p);
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &consoleCurrentFont);
int x=p.x/=consoleCurrentFont.dwFontSize.X;
int y=p.y/=consoleCurrentFont.dwFontSize.Y;
if((y-2)>=0&&(y-2)<N&&((x-2)/2+1)>=0&&((x-2)/2+1)<N)
a[y-2][(x-2)/2+1][i%2]=!a[y-2][(x-2)/2+1][i%2];
}
if(KEY_DOWN(VK_RETURN)){
i--;
set(i);
}
}
return 0;
}
|