#include<graphics.h> #define cube_size 20 //设置单个网格的大小 #define side 80 //设置一共有多少个网格 bool is_run = true; bool is_pause = false; bool field[side + 1][side + 1]; bool ass[side + 1][side + 1]; int d_x[3] = { -1,0,1 }; int d_y[3] = { -1,0,1 }; bool update(int x, int y) { int sum = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { int xt = x + d_x[i], yt = y + d_y[j]; if (field[xt][yt]) sum += 1; } if (field[x][y]) { if (sum < 3 || sum>4) return false; else return true; } else { if (sum == 3 || sum == 4) return true; else return false; } } int main() { initgraph(cube_size * side, cube_size * side); setbkcolor(BLACK); cleardevice(); setfillcolor(GREEN); setlinecolor(WHITE); for (int i = 0; i < side; i++) { line(i * cube_size, 0, i * cube_size, side * cube_size); line(0, i * cube_size, side * cube_size, i * cube_size);; } while (is_run) { Sleep(500); // 画图 for (int i = 0; i < side; i++) for (int j = 0; j < side; j++) { if (field[i][j]) fillrectangle(i * cube_size, j * cube_size, (i + 1) * cube_size, (j + 1) * cube_size); else clearrectangle(i * cube_size + 1, j * cube_size + 1, (i + 1) * cube_size - 1, (j + 1) * cube_size - 1); } //鼠标操控游戏 MOUSEMSG m; MOUSEMSG* pm = &m; while (PeekMouseMsg(pm, 1)) { if (m.mkRButton) is_pause = true; } while (is_pause)//鼠标右键暂停 { m = GetMouseMsg(); if (m.mkLButton) { int x = m.x / cube_size, y = m.y / cube_size; if (field[x][y]) { field[x][y] = false; clearrectangle(x * cube_size + 1, y * cube_size + 1, (x + 1) * cube_size - 1, (y + 1) * cube_size - 1); } else { field[x][y] = true; fillrectangle(x * cube_size, y * cube_size, (x + 1) * cube_size, (y + 1) * cube_size); } } else if (m.mkRButton)//ctrl键加鼠标右键初始化网格 { if (m.mkCtrl) { for (int i = 0; i < side; i++) for (int j = 0; j < side; j++) { field[i][j] = false; fillrectangle(i * cube_size, j * cube_size, (i + 1) * cube_size, (j + 1) * cube_size); } } else is_pause = false; } } for (int i = 0; i < side; i++) for (int j = 0; j < side; j++) ass[i][j] = update(i, j); for (int i = 0; i < side; i++) for (int j = 0; j < side; j++) field[i][j] = ass[i][j]; } return 0; }请对上述代码添加以下功能,定义类Automaton对仿真建模,并且可以暂停和恢复仿真
时间: 2024-03-23 22:37:18 浏览: 128
C++yuandaima.rar_c<graphics.h>_dos.h_graphics.h_stdlib.h
好的,我会为您添加这个功能。首先,我们可以定义一个Automaton类来对仿真建模,然后在主函数中创建一个Automaton对象,用它来更新field数组,最后在绘图时根据field数组绘制网格。同时,我们可以添加一个键盘事件监听器来实现暂停和恢复仿真的功能。
下面是修改后的代码:
```c++
#include <graphics.h>
#define cube_size 20 // 设置单个网格的大小
#define side 80 // 设置一共有多少个网格
class Automaton {
public:
Automaton() {
for (int i = 0; i <= side; i++)
for (int j = 0; j <= side; j++)
field[i][j] = false;
}
bool update(int x, int y) {
int sum = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
int xt = x + d_x[i], yt = y + d_y[j];
if (field[xt][yt])
sum += 1;
}
if (field[x][y]) {
if (sum < 3 || sum > 4)
return false;
else
return true;
} else {
if (sum == 3 || sum == 4)
return true;
else
return false;
}
}
void pause() {
is_pause = true;
while (is_pause) {
MOUSEMSG m;
while (PeekMouseMsg(&m, 1)) {
if (m.mkRButton)
is_pause = false;
}
if (GetAsyncKeyState(VK_SPACE) & 0x8000)
is_pause = false;
}
}
void toggle(int x, int y) {
if (field[x][y]) {
field[x][y] = false;
clearrectangle(x * cube_size + 1, y * cube_size + 1,
(x + 1) * cube_size - 1, (y + 1) * cube_size - 1);
} else {
field[x][y] = true;
fillrectangle(x * cube_size, y * cube_size, (x + 1) * cube_size,
(y + 1) * cube_size);
}
}
void reset() {
for (int i = 0; i <= side; i++)
for (int j = 0; j <= side; j++) {
field[i][j] = false;
clearrectangle(i * cube_size + 1, j * cube_size + 1,
(i + 1) * cube_size - 1, (j + 1) * cube_size - 1);
}
}
void update_field() {
for (int i = 0; i <= side; i++)
for (int j = 0; j <= side; j++)
ass[i][j] = update(i, j);
for (int i = 0; i <= side; i++)
for (int j = 0; j <= side; j++)
field[i][j] = ass[i][j];
}
bool is_paused() { return is_pause; }
bool is_running() { return is_run; }
bool field[side + 1][side + 1];
private:
bool ass[side + 1][side + 1];
int d_x[3] = {-1, 0, 1};
int d_y[3] = {-1, 0, 1};
bool is_pause = false;
};
int main() {
Automaton automaton;
initgraph(cube_size * side, cube_size * side);
setbkcolor(BLACK);
cleardevice();
setfillcolor(GREEN);
setlinecolor(WHITE);
for (int i = 0; i <= side; i++) {
line(i * cube_size, 0, i * cube_size, side * cube_size);
line(0, i * cube_size, side * cube_size, i * cube_size);
}
while (automaton.is_running()) {
Sleep(500);
automaton.update_field();
for (int i = 0; i <= side; i++)
for (int j = 0; j <= side; j++) {
if (automaton.field[i][j])
fillrectangle(i * cube_size, j * cube_size,
(i + 1) * cube_size, (j + 1) * cube_size);
else
clearrectangle(i * cube_size + 1, j * cube_size + 1,
(i + 1) * cube_size - 1, (j + 1) * cube_size - 1);
}
MOUSEMSG m;
while (PeekMouseMsg(&m, 1)) {
if (m.mkRButton)
automaton.pause();
}
if (GetAsyncKeyState(VK_SPACE) & 0x8000)
automaton.pause();
if (GetAsyncKeyState(VK_CONTROL) & 0x8000 &&
GetAsyncKeyState(VK_RBUTTON) & 0x8000) {
automaton.reset();
} else {
m = GetMouseMsg();
if (m.mkLButton)
automaton.toggle(m.x / cube_size, m.y / cube_size);
}
}
closegraph();
return 0;
}
```
现在您可以使用空格键暂停仿真,使用鼠标右键恢复仿真并暂停,使用Ctrl键加鼠标右键来重置所有网格。同时,可以通过创建一个Automaton对象来管理仿真。
阅读全文