• C语言第十一课(下):优化扫雷游戏


    目录

    前言:

    一、功能优化:

    1.清理展开优化:

    2.估测地雷标记:

    3.剩余雷量显示:

    4.胜利条件更新:

    二、界面优化:

    三、终极优化后最终全部代码:

    1.game.h:

    2.game.c:

    3.test.c:

    四、总结:


    前言:

            在上一篇文章中,我和各位小伙伴们一起,成功编写出了一个基础版扫雷游戏。但作为基础版的它依旧存在着一些缺陷,而在本文中,我们将要对基础版扫雷游戏进行功能、界面两个方面的优化。

    一、功能优化:

    1.清理展开优化:

            我们上次说到,在我们进行扫雷时,当我们对某坐标进行排雷时,只能排除该坐标,而不能像真正的扫雷游戏一样,在排除一个坐标时,选中一次就可以展开相邻的一大片安全位置。我们现在就来增加并优化这个功能。

            我们该怎么优化这个功能呢?首先,我们执行该功能的前提是该位置是安全的。则我们将该功能写成一个函数,并将其添加在我们之前就写好的,判断该位置没有“地雷”、已经判断安全的位置上:

    1. else
    2. {
    3. int n = get_mine_count(mine, x, y);
    4. show[x][y] = n + '0';
    5. //清除展开函数:
    6. Cls(mine, show, row, col, x, y);
    7. DisplayBoard(show, ROW, COL);
    8. win++;
    9. break;
    10. }

            接下来就是这个函数功能的实现了,首先当前选中位置已经确定为安全了,则将数组与该位置坐标全部传入函数中,若该位置存放的位置为' 0 ',则说明该以位置为中心的九宫格内均不是“地雷”,则将该位置替换为空格符'  '并放置在展示数组中,在每一次清除展开处理结束后打印展示给玩家:

    1. void Cls(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
    2. {
    3. if (show[x][y] == '0')
    4. //若此元素已经为'0',则进入此函数
    5. {
    6. show[x][y] = ' ';
    7. }
    8. }

            当然,仅仅只处理选中坐标是远远不够的,我们的目标是一次性处理大片的坐标。我们的处理方法是,通过遍历来对以该坐标为中心的九宫格内其他坐标进行相同的操作:

    1. void Cls(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
    2. {
    3. if (show[x][y] == '0')
    4. //若此坐标内元素为'0',则进入此函数
    5. {
    6. show[x][y] = ' ';
    7. int i = x - 1;
    8. for (; i <= x + 1; i++)
    9. {
    10. int j = y - 1;
    11. for (; j <= y + 1; j++)
    12. {
    13. if (i >= 1 && i <= row && j >= 1 && j <= col)
    14. //检查坐标越界
    15. {
    16. int ret = get_mine_count(mine, i, j);
    17. show[i][j] = '0' + ret;
    18. }
    19. }
    20. }
    21. }
    22. }

            同时,在对周围坐标进行处理前,若周围坐标已经在之前的处理中被替换为了空格符,则不再进行接下来的替换操作,进行下一个坐标的判断与操作:

    1. void Cls(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
    2. {
    3. if (show[x][y] == '0')
    4. //若此坐标内元素为'0',则进入此函数
    5. {
    6. show[x][y] = ' ';
    7. int i = x - 1;
    8. for (; i <= x + 1; i++)
    9. {
    10. int j = y - 1;
    11. for (; j <= y + 1; j++)
    12. {
    13. if (show[i][j] == ' ')
    14. {
    15. continue;
    16. }
    17. if (i >= 1 && i <= row && j >= 1 && j <= col)
    18. //检查坐标越界
    19. {
    20. int ret = get_mine_count(mine, i, j);
    21. show[i][j] = '0' + ret;
    22. Cls(mine, show, row, col, i, j);
    23. //不断迭代,直到不再出现0或数字为止
    24. }
    25. }
    26. }
    27. }
    28. }

            同时我们还要注意之前进行过周围计数处理,并且需要在屏幕上打印出周围“地雷”数量的坐标,我们也要将其跳过,不能进行重复处理:

    1. void Cls(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
    2. {
    3. if (show[x][y] == '0')
    4. //若此坐标内元素为'0',则进入此函数
    5. {
    6. show[x][y] = ' ';
    7. int i = x - 1;
    8. for (; i <= x + 1; i++)
    9. {
    10. int j = y - 1;
    11. for (; j <= y + 1; j++)
    12. {
    13. if (show[i][j] == ' ')
    14. {
    15. continue;
    16. }
    17. if (i >= 1 && i <= row && j >= 1 && j <= col)
    18. //检查坐标越界
    19. {
    20. if (show[i][j] != '*')
    21. //如果该元素不是'*',即已经被赋值,则无需进行下面的操作
    22. {
    23. continue;
    24. }
    25. int ret = get_mine_count(mine, i, j);
    26. show[i][j] = '0' + ret;
    27. }
    28. }
    29. }
    30. }
    31. }

            最后,我们通过使用迭代处理,对最初的坐标周围九宫格内的坐标进行相同的上述操作,清除展开功能实现:

    1. void Cls(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
    2. {
    3. if (show[x][y] == '0')
    4. //若此坐标内元素为'0',则进入此函数
    5. {
    6. show[x][y] = ' ';
    7. int i = x - 1;
    8. for (; i <= x + 1; i++)
    9. {
    10. int j = y - 1;
    11. for (; j <= y + 1; j++)
    12. {
    13. if (show[i][j] == ' ')
    14. {
    15. continue;
    16. }
    17. if (i >= 1 && i <= row && j >= 1 && j <= col)
    18. //检查坐标越界
    19. {
    20. if (show[i][j] != '*')
    21. //如果该元素不是'*',即已经被赋值,则无需进行下面的操作
    22. {
    23. continue;
    24. }
    25. int ret = get_mine_count(mine, i, j);
    26. show[i][j] = '0' + ret;
    27. Cls(mine, show, row, col, i, j);
    28. //不断迭代,直到不再出现0或数字为止
    29. }
    30. }
    31. }
    32. }
    33. }

    2.估测地雷标记:

            为了方便玩家更好的进行排雷,我们还可以在每次排雷前向玩家提供一个标记地雷的功能,来帮助玩家对排雷推理的展示更加清晰,使玩家的游戏过程更加轻松:

    1. void PlayerJudge(char show[ROWS][COLS], int row, int col)
    2. {
    3. int m = 0;
    4. do
    5. {
    6. printf("请问您是否想要标记某位置?\n");
    7. printf("是:1 否:0:");
    8. scanf("%d", &m);
    9. switch (m)
    10. {
    11. case 1:
    12. Judge(show, row, col);
    13. break;
    14. case 0:
    15. break;
    16. default:
    17. printf("输入有误,请重新输入:\n");
    18. break;
    19. }
    20. } while (m);
    21. }

            实现原理类似于菜单函数的处理,通过do-while语句switch语句的结合使用来达到我们的目的。

            而该功能的重点依旧是标记功能函数Judge的实现定义:

    1. void Judge(char show[ROWS][COLS], int row, int col)
    2. {
    3. int x = 0;
    4. int y = 0;
    5. while (1)
    6. {
    7. int a = 0;
    8. int b = 0;
    9. printf("请输入你想标记的坐标行号:");
    10. scanf("%d", &a);
    11. printf("请输入你想标记的坐标列号:");
    12. scanf("%d", &b);
    13. if (a >= 1 && a <= row && b >= 1 && b <= col)
    14. {
    15. x = a;
    16. y = b;
    17. break;
    18. }
    19. else
    20. {
    21. printf("您选择的坐标有误,请重新选择!\n");
    22. }
    23. }
    24. while (1)
    25. {
    26. printf("1 - 不确定是否为雷\n");
    27. printf("2 - 确定是雷\n");
    28. printf("3 - 取消标记\n");
    29. printf("4 - 取消操作\n");
    30. printf("请输入你想对该坐标进行的操作:");
    31. int c = 0;
    32. scanf("%d", &c);
    33. if (show[x][y] == '*')
    34. {
    35. if (c == 1)
    36. {
    37. c = 63;
    38. //符号?对应的ASCII码值为63
    39. }
    40. if (c == 2)
    41. {
    42. c = 35;
    43. //符号#对应的ASCII码值为35
    44. }
    45. if (c == 4)
    46. {
    47. printf("操作已取消,请重新选择!\n");
    48. DisplayBoard(show, row, col);
    49. break;
    50. }
    51. char ch = c;
    52. show[x][y] = ch;
    53. printf("标记成功!\n");
    54. DisplayBoard(show, row, col);
    55. break;
    56. }
    57. else if (show[x][y] == '?' || show[x][y] == '#' && c == 3)
    58. //如果输入3还要判断该元素是否已经被标记
    59. {
    60. show[x][y] = '*';
    61. printf("取消成功!\n");
    62. DisplayBoard(show, row, col);
    63. break;
    64. }
    65. if (c == 4)
    66. {
    67. printf("操作已取消,请重新选择!\n");
    68. break;
    69. }
    70. else
    71. {
    72. printf("输入位置不合法,请重新输入\n");
    73. }
    74. }
    75. }

            首先,我们让玩家按照一定格式输入想要进行标记的坐标,并对该坐标进行合理性确认。接着我们定义出一个整型变量C,用于接收玩家的标记操作输入选择,这个时候我们需要对玩家输入的操作选项C进行分类操作,确保进行正确的对应操作,同时对玩家的标记操作选择合理性也应当进行检查。

            这里我们需要注意,当我们进行取消操作时,会出现两种情况,即该位置上的符号可能为' # ',也可能不为' # ',所以我们在对选项4进行处理时,应当对各种情况均考虑到位

    3.剩余雷量显示:

            在上面的游戏中,我们便可以进行排雷和标记,则我们可以再度添加一个剩余雷量显示来辅助玩家进行游戏:

    1. else
    2. {
    3. int n = get_mine_count(mine, x, y);
    4. show[x][y] = n + '0';
    5. Cls(mine, show, row, col, x, y);
    6. int res = LeaveMine(show, row, col);
    7. printf("剩余雷量:%d\n", EASY_COUNT - res);
    8. DisplayBoard(show, ROW, COL);
    9. win++;
    10. break;
    11. }

            剩余雷量的计算则十分简单了,它的计算不是去计算真实的雷量,我们只需要采用遍历思想,计算展示数组show中被我们标记为“地雷”的表示符号' # '即可:

    1. int LeaveMine(char show[ROWS][COLS], int row, int col)
    2. {
    3. int i = 1;
    4. int num = 0;
    5. for (i = 1; i <= row; i++)
    6. {
    7. int j = 1;
    8. for (j = 1; j <= col; j++)
    9. {
    10. if (show[i][j] == '#')
    11. {
    12. num++;
    13. }
    14. }
    15. }
    16. return num;
    17. }

    4.胜利条件更新:

            细心的小伙伴们应该已经发现了,在我们进行清除展开时,在函数中同时处理了大量的坐标,而在这些坐标被操作时并没有被计算,最终导致原有的胜利条件无法进行计算,于是我们就需要一个新的胜利判断函数来帮助我们进行胜利条件判断。

            那么我们应该怎么进行判断呢?这里使用的方法是,在执行完每次循环中每个坐标操作后,进行胜利条件判断,死亡或胜利则破坏循环条件,终止循环,在终止循环时再对死亡或胜利进行区别处理即可:

    1. void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
    2. {
    3. printf("请输入您想要排查的坐标:\n");
    4. int x = 0;
    5. int y = 0;
    6. int win = 1;
    7. while (win)
    8. {
    9. PlayerJudge(show, row, col);
    10. printf("您想排查哪一行:");
    11. scanf("%d", &x);
    12. if (x >= 1 && x <= row)
    13. {
    14. while (1)
    15. {
    16. printf("您想排查哪一列:");
    17. scanf("%d", &y);
    18. if (y >= 1 && y <= col)
    19. {
    20. if (show[x][y] != '*')
    21. {
    22. printf("该坐标已被排查过!\n");
    23. DisplayBoard(show, ROW, COL);
    24. break;
    25. }
    26. if (mine[x][y] == '1')
    27. {
    28. DisplayBoard(mine, ROW, COL);
    29. printf("很遗憾,您死了!\n");
    30. win = 0;
    31. break;
    32. }
    33. else
    34. {
    35. int n = get_mine_count(mine, x, y);
    36. show[x][y] = n + '0';
    37. Cls(mine, show, row, col, x, y, &win);
    38. int res = LeaveMine(show, row, col);
    39. printf("剩余雷量:%d\n", EASY_COUNT - res);
    40. Really_win(show, row, col, &win);
    41. DisplayBoard(show, ROW, COL);
    42. break;
    43. }
    44. }
    45. else
    46. {
    47. printf("您输入的列数有误,请重新输入!\n");
    48. }
    49. }
    50. }
    51. else
    52. {
    53. printf("您输入的行数有误,请重新输入!\n");
    54. }
    55. }
    56. }

            我们对这个胜利判断函数进行定义,当我们排出的“地雷”的数量等于设置的“地雷”数量时,且没有空余格子时,破坏判断条件:

    1. void Really_win(char show[ROWS][COLS], int row, int col, int* win)
    2. {
    3. int count1 = 0;
    4. int count2 = 0;
    5. int i = 0;
    6. for (i = 1; i <= row; i++)
    7. {
    8. int j = 0;
    9. for (j = 1; j <= col; j++)
    10. {
    11. if (show[i][j] == '#')
    12. {
    13. count1++;
    14. }
    15. if (show[i][j] == '*')
    16. {
    17. count2++;
    18. }
    19. }
    20. }
    21. if (count1 == EASY_COUNT && count2 == 0)
    22. {
    23. *win = 0;
    24. }
    25. }

            接着我们最后再添加一个变量really_win,并在判断为胜利后改变其取值,再根据其取值来区别于游戏失败,反馈玩家胜利

            定义并初始化变量really_win:

    int really_win = 0;

            传入函数进行操作: 

    Really_win(show, row, col, &win, &really_win);

            函数进行操作

    1. void Really_win(char show[ROWS][COLS], int row, int col, int* win, int* really_win)
    2. {
    3. int count1 = 0;
    4. int count2 = 0;
    5. int i = 0;
    6. for (i = 1; i <= row; i++)
    7. {
    8. int j = 0;
    9. for (j = 1; j <= col; j++)
    10. {
    11. if (show[i][j] == '#')
    12. {
    13. count1++;
    14. }
    15. if (show[i][j] == '*')
    16. {
    17. count2++;
    18. }
    19. }
    20. }
    21. if (count1 == EASY_COUNT && count2 == 0)
    22. {
    23. *win = 0;
    24. *really_win = 1;
    25. }
    26. }

            最后在排雷步骤最后根据really_win的取值反馈给玩家:

    1. if (really_win == 1)
    2. {
    3. printf("恭喜您,扫雷成功!\n");
    4. }

            将其组合起来即为完整的功能实现:

    1. void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
    2. {
    3. printf("请输入您想要排查的坐标:\n");
    4. int x = 0;
    5. int y = 0;
    6. int win = 1;
    7. int really_win = 0;
    8. while (win)
    9. {
    10. PlayerJudge(show, row, col);
    11. printf("您想排查哪一行:");
    12. scanf("%d", &x);
    13. if (x >= 1 && x <= row)
    14. {
    15. while (1)
    16. {
    17. printf("您想排查哪一列:");
    18. scanf("%d", &y);
    19. if (y >= 1 && y <= col)
    20. {
    21. if (show[x][y] != '*')
    22. {
    23. printf("该坐标已被排查过!\n");
    24. DisplayBoard(show, ROW, COL);
    25. break;
    26. }
    27. if (mine[x][y] == '1')
    28. {
    29. DisplayBoard(mine, ROW, COL);
    30. printf("很遗憾,您死了!\n");
    31. win = 0;
    32. break;
    33. }
    34. else
    35. {
    36. int n = get_mine_count(mine, x, y);
    37. show[x][y] = n + '0';
    38. Cls(mine, show, row, col, x, y, &win);
    39. int res = LeaveMine(show, row, col);
    40. printf("剩余雷量:%d\n", EASY_COUNT - res);
    41. Really_win(show, row, col, &win, &really_win);
    42. DisplayBoard(show, ROW, COL);
    43. break;
    44. }
    45. }
    46. else
    47. {
    48. printf("您输入的列数有误,请重新输入!\n");
    49. }
    50. }
    51. }
    52. else
    53. {
    54. printf("您输入的行数有误,请重新输入!\n");
    55. }
    56. }
    57. if (really_win == 1)
    58. {
    59. printf("恭喜您,扫雷成功!\n");
    60. }
    61. }

    二、界面优化:

            同井字棋的界面优化一样,我们也使用同样的方式对扫雷游戏进行优化,使用的函数、头文件与井字棋游戏的界面优化完全一致,这里便不再作过多阐述。

    三、终极优化后最终全部代码:

    1.game.h:

    1. #pragma once
    2. #define ROW 9
    3. #define COL 9
    4. #define ROWS ROW+2
    5. #define COLS ROW+2
    6. #define EASY_COUNT 10
    7. #include
    8. #include
    9. #include
    10. #include
    11. void menu();
    12. void game();
    13. void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
    14. void DisplayBoard(char board[ROWS][COLS], int row, int col);
    15. void SetMine(char mine[ROWS][ROWS], int row, int col);
    16. void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
    17. int get_mine_count(char mine[ROWS][COLS], int x, int y);
    18. void Cls(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y, int* win);
    19. void Judge(char show[ROWS][COLS], int row, int col);
    20. void PlayerJudge(char show[ROWS][COLS], int row, int col);
    21. int LeaveMine(char show[ROWS][COLS], int row, int col);
    22. void Really_win(char show[ROWS][COLS], int row, int col, int* win, int* really_win);

    2.game.c:

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include"game.h"
    3. //打印菜单:
    4. void menu()
    5. {
    6. printf("******************************\n");
    7. printf("******************************\n");
    8. printf("***** 欢迎来到扫雷游戏 *****\n");
    9. printf("***** 1.play *****\n");
    10. printf("***** 0.exit *****\n");
    11. printf("******************************\n");
    12. printf("******************************\n");
    13. }
    14. //初始化棋盘:
    15. void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
    16. {
    17. int i = 0;
    18. for (i = 0; i < rows; i++)
    19. {
    20. int j = 0;
    21. for (j = 0; j < cols; j++)
    22. {
    23. board[i][j] = set;
    24. }
    25. }
    26. }
    27. //打印棋盘:
    28. void DisplayBoard(char board[ROWS][COLS], int row, int col)
    29. {
    30. printf(" --------扫雷--------\n");
    31. int i = 0;
    32. printf("| \\ 1 2 3 4 5 6 7 8 9 |\n");
    33. for (i = 1; i <= row; i++)
    34. {
    35. int j = 0;
    36. printf("| %d ", i);
    37. for (j = 1; j <= col; j++)
    38. {
    39. printf("%c ", board[i][j]);
    40. }
    41. printf("|\n");
    42. }
    43. printf(" --------扫雷--------\n");
    44. }
    45. //布置雷:
    46. void SetMine(char mine[ROWS][ROWS], int row, int col)
    47. {
    48. int count = EASY_COUNT;
    49. while (count)
    50. {
    51. //生成随机下标:
    52. int x = rand() % row + 1;
    53. int y = rand() % col + 1;
    54. //布置雷:
    55. if (mine[x][y] == '0')
    56. {
    57. mine[x][y] = '1';
    58. count--;
    59. }
    60. }
    61. }
    62. //统计雷:
    63. int get_mine_count(char mine[ROWS][COLS], int x, int y)
    64. {
    65. return (mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x - 1][y] + mine[x + 1][y] + mine[x - 1][y + 1] + mine[x][y + 1] + mine[x + 1][y + 1] - 8 * '0');
    66. }
    67. //清理展开:
    68. void Cls(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y, int* win)
    69. {
    70. if (show[x][y] == '0')
    71. //若此坐标内元素为'0',则进入此函数
    72. {
    73. show[x][y] = ' ';
    74. int i = x - 1;
    75. for (; i <= x + 1; i++)
    76. {
    77. int j = y - 1;
    78. for (; j <= y + 1; j++)
    79. {
    80. if (show[i][j] == ' ')
    81. {
    82. continue;
    83. }
    84. if (i >= 1 && i <= row && j >= 1 && j <= col)
    85. //检查坐标越界
    86. {
    87. if (show[i][j] != '*')
    88. //如果该元素不是'*',即已经被赋值,则无需进行下面的操作
    89. {
    90. continue;
    91. }
    92. int ret = get_mine_count(mine, i, j);
    93. show[i][j] = '0' + ret;
    94. *win++;
    95. Cls(mine, show, row, col, i, j, &win);
    96. //不断迭代,直到不再出现0或数字为止
    97. }
    98. }
    99. }
    100. }
    101. }
    102. //标记坐标:
    103. void Judge(char show[ROWS][COLS], int row, int col)
    104. {
    105. int x = 0;
    106. int y = 0;
    107. while (1)
    108. {
    109. int a = 0;
    110. int b = 0;
    111. printf("请输入你想标记的坐标行号:");
    112. scanf("%d", &a);
    113. printf("请输入你想标记的坐标列号:");
    114. scanf("%d", &b);
    115. if (a >= 1 && a <= row && b >= 1 && b <= col)
    116. {
    117. x = a;
    118. y = b;
    119. Sleep(500);
    120. break;
    121. }
    122. else
    123. {
    124. system("cls");
    125. printf("您选择的坐标有误,请重新选择!\n");
    126. Sleep(1000);
    127. system("cls");
    128. DisplayBoard(show, row, col);
    129. }
    130. }
    131. while (1)
    132. {
    133. system("cls");
    134. DisplayBoard(show, row, col);
    135. printf("1 - 不确定是否为雷\n");
    136. printf("2 - 确定是雷\n");
    137. printf("3 - 取消标记\n");
    138. printf("4 - 取消操作\n");
    139. printf("请输入你想对该坐标进行的操作:");
    140. int c = 0;
    141. scanf("%d", &c);
    142. Sleep(500);
    143. system("cls");
    144. if (show[x][y] == '*')
    145. {
    146. if (c == 1)
    147. {
    148. c = 63;
    149. //符号?对应的ASCII码值为63
    150. }
    151. if (c == 2)
    152. {
    153. c = 35;
    154. //符号#对应的ASCII码值为35
    155. }
    156. if (c == 4)
    157. {
    158. printf("操作已取消,请重新选择!\n");
    159. DisplayBoard(show, row, col);
    160. break;
    161. }
    162. char ch = c;
    163. show[x][y] = ch;
    164. printf("标记成功!\n");
    165. DisplayBoard(show, row, col);
    166. break;
    167. }
    168. else if (show[x][y] == '?' || show[x][y] == '#' && c == 3)
    169. //如果输入3还要判断该元素是否已经被标记
    170. {
    171. show[x][y] = '*';
    172. printf("取消成功!\n");
    173. DisplayBoard(show, row, col);
    174. break;
    175. }
    176. else if (c == 4)
    177. {
    178. printf("操作已取消,请重新选择!\n");
    179. Sleep(1000);
    180. system("cls");
    181. DisplayBoard(show, row, col);
    182. break;
    183. }
    184. else
    185. {
    186. printf("输入位置不合法,请重新输入\n");
    187. }
    188. }
    189. }
    190. //标记确认:
    191. void PlayerJudge(char show[ROWS][COLS], int row, int col)
    192. {
    193. int m = 0;
    194. do
    195. {
    196. printf("请问您是否想要标记某位置?\n");
    197. printf("是:1 否:0:");
    198. scanf("%d", &m);
    199. switch (m)
    200. {
    201. case 1:
    202. Sleep(500);
    203. system("cls");
    204. DisplayBoard(show, ROW, COL);
    205. Judge(show, row, col);
    206. break;
    207. case 0:
    208. Sleep(1000);
    209. system("cls");
    210. DisplayBoard(show, ROW, COL);
    211. break;
    212. default:
    213. system("cls");
    214. printf("输入有误,请重新输入:\n");
    215. Sleep(1000);
    216. system("cls");
    217. DisplayBoard(show, ROW, COL);
    218. break;
    219. }
    220. } while (m);
    221. }
    222. //剩余雷量计算:
    223. int LeaveMine(char show[ROWS][COLS], int row, int col)
    224. {
    225. int i = 1;
    226. int num = 0;
    227. for (i = 1; i <= row; i++)
    228. {
    229. int j = 1;
    230. for (j = 1; j <= col; j++)
    231. {
    232. if (show[i][j] == '#')
    233. {
    234. num++;
    235. }
    236. }
    237. }
    238. return num;
    239. }
    240. //胜利判定:
    241. void Really_win(char show[ROWS][COLS], int row, int col, int* win, int* really_win)
    242. {
    243. int count1 = 0;
    244. int count2 = 0;
    245. int i = 0;
    246. for (i = 1; i <= row; i++)
    247. {
    248. int j = 0;
    249. for (j = 1; j <= col; j++)
    250. {
    251. if (show[i][j] == '#')
    252. {
    253. count1++;
    254. }
    255. if (show[i][j] == '*')
    256. {
    257. count2++;
    258. }
    259. }
    260. }
    261. if (count1 == EASY_COUNT && count2 == 0)
    262. {
    263. *win = 0;
    264. *really_win = 1;
    265. }
    266. }
    267. //排雷:
    268. void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
    269. {
    270. printf("请输入您想要排查的坐标:\n");
    271. int x = 0;
    272. int y = 0;
    273. int win = 1;
    274. int really_win = 0;
    275. while (win)
    276. {
    277. PlayerJudge(show, row, col);
    278. printf("您想排查哪一行:");
    279. scanf("%d", &x);
    280. if (x >= 1 && x <= row)
    281. {
    282. while (1)
    283. {
    284. printf("您想排查哪一列:");
    285. scanf("%d", &y);
    286. if (y >= 1 && y <= col)
    287. {
    288. if (show[x][y] != '*')
    289. {
    290. system("cls");
    291. printf("该坐标已被排查过!\n");
    292. Sleep(1000);
    293. system("cls");
    294. DisplayBoard(show, ROW, COL);
    295. break;
    296. }
    297. if (mine[x][y] == '1')
    298. {
    299. system("cls");
    300. DisplayBoard(mine, ROW, COL);
    301. printf("很遗憾,您死了!\n");
    302. win = 0;
    303. Sleep(1000);
    304. system("cls");
    305. break;
    306. }
    307. else
    308. {
    309. int n = get_mine_count(mine, x, y);
    310. show[x][y] = n + '0';
    311. Cls(mine, show, row, col, x, y, &win);
    312. int res = LeaveMine(show, row, col);
    313. printf("剩余雷量:%d\n", EASY_COUNT - res);
    314. Sleep(1000);
    315. system("cls");
    316. Really_win(show, row, col, &win, &really_win);
    317. DisplayBoard(show, ROW, COL);
    318. break;
    319. }
    320. }
    321. else
    322. {
    323. printf("您输入的列数有误,请重新输入!\n");
    324. }
    325. }
    326. }
    327. else
    328. {
    329. printf("您输入的行数有误,请重新输入!\n");
    330. }
    331. }
    332. if (really_win == 1)
    333. {
    334. system("cls");
    335. printf("恭喜您,扫雷成功!\n");
    336. Sleep(1000);
    337. system("cls");
    338. }
    339. }

    3.test.c:

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include"game.h"
    3. //游戏逻辑:
    4. void game()
    5. {
    6. char mine[ROWS][COLS] = { 0 };
    7. //存放布置的雷的信息
    8. char show[ROWS][COLS] = { 0 };
    9. //存放查出的雷的信息
    10. //初始化棋盘
    11. InitBoard(mine, ROWS, COLS, '0');
    12. InitBoard(show, ROWS, COLS, '*');
    13. //打印棋盘
    14. DisplayBoard(show, ROW, COL);
    15. //布置雷
    16. SetMine(mine, ROW, COL);
    17. //扫雷
    18. FindMine(mine, show, ROW, COL);
    19. }
    20. void test()
    21. {
    22. srand((unsigned int)time(NULL));
    23. int input = 0;
    24. do
    25. {
    26. menu();
    27. printf("请选择:");
    28. scanf("%d", &input);
    29. switch (input)
    30. {
    31. case 1:
    32. Sleep(500);
    33. system("cls");
    34. game();
    35. break;
    36. case 0:
    37. printf("退出游戏...\n");
    38. Sleep(1000);
    39. break;
    40. default:
    41. system("cls");
    42. printf("输入错误,请重新选择!\n");
    43. Sleep(1000);
    44. system("cls");
    45. break;
    46. }
    47. } while (input);
    48. }
    49. int main()
    50. {
    51. test();
    52. return 0;
    53. }

    四、总结:

            当我们完成以上优化后,一个扫雷游戏程序的雏形就基本完成了,我们对其功能和界面都进行了相当程度的优化。但是对于一个程序的全部优化过程来说,可远远不止如此,在以后的学习过程中,我们会学到更多知识,到时候我们还可以使用更高深的知识来对我们的游戏程序进行更深层次的优化

            到这里我们对扫雷游戏的优化就宣告结束啦,能熟练灵活的掌握、运用我们学过的知识,才是我们的最终目的。懒惰不会让你一下子跌倒,但会在不知不觉中减少你的收获;勤奋也不会让你一夜成功,但会在不知不觉中积累你的成果!

            新人初来乍到,辛苦各位小伙伴们动动小手,三连走一走 ~ ~ ~  最后,本文仍有许多不足之处,欢迎各位看官老爷随时私信批评指正!

  • 相关阅读:
    数据结构与算法———力扣977——双指针——python
    [Python]多任务编程--线程
    前后端分离的后台管理系统源码,快速开发OA、CMS网站后台管理、毕业设计项目
    17.Table Api基础概念讲解
    数据分析 - 分散性与变异的量度
    【网络安全】使用meterpreter进行远控、Mysql注入、反弹型XSS攻防
    OpenAI视频生成Sora技术简析
    百度大模型算法实习岗上岸经验!
    【ros】解决protobuf的安装问题
    Git常用命令汇总
  • 原文地址:https://blog.csdn.net/weixin_59392935/article/details/127985581