看睡前消息的时候了解到了约翰·何顿·康威(John Horton Conway)这位伟大的数学家,同时也对他创造的生命游戏(Life of Game)很感兴趣,于是自己闲来无事实现了一下。
游戏规则:
- 当前细胞为死亡状态时,当周围有3个存活细胞时,则迭代后该细胞变成存活状态
- 当前细胞为存活状态时,当周围的邻居细胞低于两个存活时,该细胞变成死亡状态
- 当前细胞为存活状态时,当周围有两个或3个存活细胞时,该细胞保持原样
- 当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态
在Linux下编写(没在windows上写是因为感觉打印地图的时候光标闪烁的太严重)。代码如下:
#include <unistd.h>
#include <iostream>
#include <vector>
using namespace std;
const int G_WORLD_X = 30;
const int G_WORLD_Y = 30;
int g_cells_num = 0;
struct Cell;
vector<Cell> g_cells_set;
vector<vector<int>> g_world;
vector<vector<int>> g_cnt;
void InitWorld();
void CountAliveNum();
void Update();
void Draw();
void GetInput();
void Run();
struct Cell {
Cell() {
x = 0;
y = 0;
}
Cell(int dx, int dy) {
x = dx;
y = dy;
}
int x;
int y;
};
void InitWorld() {
g_world = vector<vector<int>> (G_WORLD_X, vector<int> (G_WORLD_Y, 0))