康威生命游戏(Conway's|康威生命游戏(Conway's Game of Life)的一种实现
The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.
http://en.wikipedia.org/wiki/Conway's_Game_of_Life
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead.
Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent.
At each step in time, the following transitions occur:
1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
文章图片
//This code was written last year, but some issues fixed recently to comply with Conway's original rule. #include #include #include#include const char *Icon[] = { "",/*□*/ "★" }; //图素class Cell{ private: int currStatus_; int nextStatus_; public: Cell() { currStatus_ = 0; nextStatus_ = ((rand() % 2) && (rand() % 2)); } void showCell() { currStatus_ = nextStatus_; printf("%s", Icon[currStatus_]); } void setAlive() { nextStatus_ = 1; } void setKilled() { nextStatus_ = 0; } int isAlive() { return currStatus_; } }; class Map{ private: Cell *worldMatrix_[19][19]; int dayCount_; int totalAlive_; public: Map() { dayCount_ = 1; totalAlive_ = 0; for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) worldMatrix_[i][j] = new Cell; } } ~Map() { for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) delete worldMatrix_[i][j]; } } void displayMap() { system("cls"); totalAlive_ = 0; for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { worldMatrix_[i][j]->showCell(); totalAlive_ += worldMatrix_[i][j]->isAlive(); } printf("\r\n"); } printf("第%d天\t地图中有%d个细胞\r\n", dayCount_, totalAlive_); } void startIterating() { int count = 0; for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { count = 0; if ((i - 1 >= 0) && (worldMatrix_[i - 1][j]->isAlive()))//i-1 , j count++; if ((i + 1 < 19) && (worldMatrix_[i + 1][j]->isAlive()))//i+1 , j count++; if ((j - 1 >= 0) && (worldMatrix_[i][j - 1]->isAlive()))//i , j-1 count++; if ((j + 1 < 19) && (worldMatrix_[i][j + 1]->isAlive()))//i , j+1 count++; if ((i - 1 >= 0) && (j - 1 >= 0) && (worldMatrix_[i - 1][j - 1]->isAlive()))//i-1 , j-1 count++; if ((i - 1 >= 0) && (j + 1 < 19) && (worldMatrix_[i - 1][j + 1]->isAlive()))//i-1 , j+1 count++; if ((i + 1 < 19) && (j - 1 >= 0) && (worldMatrix_[i + 1][j - 1]->isAlive()))//i+1 , j-1 count++; if ((i + 1 < 19) && (j + 1 < 19) && (worldMatrix_[i + 1][j + 1]->isAlive()))//i+1 , j+1 count++; if (count == 3) { worldMatrix_[i][j]->setAlive(); } else if (count == 2) { if (worldMatrix_[i][j]->isAlive()) { worldMatrix_[i][j]->setAlive(); } else { worldMatrix_[i][j]->setKilled(); } } else { worldMatrix_[i][j]->setKilled(); } } } dayCount_++; } }; int main(void) { SetWindowText(GetForegroundWindow(), "Life Game"); srand([]()->unsigned int { _asm { rdtsc; } }()); Map map; while (true) { Sleep(1000); map.displayMap(); map.startIterating(); } return 0; }
【康威生命游戏(Conway's|康威生命游戏(Conway's Game of Life)的一种实现】转载于:https://www.cnblogs.com/intervention/p/4054094.html
推荐阅读
- 游戏IP(立足于玩家情感的粉丝经济)
- 活着就是生命的全部意义
- 生命过客——第10章|生命过客——第10章 初为人母
- 生命的活力!2019-05-04好事
- 人生游戏--是游戏,还是人生()
- 生命中最迷人的部分轻拿轻放
- 「按键精灵安卓版」关于全分辨率脚本的一些理解(非游戏app)
- (小说)月流水几亿的火爆游戏养成记
- 游戏治愈了我无聊之症
- 日更82/365珍视生命中的每一刻