睡觉前刷视频了解到了生命游戏这个东西,今早起来就花了一些时间将游戏写了出来,代码没有多长思路也很简单,规则就是:
(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哦,但不影响大局。