其他分享
首页 > 其他分享> > 生命游戏代码V1.2

生命游戏代码V1.2

作者:互联网

这是上次代码的更新

V1.2:更新了随机生成机制。

#include<iostream>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include <cwchar>
using namespace std;
#define SLEEP 20
int N;
bool a[1010][1010][5];
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;
	}
	cout<<"  ";
	for(i=0;i<N;i++)
		cout<<"—";
	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){
	int i=N,j=N; 
	while(i!=-1&&j!=-1){
		a[i][j][0]=!a[i][j][0];
		show(g);
		cout<<endl<<"目前状态如上,需调整请输入坐标\n";
		cout<<"横坐标(x)(横纵坐标都输入 -1 结束):";
		cin>>i;
		cout<<"纵坐标(y)(横纵坐标都输入 -1 结束):";
		cin>>j;
		system("cls"); 
	}
}
int main(){
	std::ios::sync_with_stdio(false);
	cout<<"输入大小:";
	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;
}

标签:count,游戏,++,代码,else,int,V1.2,&&,include
来源: https://blog.csdn.net/weixin_44550335/article/details/120623641