生命游戏(c++/杂谈)

编程入门 行业动态 更新时间:2024-10-25 14:34:27

生命游戏(c++/<a href=https://www.elefans.com/category/jswz/34/1767767.html style=杂谈)"/>

生命游戏(c++/杂谈)

睡觉前刷视频了解到了生命游戏这个东西,今早起来就花了一些时间将游戏写了出来,代码没有多长思路也很简单,规则就是:
(1)当前细胞为死亡状态时,当周围有3个存活细胞时,则迭代后该细胞变成存活状态(模拟繁殖);若原先为生,则保持不变。
(2)当前细胞为存活状态时,当周围的邻居细胞低于两个(不包含两个)存活时,该细胞变成死亡状态(模拟生命数量稀少)。
(3)当前细胞为存活状态时,当周围有两个或3个存活细胞时,该细胞保持原样。
(4)当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态(模拟生命数量过多)。

#include<iostream>
#include<Windows.h>
#include<cstring>
#include<graphics.h>
#include<easyx.h>
using namespace std;
#define WIDTH 1600
#define HEIGHT 1200
int arr[WIDTH / 20 + 1][HEIGHT / 20 + 1];
int star[18][18] = {       //图形脉冲星0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,1,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};
int cnt[WIDTH / 20 + 1][HEIGHT / 20 + 1];
int dir[8][2] = {-1,0,1,0,0,-1,0,1,-1,-1,-1,1,1,-1,1,1
};
void creat() {for (int i = 0; i <= 7; i++)for (int j = 0; j <= 17; j++)star[16 - i][j] = star[i][j];for (int i = 0; i <= 17; i++)for (int j = 0; j <= 17; j++)arr[i][j] = star[i][j];
}
void compute() {for (int i = 0; i < WIDTH / 20 + 1; i++) {for (int j = 0; j < HEIGHT / 20 + 1; j++) {int sum = 0;for (int k = 0; k < 8; k++) {int xx = i + dir[k][0];int yy = j + dir[k][1];if (xx >= 0 && xx <= WIDTH / 20 && yy >= 0 && yy <= HEIGHT / 20) {sum += arr[xx][yy];}}cnt[i][j] = sum;}}
}
void draw() {BeginBatchDraw();for (int i = 0; i < WIDTH / 20 + 1; i++) {for (int j = 0; j < HEIGHT / 20 + 1; j++) {if (arr[i][j] == 1) {solidrectangle(i * 20 + 1, j * 20 + 1, i * 20 + 19, j * 20 + 19);}else {clearrectangle(i * 20 + 1, j * 20 + 1, i * 20 + 19, j * 20 + 19);}}}EndBatchDraw();
}
int main() {initgraph(WIDTH + 1, HEIGHT + 1);setlinecolor(0xA7978);for (int i = 0; i <= WIDTH; i += 20) {line(i, 0, i, HEIGHT);}for (int i = 0; i <= HEIGHT; i += 20) {line(0, i, WIDTH, i);}loop:memset(arr, 0, sizeof(arr));memset(cnt, 0, sizeof(cnt));draw();MOUSEMSG p;int left, top;//绘制初始化场景//creat();//draw();//去掉注释就是初始化 脉冲星图案while (1) {PeekMouseMsg(&p, 1);if ((GetAsyncKeyState(VK_LBUTTON) & 0x8000)) {left = 20 * (p.x / 20), top = 20 * (p.y / 20);arr[p.x / 20][p.y / 20] = 1;solidrectangle(left + 1, top + 1, left + 19, top + 19);}else if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) {left = 20 * (p.x / 20), top = 20 * (p.y / 20);arr[p.x / 20][p.y / 20] = 0;clearrectangle(left + 1, top + 1, left + 19, top + 19);}compute();if (GetAsyncKeyState(VK_SPACE) & 0x8000) {//空格开始执行break;}}while (1) {for (int i = 0; i < WIDTH / 20 + 1; i++) {for (int j = 0; j < HEIGHT / 20 + 1; j++) {if (arr[i][j] == 0 && cnt[i][j] == 3) arr[i][j] = 1;else if (arr[i][j] == 1 && cnt[i][j] < 2) arr[i][j] = 0;else if (arr[i][j] == 1 && cnt[i][j] > 3) arr[i][j] = 0;}}draw();compute();if (GetAsyncKeyState(VK_SHIFT) & 0x8000) {//shift暂停while (1)if (GetAsyncKeyState(VK_CONTROL) & 0x8000) break;//ctrl继续}if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {goto loop;                            //鼠标左键重新绘制}}return 0;
}

代码瞎写的,很乱,图一乐。画图用的是easyx图形库很简单。
有些小bug哦,但不影响大局。

更多推荐

生命游戏(c++/杂谈)

本文发布于:2024-03-10 04:07:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1726969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:杂谈   生命   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!