当前位置:   article > 正文

“生命游戏”c++源代码_生命游戏c++语言代码

生命游戏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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/701117
推荐阅读
相关标签
  

闽ICP备14008679号