服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现简单扫雷小游戏

C++实现简单扫雷小游戏

2021-09-28 10:50Who_Am_I. C/C++

这篇文章主要为大家详细介绍了C++实现简单扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现简单扫雷小游戏的具体代码,供大家参考,具体内容如下

头文件Mine_Sweep.h

  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <Windows.h>
  7. using namespace std;
  8.  
  9. typedef pair<int, int> P;
  10. const int M = 10;
  11. const int N = 10;
  12.  
  13. void InitGame(int A[M][N]);//初始化游戏
  14. void Map(int A[M][N], int Static[M][N]);//地图
  15. void Result(int A[M][N]);//游戏结果
  16. int IfSafety(int A[M][N],int x,int y);//周围是否有地雷
  17. void Operate(int A[M][N],int Static[M][N], int x, int y); //操作产生的影响
  18. void Welcome(); //欢迎界面
  19. void GameOver(); //结束界面
  20. void gotoxy(int x, int y); //指针位置
  21. void color(int c); //改变颜色

实现部分

  1. #include"Mine_Sweep.h"
  2.  
  3. void InitGame(int A[M][N])
  4. {
  5. //从右开始顺时针->右(1,0)右上(1,1)上(0,1)左上(-1,1)左(-1,0)左下(-1,-1)下(0,-1)右下(1,-1)
  6. int Sign[8][2] = { {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1} };
  7. int count = 0;//产生M个不同随机数
  8. int Random; //随机数1-100
  9. int x, y; //横纵坐标
  10. srand(unsigned(time(NULL)));
  11. //清空
  12. for (int i = 0; i < M; i++)
  13. for (int j = 0; j < N; j++)
  14. A[i][j] = 0;
  15. //把M个雷安放好
  16. while (count < M)
  17. {
  18. Random = rand() % 100 + 1;
  19. x = Random / 10; //转换为X
  20. y = Random - 10 * x; //转换为Y
  21. if (A[x][y] == 0) //地雷设为-1,无地雷设为0,1,2...
  22. {
  23. A[x][y] = -1;
  24. count++;
  25. }
  26. }
  27. //构建一个虚拟的地图即增加外围[M][N]->[M+2][N+2]目的是为了让边界和内部的处理方式一样
  28. int Virtual[M + 2][N + 2] = { 0 };
  29. for (int i = 0; i < M; i++)
  30. {
  31. for (int j = 0; j < N; j++)
  32. {
  33. Virtual[i + 1][j + 1] = A[i][j];//所有元素向下向右移动1格
  34. }
  35. }
  36. //计算每个地方的地雷分布情况
  37. for (int i = 1; i <= M; i++)
  38. {
  39. for (int j = 1; j <= N; j++)
  40. {
  41. int flag = 0;
  42. //如果是地雷
  43. if (Virtual[i][j] == -1)
  44. continue; //跳过
  45. for (int k = 0; k < 8; k++)
  46. {
  47. if (Virtual[i + Sign[k][0]][j + Sign[k][1]]==-1)
  48. flag++;
  49. }
  50. A[i-1][j-1] = flag;
  51. }
  52. }
  53. }
  54.  
  55. void Map(int A[M][N],int Static[M][N])
  56. {
  57. system("cls");
  58. Welcome();
  59. //地雷用※表示,数字用123...,未翻开用■ Static[M][N]用于储存状态0表示未翻开,1表示翻开
  60. for (int i = 0; i < M; i++)
  61. {
  62. cout << "\t\t\t\t\t"<<" ";
  63. for (int j = 0; j < N; j++)
  64. {
  65. if (Static[i][j] == 0)
  66. cout << "■";
  67. else
  68. {
  69. if (A[i][j] == -1)
  70. {
  71. cout << "※";
  72.  
  73. }
  74. else
  75. {
  76. if (IfSafety(A, i, j))
  77. cout << "□";
  78. else
  79. cout <<" " <<A[i][j];
  80. }
  81. }
  82. }
  83. cout << endl;
  84. }
  85. }
  86.  
  87. void Operate(int A[M][N], int Static[M][N],int x,int y)//贪心算法
  88. {
  89. queue<P> que;//队列
  90. //从右开始顺时针->右(1,0)右上(1,1)上(0,1)左上(-1,1)左(-1,0)左下(-1,-1)下(0,-1)右下(1,-1)
  91. int Sign[8][2] = { {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1} };
  92. if (A[x][y] == -1) //如果翻到地雷,游戏结束
  93. {
  94. Result(A); //显示结果
  95. GameOver(); //游戏结束
  96. exit(0);
  97. }
  98. Static[x][y] = 1;//翻开
  99. que.push(P(x, y));
  100.  
  101. while (que.size()) {
  102. P p = que.front();
  103. que.pop();
  104. if(!IfSafety(A, p.first, p.second))
  105. continue; //如果周围有炸弹,继续遍历队列
  106.  
  107. for (int i = 0; i < 8; i++) {
  108. int nx = p.first + Sign[i][0];
  109. int ny = p.second + Sign[i][1];
  110. if (0 <= nx && nx < M && 0 <= ny && ny < N && Static[nx][ny] == 0) {
  111. que.push(P(nx, ny));
  112. Static[nx][ny] = 1;//翻开
  113. }
  114. }
  115. }
  116. }
  117. void Result(int A[M][N])
  118. {
  119. for (int i = 0; i < M; i++) {
  120. cout << "\t\t\t\t\t";
  121. for (int j = 0; j < N; j++)
  122. cout << A[i][j]<<" ";
  123. cout << endl;
  124. }
  125.  
  126. }
  127.  
  128. int IfSafety(int A[M][N],int x, int y)
  129. {
  130. //从右开始顺时针->右(1,0)右上(1,1)上(0,1)左上(-1,1)左(-1,0)左下(-1,-1)下(0,-1)右下(1,-1)
  131. int Sign[8][2] = { {1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1} };
  132. int Virtual[M + 2][N + 2] = { 0 };
  133. x = x + 1;//虚拟坐标
  134. y = y + 1;
  135. for (int i = 0; i < M; i++)
  136. {
  137. for (int j = 0; j < N; j++)
  138. {
  139. Virtual[i + 1][j + 1] = A[i][j];//所有元素向下向右移动1格
  140. }
  141. }
  142. //计算每个地方的地雷分布情况
  143. for (int k = 0; k < 8; k++)
  144. {
  145. if (Virtual[x + Sign[k][0]][y + Sign[k][1]] == -1)
  146. return 0;
  147. }
  148. return 1;
  149. }
  150.  
  151. void Welcome()
  152. {
  153. cout << "\t\t\t\t\t\t扫雷游戏"<<endl<<endl;
  154. cout << "\t\t\t\t===========================================" << endl << endl;
  155. gotoxy(40, 3);
  156. for (int i = 0; i < M; i++)
  157. {
  158. //画x轴坐标
  159. cout << " " << i + 1;
  160. }
  161.  
  162. for (int j = 0; j < M; j++)
  163. {
  164. //画Y轴坐标
  165. gotoxy(38, 4+j);
  166. cout << j + 1<<endl;
  167. }
  168. gotoxy(0, 4);
  169. }
  170.  
  171. void GameOver()
  172. {
  173. cout << "\t\t\t\t游戏结束,你输了" << endl;
  174. getchar();
  175. getchar();
  176. }
  177.  
  178. void gotoxy(int x, int y)
  179. {
  180. COORD pos;
  181. pos.X = x;
  182. pos.Y = y;
  183. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  184. }
  185.  
  186. void color(int c)
  187. {
  188. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
  189. }

