生命遊戲也算比較古老的遊戲,但是其中涉及到的編程思維還是很值得借鑑的,尤其是用來動畫演示具體條件下的某羣體動態變化,也可以比較粗略的描繪羣體中疫情的傳播與蔓延
遊戲的初始化
生命遊戲裏,每個小格子裏生命如果存活值爲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;
}