经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
基于C语言实现简单的扫雷游戏
来源:jb51  时间:2022/5/9 11:08:24  对本文有异议

效果展示

开始的界面

输入0结束程序

输入1开始游戏

选择标记地雷或者选择踩坐标

输入0标记地雷模式

输入坐标

输入1踩坐标模式

输入坐标

在输入坐标处输入0 0结束游戏

踩到炸弹,出现炸弹位置

(1表示炸弹的位置,0表示没有炸弹的位置)

输入0结束程序

输入1重新开始游戏

胜利

输入0结束程序

输入1重新开始游戏

代码

我创建了两个.c源文件,一个.h头文件

test.c

  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include"game.h"
  4.  
  5. int main()
  6. {
  7. int exi = 0;
  8. srand((unsigned int)time(NULL));
  9. board();
  10. printf("请输入是否开始游戏:>");
  11. scanf("%d", &exi);
  12. do
  13. {
  14. switch (exi)
  15. {
  16. case 1:
  17. {
  18. game();
  19. printf("是否输入1重新开始游戏:>");
  20. scanf("%d", &exi);
  21. if (exi == 0)
  22. {
  23. printf("游戏结束");
  24. }
  25. break;
  26. }
  27. case 0:
  28. {
  29. printf("游戏结束");
  30. break;
  31. }
  32. default:
  33. {
  34. printf("输入错误,请重新输入:>");
  35. scanf("%d", &exi);
  36. if (exi == 0)
  37. {
  38. printf("游戏结束\n");
  39. }
  40. break;
  41. }
  42. }
  43. } while (exi);
  44.  
  45.  
  46.  
  47. return 0;
  48. }

game.h

  1. #pragma once
  2.  
  3. #include<stdio.h>
  4. #include<time.h>
  5. #include<stdlib.h>
  6.  
  7. #define WID 9
  8. #define LON 9
  9. #define WIDS WID+2
  10. #define LONS LON+2
  11. #define RAN 5
  12.  
  13. void board();
  14. //打印开始的面板
  15.  
  16. void game();
  17. //游戏运行的起点
  18.  
  19. void initialization(char mane[WIDS][LONS], char siz, int x, int y);
  20. //把数组内框初始化为siz
  21.  
  22. void display(char mane[WIDS][LONS], int x, int y);
  23. //打印数组内框的字符
  24.  
  25. void random(char mane[WIDS][LONS], int count);
  26. //在数组中随机赋予count个炸弹
  27.  
  28. int look(char mane[WIDS][LONS], int x, int y);
  29. //计算mane数组x,y位置周围有多少炸弹
  30.  
  31. void judge(char mane[WIDS][LONS], char show[WIDS][LONS],char include[WIDS][LONS]);
  32. //判断输入是否获得胜利
  33.  
  34. void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS], int X, int Y);
  35. //判断周围没有雷,会向外继续推,直到出现雷
  36.  
  37. void change(char show[WIDS][LONS], int x, int y, char siz);
  38. //改变数组show位置(x,y)为字符siz
  39.  
  40. void jishu();
  41. //统计选择了几次的位置,包括类推的位置,实现一点出现一大片的功能

