用c++写生命游戏,用户可以在运行时更改单元格的状态,模拟可以被还原(还原到上一代的状态)
时间: 2024-03-18 10:39:36 浏览: 21
好的,这是一个有趣的问题!我可以给您提供一个基本的 C++ 实现,供您参考:
```c++
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// 定义生命游戏的单元格类型
enum CellType {
DEAD, // 死细胞
ALIVE // 活细胞
};
// 定义生命游戏的单元格类
class Cell {
public:
CellType type; // 单元格类型
bool changed; // 是否被用户修改过
Cell(CellType t = DEAD) : type(t), changed(false) {}
};
// 定义生命游戏的世界类
class World {
public:
int rows, cols; // 世界的行数和列数
vector<vector<Cell>> cells; // 存储世界中所有单元格的二维向量
World(int r, int c) : rows(r), cols(c) {
cells.resize(rows);
for (int i = 0; i < rows; i++) {
cells[i].resize(cols);
}
reset(); // 初始化世界中所有单元格为死细胞
}
// 重置世界中所有单元格为死细胞
void reset() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cells[i][j].type = DEAD;
cells[i][j].changed = false;
}
}
}
// 随机生成世界中的单元格状态
void randomize() {
srand(time(NULL));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int r = rand() % 2;
cells[i][j].type = (r == 0) ? DEAD : ALIVE;
}
}
}
// 更新世界中所有单元格的状态
void update() {
vector<vector<Cell>> new_cells(rows, vector<Cell>(cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int neighbors = count_neighbors(i, j); // 统计相邻细胞的数量
if (cells[i][j].type == ALIVE) {
if (neighbors < 2 || neighbors > 3) {
new_cells[i][j].type = DEAD; // 如果周围细胞不足 2 个或超过 3 个,该细胞死亡
} else {
new_cells[i][j].type = ALIVE; // 否则该细胞生存
}
} else {
if (neighbors == 3) {
new_cells[i][j].type = ALIVE; // 如果周围细胞恰好有 3 个,该细胞复活
} else {
new_cells[i][j].type = DEAD; // 否则该细胞仍然是死亡状态
}
}
}
}
cells = new_cells; // 更新世界中的单元格状态
}
// 统计指定单元格周围的活细胞数量
int count_neighbors(int x, int y) {
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int nx = x + i;
int ny = y + j;
if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && !(i == 0 && j == 0)) {
if (cells[nx][ny].type == ALIVE) {
count++;
}
}
}
}
return count;
}
// 打印当前世界中所有单元格的状态
void print() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << ((cells[i][j].type == ALIVE) ? "*" : ".");
}
cout << endl;
}
}
// 还原到上一代的状态
void undo() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (cells[i][j].changed) {
cells[i][j].type = (cells[i][j].type == ALIVE) ? DEAD : ALIVE;
cells[i][j].changed = false;
}
}
}
}
};
int main() {
int rows, cols;
cout << "请输入世界的行数和列数:";
cin >> rows >> cols;
World world(rows, cols);
world.randomize(); // 随机生成世界中的单元格状态
world.print(); // 打印初始状态
while (true) {
cout << "请输入指令(u:还原到上一代,q:退出):";
string cmd;
cin >> cmd;
if (cmd == "u") { // 还原到上一代的状态
world.undo();
} else if (cmd == "q") { // 退出程序
break;
} else { // 修改单元格状态
int x, y;
cout << "请输入单元格的坐标(行 列):";
cin >> x >> y;
if (x >= 0 && x < rows && y >= 0 && y < cols) {
world.cells[x][y].type = (world.cells[x][y].type == ALIVE) ? DEAD : ALIVE;
world.cells[x][y].changed = true;
}
}
world.update(); // 更新世界中所有单元格的状态
world.print(); // 打印新状态
}
return 0;
}
```
上面的代码实现了一个基本的生命游戏,用户可以在运行时修改单元格的状态,模拟也可以被还原(还原到上一代的状态)。在代码中,我们使用了 `vector` 来存储世界中所有单元格的状态,同时还实现了 `reset()`、`randomize()`、`update()`、`count_neighbors()`、`print()`、`undo()` 等方法来进行世界的初始化、更新、统计、打印和还原等操作。希望这个实现对您有所帮助!