⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 生命遊戲演算法實做 判斷鄰居數量決定下一時間是否出生或死亡 利用到交替陣列和時間差距演算寫法
💻
字号:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
	int f[2][22][22],i=0,j=0,k=0,l=0,c1=0,p=0,q=0,sum=1;
	bool bo=true;
	char tmp='\n';
	srand(time(NULL));//睹计砞﹚
	for(i=0;i<=21;i++)
	{
		for(j=0;j<=1;j++){
			f[j][0][i]=0;f[j][i][0]=0;f[j][21][i]=0;f[j][i][21]=0;}
	}
	for(i=1;i<=20;i++)
	{
		for(j=1;j<=20;j++)
		{
			f[1][i][j]=int(rand()%2);
			f[0][i][j]=int(rand()%2);
			if(f[0][i][j]==1)
				printf("* ");
			else
				printf("  ");
		}
		printf("\n");
	}
	printf("If you want to continue,press \"Enter\",or press\"q\"\n%d\n",sum);
	scanf("%c",&tmp);
	system("cls");
	while(tmp!='q')
	{
		sum+=1;
		system("cls");
		if(bo==true)
		{
			p=0;
			q=1;
			bo=false;
		}
		else
		{
			p=1;
			q=0;
			bo=true;
		}
		for(i=1;i<=20;i++)
		{
			for(j=1;j<=20;j++)
			{
				for(k=-1;k<=1;k++)
				{
					for(l=-1;l<=1;l++)
					{
						if(f[p][i+k][j+l]==1)
							c1+=1;
					}
				}
				if(f[p][i][j]==1)
					c1--;
				switch(c1){
				case 3:
					f[q][i][j]=1;
					break;
				case 2:
					if(f[p][i][j]==0)
						f[q][i][j]=0;
					else
						f[q][i][j]=1;
					break;
				default:
					f[q][i][j]=0;
				}
				if(f[q][i][j]==1)
					printf("* ");
				else
					printf("  ");
				c1=0;
			}
			printf("\n");
		}
		printf("If you want to continue,press \"Enter\",or press\"q\"\n%d\n",sum);
		scanf("%c",&tmp);
	}
	printf("Created by Comaniac. 2007/07/20\n");
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -