C++用类实现一个控制台扫雷游戏随机产生 8*8 的雷分布图

CSDN问答 2021-12-30 06:28:53 阅读数:65

c++ 控制台 实现 一个 控制

完成一个控制台扫雷游戏。每个网格相邻区域只包含同一行和同一列紧邻的 4 个网格。
规则:
(1)随机产生 8*8 的雷分布图;
(2)用户“y”表示游戏,“q”退出游戏;
(3)让用户输入行列号,用键盘“m”标雷,“k”打开;
(4)打开区域为雷或者全部区域被翻开,则 game over。

按如下 CGame 类的定义完成代码。

class CGame {public:CGame(); //初始化virtual ~CGame(); void Print(); //打印当前图bool DoStep(); void FlgMine(int x,int y); //标记雷void Open(int x,int y); //打开该结点bool Judge(); //判断是否结束void Run(); //游戏运行}



采纳答案:

答题思路如下:

#include <iostream>#include <stdlib.h>#include <time.h>using namespace std;class CGame {public: CGame(); //初始化 virtual ~CGame(); void Print(); //打印当前图 bool DoStep(); void FlgMine(int x,int y); //标记雷 void Open(int x,int y); //打开该结点 bool Judge(); //判断是否结束 void Run(); //游戏运行private: int grid[8][8]; //生成雷图,0表示无雷,1表示有雷 int state[8][8]; //状态,0表示未翻开,1表示标雷,2表示已翻开 bool mflag; //是否触雷 bool bover; //游戏是否结束};CGame::CGame(){ int i,j; mflag = false; bover = false; for (i=0;i<8;i++) { for (j=0;j<8;j++) { grid[i][j] = rand()%2; //生成0-1的随机数 state[i][j] = 0; } }}void CGame::Print(){ int i,j; system("cls"); for (i=0;i<8;i++) { for (j=0;j<8;j++) { if (state[i][j] == 0) cout << "□"; else if(state[i][j] == 1) cout << "m"; else cout << grid[i][j]<<" "; //打开 } cout << endl; }}//bool CGame::DoStep(){ int x,y,op; cout << "请输入行列数:"; cin >>x>>y; cout << "请选择:1.标雷 2.翻开"; while(1) { cin >> op; if(op==1) { FlgMine(x,y); break; } else { Open(x,y); if(mflag) return false; break; } } fflush(stdin); cout << "是否继续(按y继续/按q退出):"; char ch = getchar(); getchar(); if(ch == 'Y' || ch == 'y') return true; return false;}//标记雷void CGame::FlgMine(int x,int y){ state[x-1][y-1] = 1; //标记雷 Print(); //刷新雷图}//打开该结点void CGame::Open(int x,int y){ state[x-1][y-1] = 2;//翻开 Print();//刷新雷图 if(grid[x-1][y-1] == 1)//是雷 { mflag = true; //结束游戏 cout << "翻到雷了"; }}//判断是否结束bool CGame::Judge() { if(mflag) return true; //判断是否都已经翻出 for (int i=0;i<8;i++) { for (int j=0;j<8;j++) { if (state[i] == 0) { return false; } } } return true;}//游戏运行void CGame::Run(){ Print(); while(!bover) { if( DoStep() ) Judge(); else break; }}CGame::~CGame(){}int main(){ srand((unsigned int)time(NULL)); CGame game; game.Run(); cout << "游戏运行结束"<<endl; system("pause"); return 0;}


其他答案2:

你要部署几颗雷呢?


其他答案3:

其他答案4:
#include<iostream>#include<cstdlib>#include<ctime>#include<vector>#include<iomanip>using namespace std;class CGame{public: void Print(); //打印当前图 void DoStep(); void FlgMine(int x, int y); //标记雷 void Open(int x, int y); //打开该结点 bool Judge(); //判断是否结束 void Run(); //游戏运行private: char map[8][8];};bool CGame::Judge(){ return 0;}void CGame::Open(int x, int y){ if (map[x][y] == '*') { cout << "game over" << endl; exit(0); } else { for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { //cout << map[i][j] << " "; cout << "#" << " "; } cout << endl; } }}void CGame::Print(){ srand(time(0)); //constexpr int sz = 8; //char map[sz][sz]; for (auto& row : map) { for (auto& col : row) { int num = rand() % 100; if (num <= 40) col = '*'; else col = '0'; } } for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; j++) { if (map[i][j] != '*') continue; if (i + 1 < 8 && map[i + 1][j] != '*')map[i + 1][j] += 1; if (i - 1 >= 0 && map[i - 1][j] != '*')map[i - 1][j] += 1; if (j + 1 < 8 && map[i][j + 1] != '*')map[i][j + 1] += 1; if (j + 1 >= 0 && map[i][j - 1] != '*')map[i][j - 1] += 1; } } for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { //cout << map[i][j] << " "; cout << "#" << " "; } cout << endl; }}void CGame::DoStep(){ char dostep; cin >> dostep; if (dostep == 'y') { cout << "开始游戏" << endl; } else if(dostep == 'q') { cout << "退出游戏" << endl; exit(0); } else { cout << "输入错误" << endl; exit(0); }}void CGame::FlgMine(int x, int y){ if (map[x][y] == '*') { cout << map[x][y] << endl; } else { cout << map[x][y] << endl; }}void CGame::Run(){ cout << "按Y开始游戏按Q退出游戏" << endl; DoStep(); Print(); cout << "请输入行列号(X,Y):" << endl; int x; int y; cin >> x; cin >> y; cout << "按M标记雷,按K打开雷" << endl; char o; cin >> o; if (o == 'm') { FlgMine(x, y); } else if (o == 'k') { Open(x, y); } else { cout << "输入错误"; exit(0); }}void main(){ CGame game; game.Run(); }

我写了一部分,而后卡住了

版权声明:本文为[CSDN问答]所创,转载请带上原文链接,感谢。 https://ask.csdn.net/questions/7617956