C++ 生命游戏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/PYLON_GCL/article/details/62248127

依然是跟着知乎上的Dalao学习。地址在这:

https://zhuanlan.zhihu.com/p/24768071



以下代码:


#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
using namespace std;

#define High 25
#define Width 50

int cells[25][50];

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void startup()
{
	int i, j;
	for ( i = 0; i < High; i++)
	{
		for ( j = 0; j < Width; j++)
		{
			cells[i][j] = 1;
		}
	}
}

void show()
{
	gotoxy(0, 0);
	int i, j;
	for ( i = 1; i <= High-1; i++)
	{
		for ( j = 1; j <= Width-1; j++)
		{
			if (cells[i][j]==1)
			{
				cout << "*";
			}
			else
			{
				cout << " ";
			}
		}
		cout << endl;
	}
	Sleep(50);
}

void UpdateWithoutInput()
{
	int newcells[25][50];
	int neibournumber;
	int i, j;
	for ( i = 1; i <= High-1; i++)
	{
		for ( j = 1; j <= Width-1; j++)
		{
			neibournumber = cells[i - 1][j - 1] + cells[i - 1][j] + cells[i - 1][j + 1] + cells[i][j - 1] + cells[i][j + 1] + cells[i + 1][j - 1] + cells[i + 1][j] + cells[i + 1][j + 1];
			if (neibournumber == 3)
			{
				newcells[i][j] = 1;
			}
			else if (neibournumber == 2)
			{
				newcells[i][j] = cells[i][j];
			}
			else
				newcells[i][j] = 0;
		}
	}
	for (i = 1; i <= High - 1; i++)
	{
		for ( j = 1; j <= Width-1 ; j++)
		{
			cells[i][j] = newcells[i][j];
		}
	}
}

void UpdateWithInput()
{

}

int main()
{
	startup();
	while (1)
	{
		show();
		UpdateWithoutInput();
		UpdateWithInput();
	}
	return 0;
}


阅读更多

没有更多推荐了,返回首页