主函数

  1. #include"Mine_Sweep.h"
  2.  
  3. int main()
  4. {
  5. int A[M][N], x, y, Static[M][N] = { 0 };
  6. color(13);
  7. InitGame(A);
  8. cout << "\n\n\n\t\t游戏说明:" << "你需要在不点错雷的情况下尽可能快的将所有的雷都标记出来\n";
  9. cout << "\t\t你点出了一个数字,是1,就说明它周围的8的格子里有1个雷,是2就有两个雷,是3就有三个雷···" << endl;
  10. cout<< "\t\t但如果你标记错了雷,那就会boom!游戏结束。"<<endl;
  11. cout << "\t\t请先输入横坐标,再输入纵坐标 eg: 1 2" << endl;
  12. cout << "\t\t回车键开始,祝你好运!" << endl;
  13. getchar();
  14. color(15);
  15. while (1)
  16. {
  17. int flag = 1;
  18. Map(A, Static);
  19. cout << "\t\t\t\t请输入要翻开的坐标:";
  20. cin >> x >> y;
  21. while (x <= 0 || x > M || y <= 0 || y > N)
  22. {
  23. cout << "\t\t\t\t超出范围,请重新输入要翻开的坐标:" ;
  24. cin >> x >> y;
  25. }
  26. Operate(A, Static, x-1, y-1);
  27. }
  28. return 0;
  29. }

运行效果

C++实现简单扫雷小游戏
C++实现简单扫雷小游戏
C++实现简单扫雷小游戏
C++实现简单扫雷小游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/weixin_45710947/article/details/108687305

延伸 · 阅读

精彩推荐