C++如何实现趣味扫雷游戏
发表于:2024-10-14 作者:千家信息网编辑
千家信息网最后更新 2024年10月14日,这篇"C++如何实现趣味扫雷游戏"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"C++
千家信息网最后更新 2024年10月14日C++如何实现趣味扫雷游戏
这篇"C++如何实现趣味扫雷游戏"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"C++如何实现趣味扫雷游戏"文章吧。
流程设计
1.初始化阵列。
2.输入坐标点。
3.选择:挖掘,标记,取消标记,重启,退出游戏。
如果选了挖掘,判断坐标点是地雷则游戏结束,是数字则显示数字并回到2,是空格则显示周围8个元素值并直到连带的空格显示完了回到2;
如果选了标记,将该点的元素值设为-2并回到2;
如果选了取消标记,初始化该点,回到2;
如果选了重启,则初始化阵列,回到2;
如果选了退出游戏,则exit。
4.挖掘完所有非地雷点后,游戏胜利,选择是否再来一局,是则回到1,否则exit
面向对象设计思想
创建一个bombsweep类,存储几个方法:
calculate:统计以(x,y)为中心周围8个点的地雷数目。
game:模拟游戏过程。
print:打印阵列。
check:检查是否满足胜利条件。
在main函数中,在需要的时候根据bombsweep类创建bs对象,调用bs里面的相关方法。
程序代码
#include#include #include #include using namespace std;int map[12][12]; // ??????????,????????????1int derection[3] = { 0, 1, -1 }; //????????8?????int type;class bombsweep{public: int calculate ( int x, int y ) { int counter = 0; for ( int i = 0; i < 3; i++ ) for ( int j = 0; j < 3; j++ ) if ( map[ x+derection[i]][ y+derection[j] ] == 9 ) counter++; // ???(x,y)?????8??????? return counter; } void game ( int x, int y ) { if ( calculate ( x, y ) == 0 ) { map[x][y] = 0; for ( int i = 0; i < 3; i++ ) { // ???????,????????? for ( int j = 0; j < 3; j++ ) if ( x+derection[i] <= 9 && y+derection[j] <= 9 && x+derection[i] >= 1 && y+derection[j] >= 1 && !( derection[i] == 0 && derection[j] == 0 ) && map[x+derection[i]][y+derection[j]] == -1 ) game( x+derection[i], y+derection[j] ); // ???????????????0,????????! } //????????.??????????? } else map[x][y] = calculate(x,y); } void print (int x,int y) { cout << " |"; for (int i=1; i<10; i++) cout << " " << i; cout << endl; cout << "__|__________________Y" ; cout << endl; for ( int i = 1; i < 10; i++ ) { cout << i << " |"; for ( int j = 1; j < 10; j++ ) { if(map[i][j]==-2) cout <<" B"; else if ( map[i][j] == -1 || map[i][j] == 9 ) cout << " #"; else cout << " "<< map[i][j]; } cout << "\n"; } cout << " X\n"; } bool check () { int counter = 0; for ( int i = 1; i < 10; i++ ) for ( int j = 1; j < 10; j++ ) if ( map[i][j] != -1 ) counter++; if ( counter == 10 ) return true; else return false; }}; int main (){ int i, j, x, y; char ch; srand ( time ( 0 ) ); do { //????? memset ( map, -1, sizeof(map) ); for ( i = 0; i < 10; ) { x = rand()%9 + 1; y = rand()%9 + 1; if ( map[x][y] != 9 ) { map[x][y] = 9; i++; } } cout << " |"; for (i=1; i<10; i++) cout << " " << i; cout << endl; cout << "__|__________________Y" ; cout << endl; for ( i = 1; i < 10; i++ ) { cout << i << " |"; for ( j = 1; j < 10; j++ ) cout << " "<< "#"; cout << "\n"; } cout << " X\n"; cout << "Please input location x,press enter then input location y: \n"; while ( cin >> x >> y ) { cout << "Please select:1.dig, 2.sign, 3.cancel sign, 4.restart, 5.exit: \n"; cin >>type; switch(type) { case 1: { if ( map[x][y] == 9 || map[x][y]==-2) { cout << "YOU LOSE!" << endl; cout << " |"; for (i=1; i<10; i++) cout << " " << i; cout << endl; cout << "__|__________________Y"< > x >>y) cout << "Do you want to play again?(y/n):" << endl; cin >> ch; }//end do while ( ch == 'y' ); return 0;}//end main()
以上就是关于"C++如何实现趣味扫雷游戏"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
j++
内容
标记
趣味
C++
扫雷
地雷
阵列
元素
对象
数字
文章
方法
标点
知识
空格
篇文章
胜利
设计
选择
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全一般事件启动几级响应
兖矿班组网络安全培训
博智网络安全物理隔离系统
摇感测试 网络安全
山东赤子城网络技术有限公
无法远程连接数据库
互联网信息科技发展
诏安县网络安全
网络安全知识主题队会
爱口袋登录服务器出现错误
刺客信条自动退出服务器
西湖区手机应用软件开发
数据库折旧分配表
财务软件服务器域名不正确
网络技术论坛哪个好
方舟方块世界官方服务器
上海网络安全警察做什么的
数据库三年工作经验薪资水平
cac国家网络安全
网络安全的活动感悟
北斗在软件开发中如何用
网络技术和弱电技术
泰拉瑞亚服务器吧
vba数据入数据库
万方硕博论文包含什么数据库
工程管理数据库系统
数据库会崩溃吗
数据库技术问答
数据库形成性
国家网络安全教育的手抄报