#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 15:37:18 浏览: 129
C++yuandaima.rar_c<graphics.h>_dos.h_graphics.h_stdlib.h
以下是对代码的修改,添加了Automaton类来进行仿真建模,并且添加了暂停和恢复仿真的功能:
```cpp
#include <graphics.h>
#include <iostream>
#include <conio.h>
#include <windows.h>
#define cube_size 20 //设置单个网格的大小
#define side 80 //设置一共有多少个网格
class Automaton {
public:
Automaton();
void update();
void draw();
void pause();
void resume();
void reset();
bool is_running();
private:
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 };
};
Automaton::Automaton() {
for (int i = 0; i < side; i++)
for (int j = 0; j < side; j++)
field[i][j] = false;
}
void Automaton::update() {
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];
}
void Automaton::draw() {
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);
}
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);
}
}
void Automaton::pause() {
while (true) {
if (_kbhit()) {
if (_getch() == VK_SPACE) {
break;
}
}
}
}
void Automaton::resume() {
cleardevice();
}
void Automaton::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);
}
}
bool Automaton::is_running() {
MOUSEMSG m;
while (PeekMouseMsg(&m, 1)) {
if (m.mkRButton) {
pause();
}
if (m.mkCtrl && m.mkRButton) {
reset();
}
}
return true;
}
bool Automaton::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();
Automaton automaton;
while (true) {
automaton.draw();
automaton.update();
if (!automaton.is_running())
break;
automaton.resume();
}
closegraph();
return 0;
}
```
注意:在添加类之后,原来的代码逻辑需要进行修改,主程序的循环应该改成调用类的相关方法,同时需要将画图、鼠标操控等功能都放到类里面实现。
阅读全文