当前位置:   article > 正文

c语言扫雷(递归算法)_扫雷c语言用的什么算法

扫雷c语言用的什么算法

本扫雷程序,由c语言编写,应用二维数组,递归算法,函数调用。

三个文件综合而生成扫雷程序。



game.h //文件

  1. #ifndef __GAME_h__
  2. #define __GAME_h__
  3. #define ROW 10
  4. #define LOW 10
  5. #define ROWS ROW+2
  6. #define LOWS LOW+2
  7. #define COUNT 10
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. void menu();
  12. void game();
  13. void set(char board[ROWS][LOWS],int row,int low,char M);
  14. void show(char board[ROWS][LOWS],int row,int low);
  15. void insert(char board[ROWS][LOWS],int count);
  16. void play(char mine[ROWS][LOWS],char look[ROWS][LOWS],int x,int y);
  17. int jishu(char mine[ROWS][LOWS],int x,int y);
  18. #endif
game.c //文件
  1. #include "game.h"
  2. int main()
  3. {
  4. int n=0;
  5. srand((unsigned)time(NULL));
  6. do
  7. {
  8. menu();
  9. scanf("%d",&n);
  10. switch(n)
  11. {
  12. case 1:
  13. game();
  14. //system("cls");
  15. break;
  16. case 2:
  17. break;
  18. default:printf("输入错误!请重新输入:\n");
  19. }
  20. if (n==2)
  21. {
  22. break;
  23. }
  24. } while (n);
  25. }
  26. void menu()
  27. {
  28. printf("******************************\n");
  29. printf("******************************\n");
  30. printf("******************************\n");
  31. printf("******* 1.play ********\n");
  32. printf("******* 2.Exit ********\n");
  33. printf("******************************\n");
  34. printf("******************************\n");
  35. printf("******************************\n");
  36. }
  37. void game()
  38. {
  39. int x,y;
  40. int m;
  41. int n=0;
  42. int lei=0;
  43. char mine[ROWS][LOWS]={0};
  44. char look[ROWS][LOWS]={0};
  45. set(mine,ROWS,LOWS,'0');//建立布雷棋盘
  46. set(look,ROWS,LOWS,'*');//建立可视界面棋盘
  47. insert(mine,COUNT);//布雷
  48. do
  49. {
  50. int select;
  51. system("cls");
  52. //show(mine,ROW,LOW);
  53. show(look,ROW,LOW);//显示排过的雷
  54. printf("-------1.扫雷-----2.标记雷--------\n");
  55. scanf("%d",&select);
  56. if(select==1)
  57. {
  58. printf("请输入坐标:");
  59. scanf("%d%d",&x,&y);
  60. if (mine[x][y]=='1'&&n==0)//保证第一次踩到的不是雷
  61. {
  62. set(mine,ROWS,LOWS,'0');
  63. insert(mine,COUNT);
  64. }
  65. if (mine[x][y]=='0'&&look[x][y]=='*')
  66. {
  67. play(mine,look,x,y);
  68. show(look,ROW,LOW);
  69. }
  70. if (mine[x][y]=='1'&&look[x][y]=='*')
  71. {
  72. m=0;
  73. look[x][y]='@';
  74. show(look,ROW,LOW);
  75. printf("游戏结束,你踩雷了!!!\n");
  76. }
  77. n++;
  78. }
  79. if (select==2)
  80. {
  81. printf("请输入标记雷的坐标:");
  82. scanf("%d%d",&x,&y);
  83. if (mine[x][y]=='1'&&look[x][y]=='*')
  84. {
  85. look[x][y]='X';
  86. lei++;
  87. show(look,ROW,LOW);
  88. }
  89. if (mine[x][y]=='0'&&look[x][y]=='*')
  90. {
  91. m=0; //为了跳出循环
  92. printf("游戏结束,你雷标记错误了!!!\n");
  93. }
  94. if (lei==COUNT)
  95. {
  96. m=0; //为了跳出循环
  97. printf("恭喜你排雷成功!!!\n");
  98. }
  99. }
  100. } while (m);
  101. }

test.c //文件

  1. #include "game.h"
  2. void set(char board[ROWS][LOWS],int row,int low,char M)
  3. {
  4. int i,j;
  5. for (i=0;i<row;i++)
  6. {
  7. for (j=0;j<low;j++)
  8. {
  9. board[i][j]=M;
  10. }
  11. }
  12. }
  13. void show(char board[ROWS][LOWS],int row,int low)
  14. {
  15. int i,j;
  16. printf(" ");//为了棋盘的美观
  17. for (i=1;i<=row;i++)
  18. {
  19. printf(" %d ",i);
  20. }
  21. printf("\n ------------------------------\n");
  22. for (i=1;i<=row;i++)
  23. { printf("%2d |",i);
  24. for (j=1;j<=low;j++)
  25. {
  26. printf(" %c ",board[i][j]);
  27. if (j==low)
  28. {
  29. printf("|");
  30. }
  31. }
  32. printf("\n");
  33. }
  34. printf(" ------------------------------\n");
  35. }
  36. void insert(char board[ROWS][LOWS],int count)
  37. {
  38. while(count)
  39. {
  40. int x,y;
  41. x=rand()%ROW+1;
  42. y=rand()%LOW+1;
  43. if (board[x][y]=='0')
  44. {
  45. board[x][y]='1';
  46. count--;
  47. }
  48. }
  49. }
  50. void play(char mine[ROWS][LOWS],char look[ROWS][LOWS],int x,int y)//应用递归排除周围没有雷的区域
  51. {
  52. int ret;
  53. ret=jishu(mine,x,y);
  54. if (ret==0)
  55. {
  56. look[x][y]=' ';
  57. if ((x-1)>0&&(y-1)>0&&(look[x-1][y-1]=='*'))
  58. play(mine,look,x-1,y-1);
  59. if ((x-1)>0&&(y)>0&&(look[x-1][y]=='*'))
  60. play(mine,look,x-1,y);
  61. if ((x-1)>0&&(y+1)>0&&(look[x-1][y+1]=='*'))
  62. play(mine,look,x-1,y+1);
  63. if ((x)>0&&(y-1)>0&&(look[x][y-1]=='*'))
  64. play(mine,look,x,y-1);
  65. if ((x)>0&&(y+1)>0&&(look[x][y+1]=='*'))
  66. play(mine,look,x,y+1);
  67. if ((x+1)>0&&(y-1)>0&&(look[x+1][y-1]=='*'))
  68. play(mine,look,x+1,y-1);
  69. if ((x+1)>0&&(y)>0&&(look[x+1][y]=='*'))
  70. play(mine,look,x+1,y);
  71. if ((x+1)>0&&(y+1)>0&&(look[x+1][y+1]=='*'))
  72. play(mine,look,x+1,y+1);
  73. }else
  74. look[x][y]=ret+'0';
  75. }
  76. int jishu(char mine[ROWS][LOWS],int x,int y)//统计周围的雷数
  77. {
  78. return mine[x-1][y-1]+
  79. mine[x-1][y]+
  80. mine[x-1][y+1]+
  81. mine[x][y-1]+
  82. mine[x][y+1]+
  83. mine[x+1][y-1]+
  84. mine[x+1][y]+
  85. mine[x+1][y+1]-8*'0';
  86. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/748815
推荐阅读
相关标签
  

闽ICP备14008679号