生命游戏代码V2.0

这是V1.2的更新

V2.0:更新了初始化为空的修改机制为鼠标左右键单击,游戏体验提升了。

#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];
POINT p;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HWND h=GetForegroundWindow();
CONSOLE_FONT_INFO consoleCurrentFont;
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){
	HideCursor(); 
	gotoxy(0,0);
	int i,j;
	cout<<"           Second = "<<g<<"                                "<<endl;
	cout<<"  ";
	for(i=0;i<N;i++)
		cout<<"—"; 
	cout<<endl;
	g%=2;
	for(i=0;i<N;i++){
		cout<<"| ";
		for(j=0;j<N;j++){
			if(a[i][j][g]==1)
				cout<<"█ ";
			else if(a[i][j][g]==0)
				cout<<"  ";
		}
		cout<<" |"<<endl;
	}
	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 set(int g){
	g%=2; 
	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;
			a[y-3][(x-2)/2][0]=!a[y-3][(x-2)/2][0];
			while(KEY_DOWN(VK_LBUTTON));
		}
		show(0);
		cout<<"鼠标左键点击可更改,右键点击开始运行\n"; 
	}
	system("cls");
}
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/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);
		update(i);
		i++;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44550335/article/details/120629780