生命游戏代码V3.1

这是V3.0的更新(更新的算少了)

 目前最新版本为V5.0,链接生命游戏代码V5.0_小学生的博客-CSDN博客

这里放上其他版本的链接

生命游戏代码V1.0_博客-CSDN博客

生命游戏代码V1.2_博客-CSDN博客

生命游戏代码V2.0_博客-CSDN博客

生命游戏代码V3.0_博客-CSDN博客

V3.1:更新如下

        1.对代码进行了部分优化

        2.运行时用 Enter 停止运行

已知错误:

        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];
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 = "<<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 set(int g){
	Sleep(100);
	system("cls");
	int i=N,j=N;
	while(!KEY_DOWN(VK_RETURN)) {                      		
		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&&((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 开始运行  \n"; 
	}
	Sleep(100);
}
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)){
			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)<N&&((x-2)/2+1)<N)
				a[y-1][(x-2)/2+1][0]=!a[y-1][(x-2)/2+1][0];
		}
		if(KEY_DOWN(VK_RETURN)){
			i--;
			set(i);
		}
	}
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值