game扫雷.c

  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include"game.h"
  4.  
  5. static int a = 0;
  6.  
  7.  
  8. void board()
  9. {
  10. printf("****************************\n");
  11. printf("****************************\n");
  12. printf("********* 1.play **********\n");
  13. printf("********* 0.exit **********\n");
  14. printf("****************************\n");
  15. printf("****************************\n");
  16.  
  17. }
  18.  
  19. //数组初始化
  20. void initialization(char mane[WIDS][LONS], char siz, int x, int y)
  21. {
  22. int i = 0;
  23. for (i = 0; i <= x+1; i++)
  24. {
  25. int j = 0;
  26. for (j = 0; j <= y+1; j++)
  27. {
  28. mane[i][j] = siz;
  29. }
  30.  
  31. }
  32. }
  33.  
  34.  
  35. //打印第一个面板
  36. void display(char mane[WIDS][LONS], int x,int y)
  37. {
  38. int i = 0;
  39. int j = 0;
  40. printf("-----------扫雷-----------\n");
  41. printf("0 | ");
  42.  
  43. for (j = 1; j <= y; j++)
  44. {
  45. printf("%d ",j);
  46. }
  47. printf("\n");
  48. printf("- - -");
  49.  
  50. for (j = 1; j <= y; j++)
  51. {
  52. printf(" -");
  53. }
  54.  
  55.  
  56. for (i = 1; i <= x; i++)
  57. {
  58. printf("\n");
  59. printf("%d | ",i);
  60. for (j = 1; j <= y; j++)
  61. {
  62. printf("%c ", mane[i][j]);
  63. }
  64.  
  65. }
  66. printf("\n-----------扫雷-----------\n");
  67. }
  68.  
  69.  
  70.  
  71. void random(char mane[WIDS][LONS],int count)
  72. {
  73. int x = 0;
  74. int y = 0;
  75. while (count)
  76. {
  77. x = rand() % WID + 1;
  78. y = rand() % LON + 1;
  79. if (mane[x][y] == '0')
  80. {
  81. mane[x][y] = '1';
  82. count--;
  83. }
  84.  
  85. }
  86.  
  87. }
  88.  
  89. int look(char mane[WIDS][LONS],int x,int y)
  90. {
  91. return mane[x][y + 1] +
  92. mane[x][y - 1] +
  93. mane[x - 1][y + 1] +
  94. mane[x - 1][y - 1] +
  95. mane[x + 1][y + 1] +
  96. mane[x + 1][y - 1] +
  97. mane[x - 1][y] +
  98. mane[x + 1][y]-8*'0';
  99.  
  100.  
  101. }
  102.  
  103. void jishu()
  104. {
  105. a++;
  106. }
  107.  
  108. void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS],int X,int Y)
  109. {
  110. if (include[X][Y] != '1')
  111. {
  112. int count = 0;
  113. count = look(mane, X, Y);
  114. show[X][Y] = count + '0';
  115. include[X][Y] = '1';
  116. jishu();
  117. if (count == 0)
  118. {
  119. xunhuan(mane, show, include, X + 1, Y + 1);
  120. xunhuan(mane, show, include, X - 1, Y - 1);
  121. xunhuan(mane, show, include, X + 1, Y);
  122. xunhuan(mane, show, include, X - 1, Y);
  123. xunhuan(mane, show, include, X, Y + 1);
  124. xunhuan(mane, show, include, X, Y - 1);
  125. xunhuan(mane, show, include, X + 1, Y - 1);
  126. xunhuan(mane, show, include, X - 1, Y + 1);
  127. }
  128. }
  129.  
  130. }
  131.  
  132. void change(char show[WIDS][LONS], int x, int y,char siz)
  133. {
  134. show[x][y] = siz;
  135.  
  136. }
  137.  
  138. void judge(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS])
  139. {
  140. int X = 0;
  141. int Y = 0;
  142. display(show, WID, LON);
  143.  
  144.  
  145. do
  146. {
  147. int num = a;
  148.  
  149. if (num == WID * LON - RAN)
  150. {
  151. printf("恭喜你获得胜利!\n\n");
  152. display(mane, WID, LON);
  153.  
  154. break;
  155. }
  156.  
  157.  
  158. printf("想要标记地雷就输入0,想要选择就输入1):>");
  159. int choose = 0;
  160. scanf("%d", &choose);
  161. printf("\n");
  162.  
  163. if (choose==1)
  164. {
  165. printf("输入0 0结束游戏\n");
  166.  
  167. printf("请输入你选择的坐标:>");
  168.  
  169. scanf("%d%d", &X, &Y);
  170.  
  171. if (X == 0 && Y == 0)
  172. {
  173. printf("\n结束此次游戏\n\n");
  174. break;
  175. }
  176.  
  177. if (X >= 1 && X <= 9 && Y >= 1 && Y <= 9)
  178. {
  179. if (mane[X][Y] == '1')
  180. {
  181. printf("\n你吃到炸弹啦,死翘翘了\n\n");
  182. display(mane, WID, LON);
  183. break;
  184. }
  185. else
  186. {
  187. xunhuan(mane, show, include, X, Y);
  188. display(show, WID, LON);
  189.  
  190. //display(mane, WID, LON);
  191. }
  192. }
  193. else
  194. {
  195. printf("\n你输的超过范围啦,");
  196. }
  197. }
  198. else
  199. {
  200.  
  201. printf("\n输入0 0结束游戏\n");
  202.  
  203. printf("请输入你选择的坐标:>");
  204.  
  205. scanf("%d%d", &X, &Y);
  206.  
  207. if (X == 0 && Y == 0)
  208. {
  209. printf("\n结束此次游戏\n\n");
  210. break;
  211. }
  212. change(show,X,Y,'F');
  213. display(show, WID, LON);
  214.  
  215. }
  216. } while (1);
  217.  
  218.  
  219. }
  220.  
  221. void chu(char mane[WIDS][LONS], char siz,int x, int y)
  222. {
  223. int i = 0;
  224. for (i = 1; i <= x ; i++)
  225. {
  226. int j = 0;
  227. for (j = 1; j <= y ; j++)
  228. {
  229. mane[i][j] = siz;
  230. }
  231.  
  232. }
  233.  
  234. }
  235.  
  236. void game()
  237. {
  238. char mane[WIDS][LONS];
  239. char show[WIDS][LONS];
  240. char include[WIDS][LONS];
  241.  
  242. initialization(mane, '0', WID, LON);
  243. initialization(show, '*', WID, LON);
  244. initialization(include, '1', WID, LON);
  245.  
  246. chu(include, '0', WID, LON);
  247.  
  248. random(mane,RAN);
  249.  
  250. //display(mane, WID, LON);
  251. //display(show, WID, LON);
  252. judge(mane,show,include);
  253. }

我写的这个小游戏还很粗糙,不过才开始学,进步空间还是很大的,代码就上传到gitee

以上就是基于C语言实现简单的扫雷游戏的详细内容,更多关于C语言 扫雷的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号