当前位置:首页 > 通信资讯 > 正文

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. typedef pair<int, int> P;
  9. const int M = 10;
  10. const int N = 10;
  11. void InitGame(int A[M][N]);//初始化游戏
  12. void Map(int A[M][N], int Static[M][N]);//地图
  13. void Result(int A[M][N]);//游戏结果
  14. int IfSafety(int A[M][N],int x,int y);//周围是否有地雷
  15. void Operate(int A[M][N],int Static[M][N], int x, int y); //操作产生的影响
  16. void Welcome(); //欢迎界面
  17. void GameOver(); //结束界面
  18. void gotoxy(int x, int y); //指针位置
  19. void color(int c); //改变颜色

实现部分

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

主函数

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

运行效果

c语言扫雷小游戏(c++ 扫雷游戏)
c语言扫雷小游戏(c++ 扫雷游戏)
c语言扫雷小游戏(c++ 扫雷游戏)
c语言扫雷小游戏(c++ 扫雷游戏)

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

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

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。