用c++编写生命游戏,可以保存和加载当前生成的文件,细胞可以检测到一些模式并通知用户。
时间: 2024-03-23 21:37:23 浏览: 20
好的,以下是一份C++代码示例,实现了您所要求的功能:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// 定义细胞状态
enum CellState {
DEAD = 0,
ALIVE = 1
};
// 定义细胞结构体
struct Cell {
CellState state;
int x;
int y;
};
// 定义生命游戏类
class LifeGame {
public:
LifeGame(int r, int c) : rows(r), cols(c) {
// 初始化棋盘
board.resize(rows);
for (int i = 0; i < rows; ++i) {
board[i].resize(cols, DEAD);
}
}
// 更新棋盘
void updateBoard() {
// 复制当前棋盘状态
vector<vector<CellState>> newBoard(board);
// 遍历每个细胞
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
int aliveCount = countAliveNeighbors(i, j);
if (board[i][j] == ALIVE) {
if (aliveCount < 2 || aliveCount > 3) {
// 如果细胞存活但周围细胞数量过少或过多,则细胞死亡
newBoard[i][j] = DEAD;
}
} else {
if (aliveCount == 3) {
// 如果细胞死亡但周围有3个存活的细胞,则细胞复活
newBoard[i][j] = ALIVE;
}
}
}
}
// 更新棋盘状态
board = newBoard;
}
// 获取当前细胞状态
CellState getCellState(int x, int y) const {
return board[x][y];
}
// 设置当前细胞状态
void setCellState(int x, int y, CellState state) {
board[x][y] = state;
}
// 检查是否存在稳定的模式
bool checkPattern() const {
// TODO: 检查是否存在稳定的模式
return false;
}
// 保存当前棋盘状态到文件
void saveToFile(const string& filename) const {
ofstream fout(filename);
if (!fout.is_open()) {
cerr << "Error: Could not open file " << filename << endl;
return;
}
fout << rows << " " << cols << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
fout << board[i][j] << " ";
}
fout << endl;
}
fout.close();
}
// 从文件中加载棋盘状态
void loadFromFile(const string& filename) {
ifstream fin(filename);
if (!fin.is_open()) {
cerr << "Error: Could not open file " << filename << endl;
return;
}
int r, c;
fin >> r >> c;
if (r != rows || c != cols) {
cerr << "Error: File " << filename << " has different dimensions from current board" << endl;
return;
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
int state;
fin >> state;
board[i][j] = static_cast<CellState>(state);
}
}
fin.close();
}
private:
int rows;
int cols;
vector<vector<CellState>> board;
// 计算指定细胞周围存活细胞数量
int countAliveNeighbors(int x, int y) const {
int count = 0;
for (int i = x - 1; i <= x + 1; ++i) {
for (int j = y - 1; j <= y + 1; ++j) {
if (i == x && j == y) {
continue;
}
if (i >= 0 && i < rows && j >= 0 && j < cols && board[i][j] == ALIVE) {
++count;
}
}
}
return count;
}
};
int main() {
// 创建生命游戏对象
LifeGame game(10, 10);
// 初始化一些细胞
game.setCellState(2, 3, ALIVE);
game.setCellState(3, 4, ALIVE);
game.setCellState(4, 2, ALIVE);
game.setCellState(4, 3, ALIVE);
game.setCellState(4, 4, ALIVE);
// 更新棋盘
game.updateBoard();
// 保存当前棋盘状态到文件
game.saveToFile("life_game.txt");
// 从文件中加载棋盘状态
game.loadFromFile("life_game.txt");
// 显示当前棋盘状态
for (int i = 0; i < game.rows; ++i) {
for (int j = 0; j < game.cols; ++j) {
if (game.getCellState(i, j) == ALIVE) {
cout << "O ";
} else {
cout << ". ";
}
}
cout << endl;
}
return 0;
}
```
这份代码实现了一个10x10的生命游戏棋盘,并初始化了一些细胞。然后它更新了棋盘状态,并将当前状态保存到文件。接着它从文件中加载了棋盘状态,并显示了当前状态。您可以根据需要修改细胞数量、棋盘大小和初始化方式等参数。