用c++编写康威生命游戏,要求定义class Cell,可以保存到文件或者加载文件,并且用户可以在运行时更改单元格的状态。模拟可以回滚(恢复之前状态)

时间: 2024-03-10 10:45:41 浏览: 23
好的,我可以为您提供一个简单的康威生命游戏的C++实现,以下是代码: ```cpp #include <iostream> #include <fstream> #include <vector> class Cell { public: Cell(bool alive) : alive(alive), prev(alive) {} bool isAlive() const { return alive; } void setAlive(bool state) { alive = state; } void savePrevState() { prev = alive; } void restorePrevState() { alive = prev; } private: bool alive; bool prev; }; class GameOfLife { public: GameOfLife(size_t width, size_t height) : width(width), height(height) { board.resize(width * height, Cell(false)); } void update() { for (size_t i = 0; i < width; ++i) { for (size_t j = 0; j < height; ++j) { int count = countNeighbors(i, j); if (count == 3) { setCellState(i, j, true); } else if (count != 2) { setCellState(i, j, false); } } } } void setCellState(size_t x, size_t y, bool state) { board[getIndex(x, y)].setAlive(state); } bool getCellState(size_t x, size_t y) const { return board[getIndex(x, y)].isAlive(); } void saveToFile(const std::string& filename) { std::ofstream file(filename); if (!file.is_open()) { std::cerr << "Error: cannot open file " << filename << std::endl; return; } file << width << ' ' << height << std::endl; for (size_t j = 0; j < height; ++j) { for (size_t i = 0; i < width; ++i) { file << (getCellState(i, j) ? '1' : '0'); } file << std::endl; } file.close(); } void loadFromFile(const std::string& filename) { std::ifstream file(filename); if (!file.is_open()) { std::cerr << "Error: cannot open file " << filename << std::endl; return; } size_t newWidth, newHeight; file >> newWidth >> newHeight; board.clear(); board.resize(newWidth * newHeight, Cell(false)); width = newWidth; height = newHeight; for (size_t j = 0; j < height; ++j) { std::string line; file >> line; for (size_t i = 0; i < width; ++i) { setCellState(i, j, line[i] == '1'); } } file.close(); } void rollback() { for (size_t i = 0; i < width; ++i) { for (size_t j = 0; j < height; ++j) { board[getIndex(i, j)].restorePrevState(); } } } private: int countNeighbors(size_t x, size_t y) const { int count = 0; for (int i = -1; i <= 1; ++i) { for (int j = -1; j <= 1; ++j) { if (i == 0 && j == 0) continue; int nx = x + i; int ny = y + j; if (nx < 0 || nx >= width || ny < 0 || ny >= height) continue; if (getCellState(nx, ny)) count++; } } return count; } size_t getIndex(size_t x, size_t y) const { return y * width + x; } std::vector<Cell> board; size_t width; size_t height; }; int main() { GameOfLife game(10, 10); game.setCellState(3, 3, true); game.setCellState(4, 3, true); game.setCellState(5, 3, true); game.setCellState(4, 4, true); game.setCellState(4, 5, true); game.saveToFile("initial_state.txt"); game.update(); game.rollback(); game.loadFromFile("initial_state.txt"); for (size_t j = 0; j < 10; ++j) { for (size_t i = 0; i < 10; ++i) { std::cout << (game.getCellState(i, j) ? '*' : ' '); } std::cout << std::endl; } return 0; } ``` 这个实现定义了一个`Cell`类来表示一个单元格,然后定义了`GameOfLife`类来表示整个生命游戏的状态。`GameOfLife`类的构造函数接受宽度和高度参数,然后初始化一个二维的单元格数组,并将其中的每个单元格初始化为死亡状态。 `GameOfLife`类提供了一些方法来操作单元格数组。例如,`setCellState`和`getCellState`方法用于设置和获取单元格的状态。`update`方法用于更新整个游戏状态。`saveToFile`和`loadFromFile`方法用于将游戏状态保存到文件或从文件中加载。`rollback`方法用于撤销上一次`update`操作。 在这个实现中,我们使用了一个简单的算法来计算每个单元格周围的邻居数量,然后根据规则来更新单元格的状态。在每次更新之前,我们使用`savePrevState`方法保存每个单元格的前一个状态,以便在撤销操作时恢复状态。 在`main`函数中,我们首先创建一个`GameOfLife`对象,并设置一些单元格的状态。然后,我们将当前游戏状态保存到文件`initial_state.txt`中。接下来,我们执行一次更新操作,然后立即执行撤销操作。然后,我们从文件中加载初始状态,并打印整个游戏状态。

相关推荐

最新推荐

recommend-type

Python优秀项目 基于Flask+Markdown实现的生成app官方网站源码+部署文档+数据资料.zip

CSDN IT狂飙上传的代码均可运行,功能ok的情况下才上传的,直接替换数据即可使用,小白也能轻松上手 【资源说明】 Python优秀项目 基于Flask+Markdown实现的生成app官方网站源码+部署文档+数据资料.zip 1、代码压缩包内容 代码的项目文件 部署文档文件 2、代码运行版本 python3.7或者3.7以上的版本;若运行有误,根据提示GPT修改;若不会,私信博主(问题描述要详细) 3、运行操作步骤 步骤一:将代码的项目目录使用IDEA打开(IDEA要配置好python环境) 步骤二:根据部署文档或运行提示安装项目所需的库 步骤三:IDEA点击运行,等待程序服务启动完成 4、python资讯 如需要其他python项目的定制服务,可后台私信博主(注明你的项目需求) 4.1 python或人工智能项目辅导 4.2 python或人工智能程序定制 4.3 python科研合作 Django、Flask、Pytorch、Scrapy、PyQt、爬虫、可视化、大数据、推荐系统、人工智能、大模型
recommend-type

用Go编写的Express启发web框架.zip

用Go编写的Express启发web框架
recommend-type

QJ 3173-2003 航天电子电气产品再流焊接技术要求.rar

QJ 3173-2003 航天电子电气产品再流焊接技术要求.rar
recommend-type

电工杯电工杯电工杯电工杯.txt

电工杯电工杯电工杯电工杯电工杯
recommend-type

练习和解决方案从书知道Go泛型.zip

练习和解决方案从书知道Go泛型
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

深入了解MATLAB开根号的最新研究和应用:获取开根号领域的最新动态

![matlab开根号](https://www.mathworks.com/discovery/image-segmentation/_jcr_content/mainParsys3/discoverysubsection_1185333930/mainParsys3/image_copy.adapt.full.medium.jpg/1712813808277.jpg) # 1. MATLAB开根号的理论基础 开根号运算在数学和科学计算中无处不在。在MATLAB中,开根号可以通过多种函数实现,包括`sqrt()`和`nthroot()`。`sqrt()`函数用于计算正实数的平方根,而`nt
recommend-type

react的函数组件的使用

React 的函数组件是一种简单的组件类型,用于定义无状态或者只读组件。 它们通常接受一个 props 对象作为参数并返回一个 React 元素。 函数组件的优点是代码简洁、易于测试和重用,并且它们使 React 应用程序的性能更加出色。 您可以使用函数组件来呈现简单的 UI 组件,例如按钮、菜单、标签或其他部件。 您还可以将它们与 React 中的其他组件类型(如类组件或 Hooks)结合使用,以实现更复杂的 UI 交互和功能。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。