快来用C语言写个扫雷小游戏吧!
1. #define _CRT_SECURE_NO_WARNINGS 2. #define MAX_ROW 9 3. #define MAX_COL 9 4. #define DIFFICULTY 10 5. #include <stdio.h> 6. #include <time.h> 7. #include <stdlib.h> 8. char show[MAX_ROW][MAX_COL]; 9. char mine[MAX_ROW][MAX_COL]; 10. int safe; 11. //打印菜单 12. int menu(){ 13. printf("---------------------\n"); 14. printf("-----1. 开始游戏-----\n"); 15. printf("-----0. 退出游戏-----\n"); 16. printf("---------------------\n"); 17. int choice = 0; 18. printf("请输入你的选择:"); 19. scanf("%d", &choice); 20. return choice; 21. } 22. //初始化地图 23. void init(char show[MAX_ROW][MAX_COL], char mine[MAX_ROW][MAX_COL]){ 24. for (int row = 0; row < MAX_ROW; row++){ 25. for (int col = 0; col < MAX_COL; col++){ 26. show[row][col] = '*'; 27. mine[row][col] = '0'; 28. } 29. } 30. int n = DIFFICULTY; 31. while (n>0){ 32. int row = rand() % MAX_ROW; 33. int col = rand() % MAX_COL; 34. if (mine[row][col] == '@')continue; 35. mine[row][col] = '@'; 36. n--; 37. } 38. for(int x=0;x<MAX_ROW;x++) 39. for(int y=0;y<MAX_COL;y++){ 40. int count = 0; 41. if(mine[x][y]!='@'){ 42. for (int r = x - 1; r <= x + 1; r++) 43. for (int c = y - 1; c <= y + 1; c++) { 44. if (r>= 0 && r < MAX_ROW&& c >=0 &&c < MAX_COL && mine[r][c] == '@') 45. count++; 46. } 47. mine[x][y] = count + '0'; 48. } 49. } 50. } 51. //打印地图 52. void printMap(char themap[MAX_ROW][MAX_COL]){ 53. printf(" |"); 54. for (int col = 0; col < MAX_COL; col++) 55. printf("%d ", col); 56. printf("\n"); 57. printf("--+------------------\n"); 58. for (int row = 0; row < MAX_ROW; row++) { 59. printf(" %d|", row); 60. for (int col = 0; col < MAX_COL; col++) 61. printf("%c ", themap[row][col]); 62. printf("\n"); 63. } 64. } 65. //标注周围雷区 66. void updateShow(char show[MAX_ROW][MAX_COL], char mine[MAX_ROW][MAX_COL], int row, int col){ 67. int mee[MAX_ROW][MAX_COL]={0}; 68. int book[100][2]={0}; 69. int top=0,tail=1; 70. book[1][0]=row;book[1][1]=col; 71. int x,y; 72. while(tail>top){ 73. top++; 74. x=book[top][0];y=book[top][1];mee[x][y]=1; 75. int count = 0; 76. for (int r = x - 1; r <= x + 1; r++) 77. for (int c = y - 1; c <= y + 1; c++) { 78. if (r>= 0 && r < MAX_ROW&& c >=0 &&c < MAX_COL && mee[r][c]==0&&mine[x][y]=='0'){ 79. tail++;book[tail][0]=r;book[tail][1]=c; mee[r][c]=1; 80. } 81. } 82. if(show[x][y]!= mine[x][y]) { 83. show[x][y] = mine[x][y];safe++; 84. } 85. } 86. } 87. //游戏开始 88. void game(){ 89. init(show, mine); 90. safe = 0; 91. while (1){ 92. printMap(show); 93. int row = 0; 94. int col = 0; 95. printf("请输入翻开的坐标(row col):"); 96. scanf("%d %d", &row, &col); 97. if (row < 0 || col < 0 || row >= MAX_ROW || col >= MAX_COL){ 98. printf("输入的坐标超过范围,重新输入:\n"); 99. continue; 100. } 101. else if (show[row][col] != '*'){ 102. printf("已经翻开,请重新输入:\n"); 103. continue; 104. } 105. else if (mine[row][col] == '@'){ 106. printf("你已经被炸死了!!!\n"); 107. // 打印一遍地雷的地图, 让玩家死的明白 108. printMap(mine); 109. break; 110. } 111. else{ 112. updateShow(show, mine, row, col); 113. printf("safe:%d\n",safe); 114. if (safe == MAX_ROW*MAX_COL - DIFFICULTY){ 115. printf("已经排除所有的雷,你已经安全!!!\n"); 116. // 打印一遍地雷的地图, 让玩家知道雷在哪 117. printMap(mine); 118. break; 119. } 120. } 121. } 122. } 123. int main() 124. { 125. srand((unsigned int)time(0)); 126. while (1){ 127. int choice = menu(); 128. if (choice == 1)game(); 129. else if (choice == 0){ 130. printf("退出游戏,byebye!"); 131. break; 132. } 133. else{ 134. printf("输入错误,请重新输入:"); 135. continue; 136. } 137. } 138. return 0; 139. }