生命游戏

您所在的位置:网站首页 真爱是放手句子 生命游戏

生命游戏

2024-02-17 11:20| 来源: 网络整理| 查看: 265

生命游戏也算比较古老的游戏,但是其中涉及到的编程思维还是很值得借鉴的,尤其是用来动画演示具体条件下的某群体动态变化,也可以比较粗略的描绘群体中疫情的传播与蔓延 游戏的初始化

生命游戏里,每个小格子里生命如果存活值为1,如果死亡值为0,可以通过所有元素的生命状态输出得到相应的图案

生命游戏初始化

#include #include #include #include #include #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 cells[i][j] = rand() % 2; //随机初始化 } } } void show() { gotoxy(0, 0); HideCursor(); int i, j; for(i = 0; i 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 #include #include #include #include #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 cells[i][j] = rand() % 2; //随机初始化 // cells[i][j] = 1; } } } void show() { gotoxy(0, 0); HideCursor(); int i, j; for(i = 0; i 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 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 cells[i][j] = New[i][j]; } } } void updateWithInput() { } int main() { startup(); while(1) { show(); updateWithoutInput(); updateWithInput(); } return 0; } 在细胞都在的情况下

我们会发现每一帧的变化都是对称的,每一帧意思就是每一轮

初始全是活细胞的情况

#include #include #include #include #include #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 // cells[i][j] = rand() % 2; //随机初始化 cells[i][j] = 1; } } } void show() { gotoxy(0, 0); HideCursor(); int i, j; for(i = 0; i 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 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 cells[i][j] = New[i][j]; } } } void updateWithInput() { } int main() { startup(); while(1) { show(); updateWithoutInput(); updateWithInput(); } return 0; } 如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持,明天我们不见不散!!!


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3