C语言实现扫雷

game.h [cpp]view plaincopy

  1. #ifndef __GAME_H__
  2. #define __GAME_H__
  3. #include
  4. #include
  5. #include
  6. #define EASY_COUNT 10
  7. #define ROW 9
  8. #define COL 9
  9. #define ROWS ROW+2
  10. #define COLS COL+2
  11. void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set);
  12. void DisplayBoard(char arr[ROWS][COLS], int row, int col);
  13. void SetMine(char arr[ROWS][COLS], int row, int col);
  14. void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int rwo, int col);
  15. int GetMineCount(char mine[ROWS][COLS], int x, int y);
  16. #endif//__GAME_H__
game.c [cpp]view plaincopy
  1. #include
  2. #include "game.h"
  3. void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set)//初始化棋盘
  4. {
  5. int i = 0;
  6. int j = 0;
  7. for(i = 0; i < rows; i++)
  8. {
  9. for(j = 0; j < cols; j++)
  10. {
  11. arr[i][j] = set;
  12. }
  13. }
  14. }
  15. void DisplayBoard(char arr[ROWS][COLS], int row, int col)//打印棋盘
  16. {
  17. int i = 0;
  18. int j = 0;
  19. for(i = 0; i<= col; i++)
  20. {
  21. printf("%d ",i);
  22. }
  23. printf("\n");
  24. for(i = 1; i <= row; i++)
  25. {
  26. printf("%d ",i);
  27. for(j = 1; j <= col; j++)
  28. {
  29. printf("%c ", arr[i][j]);
  30. }
  31. printf("\n");
  32. }
  33. printf("---------------------\n");
  34. }
  35. void SetMine(char arr[ROWS][COLS], int row, int col)//设置地雷
  36. {
  37. int i = 0;
  38. int j = 0;
  39. int count = EASY_COUNT;
  40. while(count)
  41. {
  42. i = rand()%row+1;
  43. j = rand()%col+1;
  44. if(arr[i][j] == '0')
  45. {
  46. arr[i][j] = '1';
  47. count--;
  48. }
  49. }
  50. }
  51. void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
  52. {
  53. while(1)
  54. {
  55. int x = 0;
  56. int y = 0;
  57. printf("请输入要排查的坐标:->");
  58. scanf("%d%d", &x, &y);
  59. if(x>=1 && x<=ROW && y>=1 && y<= COL)
  60. {
  61. if(mine[x][y] == '1')
  62. {
  63. printf("恭喜你被炸死了\n");
  64. break;
  65. }
  66. else
  67. {
  68. int count = GetMineCount(mine, x, y);
  69. show[x][y] = count+'0';
  70. DisplayBoard(show, row, col);
  71. }
  72. }
  73. else
  74. {
  75. printf("坐标输入不合法,请重新输入:->\n");
  76. }
  77. }
  78. }
  79. int GetMineCount(char arr[ROWS][COLS], int x, int y)
  80. {
  81. return (arr[x-1][y]+
  82. arr[x-1][y-1]+
  83. arr[x][y-1]+
  84. arr[x+1][y-1]+
  85. arr[x+1][y]+
  86. arr[x+1][y+1]+
  87. arr[x][y+1]+
  88. arr[x-1][y+1]-'0'*8);
  89. }
test.c [cpp]view plaincopy
  1. #include
  2. #include "game.h"
  3. void menu()
  4. {
  5. printf("*************************\n");
  6. printf("*****1.play*****\n");
  7. printf("*****0.exit*****\n");
  8. printf("*************************\n");
  9. }
  10. void game()
  11. {
  12. char mine[ROWS][COLS] = {0};
  13. char show[ROWS][COLS] = {0};
  14. InitBoard(mine, ROWS, COLS, '0');
  15. InitBoard(show, ROWS, COLS, '*');
  16. //DisplayBoard(show, ROW, COL);
  17. SetMine(mine, ROW, COL); //设置雷
  18. DisplayBoard(show, ROW, COL); //打印
  19. FindMine(mine, show, ROW, COL); //根据两个棋盘找雷
  20. GetMineCount(mine, ROW, COL ); //得到目标周围雷的个数
  21. }
  22. int main()
  23. {
  24. int input = 0;
  25. srand((unsigned int)time(NULL));
  26. do
  27. {
  28. menu();
  29. printf("请选择->:");
  30. scanf("%d", &input);
  31. switch(input)
  32. {
  33. case 0:
  34. printf("退出游戏\n");
  35. break;
  36. case 1:
  37. game();
  38. break;
  39. default:
  40. printf("输入错误,请重新输入\n");
  41. break;
  42. }
  43. }while(input);
  44. return 0;
  45. }

    推荐阅读