生命游戏也算比较古老的游戏,但是其中涉及到的编程思维还是很值得借鉴的,尤其是用来动画演示具体条件下的某群体动态变化,也可以比较粗略的描绘群体中疫情的传播与蔓延
游戏的初始化
生命游戏里,每个小格子里生命如果存活值为1,如果死亡值为0,可以通过所有元素的生命状态输出得到相应的图案
生命游戏初始化
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define High 25 //画面尺寸
#define Width 50
int cells[High][Width];
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()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{
int i, j;
for(i = 0; i < High; i++) //数据初始化
{
for(j = 0; j < Width; j++)
{
cells[i][j] = rand() % 2; //随机初始化
}
}
}
void show()
{
gotoxy(0, 0);
HideCursor();
int i, j;
for(i = 0; i <= High; i++)
{
for(j = 0; j <= Width; j++)
{
if(cells[i][j] == 1)
printf("*"); //输出细胞
else
printf(" ");
sleep(1);
}
printf("\n");
}
}
void updateWithoutInput()
{
}
void updateWithInput()
{
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
繁衍或死亡
生命游戏的演化规则如下:
1、如果一个细胞周围有3活细胞,则该细胞就活过来(即若该细胞若原本死了,则复活;若该细胞原本就是活的,则保持不变)
2、如果一个细胞周围有两个活细胞,则该细胞的生死状态不变
3、其他情况下,该细胞为死(即该细胞若原本是活的,则死了;若原本是死的,仍保持死)
在细胞随机分布的情况下:
初始状态随机的情况
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define High 25 //画面尺寸
#define Width 50
int cells[High][Width];
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()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{
int i, j;
for(i = 0; i < High; i++) //数据初始化
{
for(j = 0; j < Width; j++)
{
cells[i][j] = rand() % 2; //随机初始化
// cells[i][j] = 1;
}
}
}
void show()
{
gotoxy(0, 0);
HideCursor();
int i, j;
for(i = 0; i <= High; i++)
{
for(j = 0; j <= Width; j++)
{
if(cells[i][j] == 1)
printf("*"); //输出细胞
else
printf(" ");
// sleep(1);
}
sleep(1);
printf("\n");
}
}
void updateWithoutInput()
{
int New[High][Width];
int Neighbnum = 0;
int i, j;
for(i = 0; i < High; i++)
{
for(j = 0; j < Width; j++)
{
Neighbnum = 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(Neighbnum == 3)
New[i][j] = 1;
else if(Neighbnum == 2)
New[i][j] = cells[i][j];
else
New[i][j] = 0;
}
}
for(i = 1; i < High; i++)
{
for(j = 1; j < Width; j++)
{
cells[i][j] = New[i][j];
}
}
}
void updateWithInput()
{
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
在细胞都在的情况下
我们会发现每一帧的变化都是对称的,每一帧意思就是每一轮
初始全是活细胞的情况
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define High 25 //画面尺寸
#define Width 50
int cells[High][Width];
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()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{
int i, j;
for(i = 0; i < High; i++) //数据初始化
{
for(j = 0; j < Width; j++)
{
// cells[i][j] = rand() % 2; //随机初始化
cells[i][j] = 1;
}
}
}
void show()
{
gotoxy(0, 0);
HideCursor();
int i, j;
for(i = 0; i <= High; i++)
{
for(j = 0; j <= Width; j++)
{
if(cells[i][j] == 1)
printf("*"); //输出细胞
else
printf(" ");
// sleep(1);
}
sleep(1);
printf("\n");
}
}
void updateWithoutInput()
{
int New[High][Width];
int Neighbnum = 0;
int i, j;
for(i = 0; i < High; i++)
{
for(j = 0; j < Width; j++)
{
Neighbnum = 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(Neighbnum == 3)
New[i][j] = 1;
else if(Neighbnum == 2)
New[i][j] = cells[i][j];
else
New[i][j] = 0;
}
}
for(i = 1; i < High; i++)
{
for(j = 1; j < Width; j++)
{
cells[i][j] = New[i][j];
}
}
}
void updateWithInput()
{
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}