“生命游戏”c++源代码

/*vs2017*/
#include<iostream>
#include<time.h>
using namespace std;
#define X 20 //地图高度
#define Y 30 //地图宽度
void delay(int ms)
{
	clock_t start = clock();
	while (clock() - start < ms);
}

void Evolution()
{
	int next[X][Y] = {0};
	int Next[X][Y] = { 0 };
	int i, j;
	//初始化数组
	next[2][4] = 1;
	next[3][2] = 1;
	next[3][4] = 1;
	next[4][3] = 1;
	next[4][4] = 1;
	//next[3][4] = 1;
	for (i = 0; i < X; i++)
	{
		for (j = 0; j < Y; j++)
		{
			Next[i][j] = next[i][j];
			if (next[i][j] == 1)
				cout << " ●";
			else
			{
				cout << " ○";
			}
		}
		cout << endl;
	}
	delay(2000);
	system("cls");
	int counter;
	while (1) 
	{
		for (i = 1; i < X-1; i++)
		{
			for (j = 1; j < Y-1; j++)
			{
				counter = 0;//统计周围的细胞数
				if (Next[i - 1][j] == 1)
					counter += 1;
				if (Next[i - 1][j + 1] == 1)
					counter += 1;
				if (Next[i - 1][j - 1] == 1)
					counter += 1;
				if (Next[i + 1][j] == 1)
					counter += 1;
				if (Next[i + 1][j - 1] == 1)
					counter += 1;
				if (Next[i + 1][j + 1] == 1)
					counter += 1;
				if (Next[i][j + 1] == 1)
					counter += 1;
				if (Next[i][j - 1] == 1)
					counter += 1;


				if (next[i][j] == 1)
				{
					if (counter < 2)//寂寞死
					{
						next[i][j] = 0;
					}
					else if (counter < 4)//存活
					{ }
					else//挤死
					{
						next[i][j] = 0;
					}
				}
				else if (next[i][j] == 0)
				{
					if (counter > 2)//新生
					{
						next[i][j] = 1;
					}
				}

			}
		}
		for (i = 0; i < X; i++)//更新
		{
			for (j = 0; j < Y; j++)
			{
				Next[i][j] = next[i][j];
			}
		}
		/*********打印*********/
		for (i = 0; i < X; i++)
		{
			for (j = 0; j < Y; j++)
			{
				if (next[i][j] == 1)
					cout << " ●";
				else
				{
					cout << " ○";
				}
			}
			cout << endl;
		}
		delay(100);
		system("cls");
	}
}
int main()
{
	Evolution();
	return 0;
}
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值