这是V2.0的更新
这里放上其他版本的链接
生命游戏代码V1.0_黄景行的博客-CSDN博客
生命游戏代码V1.2_黄景行的博客-CSDN博客
生命游戏代码V2.0_黄景行的博客-CSDN博客
V3.0:更新如下
1.更改了场景绘制方法
2.对代码进行了部分优化
3.运行时可修改界面
已知错误:
1.调整控制台字体大小后坐标可能错位
2.点击窗口外程序可能崩溃
3.鼠标点击可能无效
↓代码如下↓
#include#include #include #include #include #include using namespace std; #define SLEEP 50 #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0) int N; bool a[1010][1010][5]; POINT p; HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); HWND h=GetForegroundWindow(); CONSOLE_FONT_INFO consoleCurrentFont; 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 = "< =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 =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 set(int g){ Sleep(300); system("cls"); int i=N,j=N; while(!KEY_DOWN(VK_RBUTTON)) { if(KEY_DOWN(VK_LBUTTON)){ POINT p; GetCursorPos(&p); ScreenToClient(h,&p); GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont); int x=p.x/=consoleCurrentFont.dwFontSize.X; int y=p.y/=consoleCurrentFont.dwFontSize.Y; if((y-2) >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/4;i++) a[rand()%N][rand()%N][0]=1; }else if(t==3){ for(int i=1;i<=N*N/2;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<<"鼠标左键点击可更改,右键点击可停止运行n"; update(i); i++; if(KEY_DOWN(VK_LBUTTON)){ POINT p; GetCursorPos(&p); ScreenToClient(h,&p); GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont); int x=p.x/=consoleCurrentFont.dwFontSize.X; int y=p.y/=consoleCurrentFont.dwFontSize.Y; if((y-1)



