🌈write in front🌈
🧸大家好,我是Aileen🧸.希望你看完之后,能对你有所帮助,不足请指正!共同学习交流.
🆔本文由Aileen_0v0🧸 原创 CSDN首发🐒 如需转载还请通知⚠️
📝个人主页:Aileen_0v0🧸—CSDN博客
🎁欢迎各位→点赞👍 + 收藏⭐️ + 留言📝
📣系列专栏:Aileen_0v0🧸的C语言学习系列专栏——CSDN博客
🗼我的格言:"没有罗马,那就自己创造罗马~"
目录
• 使用控制台实现经典的扫雷游戏
初始界面
排雷界面
排雷失败界面
但是如果我们判断边缘的格子位置是否含雷时,
由于周围边界没有东西,导致我们需要判断这个格子是否位于边缘位置,这就会让代码变的复杂~
于是,我们可以通过 在原来9 * 9 的方格的 上下 , 左右位置 放没有雷的空格子(如上面左右两个数组的橙色边界)
根据刚刚的分析,
我们在左边创建一个mine数组 布置好雷的信息,全部初始化成 字符"0" ~
雷 - "1"
非雷 - "0"
在右边创建一个show数组放置 排查处的雷的信息, 最初未排查时,都放 *
没有排查 - "*"
排查 - 数字字符
小细节:之所以都用 字符数组 是因为 只需要定义字符函数, 方便操作~
如果 左边是整形数组,右边是字符数组 就 需要调用两个不同的函数~
在game.c中打印棋盘的时候,我们只打印9*9的~
因为外边的绿色空格只是为了编写变得容易一点,不会越界~~
但是,按照这种方式打印,我们很难知道是第几行第几列,因为没有标识
于是我们,再利用 for 循环打印出 行和列的序号
利用库函数rand 来随机布置雷
要从1到n中随机取一个数的公式是:
rand()%n+1.
解释一下:
1、库函数rand()会返回一个大于0的随机整数;
2、rand()%n,对这个返回的随机整数除以n取余,结果是一个0到n-1的随机整数3、rand()%n+1,将rand()%n的结果加上1,就可得到一个1到n的随机整数;
更通用一点的公式,产生m到n中(n>m)的一个随机数的公式是:
rand()%(n-m+1)+m。
首先,先创建这三个文件.
- #define _CRT_SECURE_NO_WARNINGS
- #include
-
- //扫雷页面都实现
- void menu()
- {
- printf("*********************************\n");
- printf("****** 1. play *****\n");
- printf("****** 0. exit *****\n");
- printf("*********************************\n");
- }
-
-
- int main()
- {
- int input = 0;
- do
- {
- menu();
- printf("请选择:<");
- scanf("%d", &input);
- switch (input)
- {
- case 1:
- printf("扫雷\n");
- break;
- case 0:
- printf("退出游戏\n");
- break;
- default:
- printf("选择错误,请重新选择\n");
- break;
-
- }
- //case 1 和 default 都是非零
- //里层switch语句走完 就会走while语句
- } while (input);
- return 0;
- //如果while 后面为0,程序就会自动退出游戏
- //非0 1就打印扫雷 其他值则重新打印菜单让用户选择
-
- }
运行效果:
头文件game.h 的代码:
- #define _CRT_SECURE_NO_WARNINGS
- #include
- #pragma once
-
- #define ROW 9
- #define COL 9
-
- #define ROWS ROW + 2
- #define COLS COL + 2
-
- // 函数的声明
-
- //初始化棋盘
- void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
-
- //打印棋盘的
- void DisplayBoard(char board[ROWS][COLS],int rows, int cols);
源文件game.c 的代码:
- #define _CRT_SECURE_NO_WARNINGS
-
- #include "game.h"
-
- void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
- {
- int i = 0;
- for (i = 0; i < rows; i++)
- {
- int j = 0;
- for (j = 0; j < cols; j++)
- {
- board[i][j] = set;
- }
- }
- }
-
- //打印棋盘,就是打印数组
- void DisplayBoard(char board[ROWS][COLS], int row, int col)
- {
- int i = 0;
- for (i = 1; i <= row; i++)
- {
- int j = 0;
- for (j = 1; j <= col; j++)
- {
- printf("%c ", board[i][j]);
- }
- printf("\n");
- }
- }
源文件 test.c 的代码:
- #define _CRT_SECURE_NO_WARNINGS
- #include
- #include "game.h"
-
- //扫雷页面都实现
- void menu()
- {
- printf("*********************************\n");
- printf("****** 1. play *****\n");
- printf("****** 0. exit *****\n");
- printf("*********************************\n");
- }
-
- void game()
- {
- //数组
- char mine[ROWS][COLS];//"0"
- char show[ROWS][COLS];//"*"
- InitBoard(mine, ROWS, COLS,'0');
- InitBoard(show, ROWS, COLS,'*');
- //棋盘打印
- DisplayBoard(mine, ROW, COL);
- DisplayBoard(show, ROW, COL);
- //布置雷
- //排查雷
-
- }
-
-
- int main()
- {
- int input = 0;
- do
- {
- menu();
- printf("请选择:<");
- scanf_s("%d", &input);
- switch (input)
- {
- case 1:
- printf("扫雷\n");
- game();//游戏代码模块化
- break;
- case 0:
- printf("退出游戏\n");
- break;
- default:
- printf("选择错误,请重新选择\n");
- break;
-
- }
- //case 1 和 default 都是非零
- //里层switch语句走完 就会走while语句
- } while (input);
- return 0;
- //如果while 后面为0,程序就会自动退出游戏
- //非0 1就打印扫雷 其他值则重新打印菜单让用户选择
-
- }
打印结果:
在原来打印棋盘上加上坐标,进行定位,只需修改game.c部分的代码:
- #define _CRT_SECURE_NO_WARNINGS
-
- #include "game.h"
-
- void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
- {
- int i = 0;
- for (i = 0; i < rows; i++)
- {
- int j = 0;
- for (j = 0; j < cols; j++)
- {
- board[i][j] = set;
- }
- }
- }
-
- //打印棋盘,就是打印数组
- void DisplayBoard(char board[ROWS][COLS], int row, int col)
- {
- int i = 0;
- printf("-----------扫雷游戏-----------\n");
- //打印棋盘序号
- for (i = 0; i <= row; i++)
- {
- printf("%d ", i);
- }
- printf("\n");
- //打印9*9的棋盘
- for (i = 1; i <= row; i++)
- {
- printf("%d ", i);
- int j = 0;
- for (j = 1; j <= col; j++)
- {
- printf("%c ", board[i][j]);
- }
- printf("\n");
- }
- printf("-----------扫雷游戏-----------\n");
- }
打印结果:
game.h:
- #define _CRT_SECURE_NO_WARNINGS
- #include
- #include
//标准库头文件 - #include
-
- //布置雷
- void SetMine(char mine[ROWS][COLS],int row , int col);
- //虽然布置雷是在9*9的格子里面布置--->row 和 col
- //但是,数组传参还是11*11的格子,即ROWS和COLS
game.c:
-
- //布置雷
- void SetMine(char mine[ROWS][COLS], int row, int col)
- {
- int count = EASY_COUNT;
-
- while (count)
- {
- int x = rand() % row + 1;
- int y = rand() % col + 1;
- if (mine[x][y] == '0')//如果该位置无雷才在这个位置放雷
- {
- mine[x][y] = '1';
- count--;
- }
- }
-
-
- }
test.c文件:
- //布置雷
- SetMine(mine, ROW, COL);
- DisplayBoard(mine, ROW, COL);
打印结果:
通过观察,ASCII表可知:
字符'0'-->ASCII值:48
字符'1'-->ASCII值:49
字符'2'-->ASCII值:50
字符'3'-->ASCII值:51
依此类推
得出规律:'1' - '0' = 49-48 = 1
'3' - '0' = 51-48 = 3
字符-字符=数字 ----------> 反推: 数字+字符=字符
统计 x y 周围有几个雷 --->
把其周围的字符值'0'和'1'加起来即可
然后减去8个字符'0' 得到数字,去代替 x y 处的 '*'.
game.h:
- //排查雷
- void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
game.c:
-
- //实现GetMineCount数组
- static int GetMineCount(char mine[ROWS][COLS], int x, int y)
- {
- return(mine[x - 1][y] +
- mine[x - 1][y - 1] +
- mine[x][y - 1] +
- mine[x + 1][y - 1] +
- mine[x + 1][y] +
- mine[x + 1][y + 1] +
- mine[x][y + 1] +
- mine[x - 1][y + 1] - 8 * '0');
- }
- //排查雷
- void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
- {
- int x = 0;
- int y = 0;
- while (1)//死循环排雷
- {
- printf("请输入要排查的坐标:>");
- scanf("%d %d", &x, &y);
- //注意:x y 要在有效的排查范围(9*9)之内
- if (x >= 1 && x <= row && y >= 1 && y <= col)
- {
- //开始排查是否是雷
- if (mine[x][y] == '1')
- {
- printf("很遗憾,你被炸死了\n");
- DisplayBoard(mine, ROW, COL);
- break;
- }
- else
- {
- int count = GetMineCount(mine, x, y);
- show[x][y] = count + '0';
- DisplayBoard(show, ROW, COL);
- //count + 字符'0;变成对应的数字字符放到show数组里
- }
-
- }
- else
- {
- printf("坐标非法,重新输入\n");
- }
- }
-
- }
test.c:
- //排查雷
- FindMine(mine, show, ROW, COL);
注意:GetMineCount 没有在其它文件中声明是因为,我们只希望它在game.c处悄悄使用它,所以前加static
运行结果:
上面的排雷,未限制排雷次数,即可无限循环下去,这样子的游戏设计显然不合理~
于是,我们可以根据,雷和非雷的数量关系进行排雷循环次数的限制.
game.h:
- //布置80个雷
- #define EASY_COUNT 80
game.c:
- //排查雷
- void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
- {
- int x = 0;
- int y = 0;
- int win = 0;
- while (win
//根据雷和非雷的数量关系限制循环次数
- {
- printf("请输入要排查的坐标:>");
- scanf("%d %d", &x, &y);
- //注意:x y 要在有效的排查范围(9*9)之内
- if (x >= 1 && x <= row && y >= 1 && y <= col)
- {
- //开始排查是否是雷
- if (mine[x][y] == '1')
- {
- printf("很遗憾,你被炸死了\n");
- DisplayBoard(mine, ROW, COL);
- break;
- }
- else
- {
- int count = GetMineCount(mine, x, y);
- show[x][y] = count + '0';
- DisplayBoard(show, ROW, COL);
- //count + 字符'0;变成对应的数字字符放到show数组里
- win++;
- }
-
- }
- else
- {
- printf("坐标非法,重新输入\n");
- }
- }
- if (win == row * col - EASY_COUNT)
- {
- printf("恭喜你,扫雷成功\n");
- DisplayBoard(mine, ROW, COL);
- }
-
- }
我们可以通过改变雷的个数,然后根据mine的数组打印的结果对照着进行排雷成功的结果输出,检查是否有误.
运行结果:
game.h:
- #define _CRT_SECURE_NO_WARNINGS
- #include
- #include
//标准库头文件 - #include
- #pragma once
-
- #define ROW 9
- #define COL 9
-
- #define ROWS ROW + 2
- #define COLS COL + 2
-
- //布置10个雷
- #define EASY_COUNT 10
-
-
-
- // 函数的声明
-
- //初始化棋盘
- void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
-
- //打印棋盘的
- void DisplayBoard(char board[ROWS][COLS],int rows, int cols);
game.c:
- #define _CRT_SECURE_NO_WARNINGS
-
- #include "game.h"
- #include
-
- void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
- {
- int i = 0;
- for (i = 0; i < rows; i++)
- {
- int j = 0;
- for (j = 0; j < cols; j++)
- {
- board[i][j] = set;
- }
- }
- }
-
- //打印棋盘,就是打印数组
- void DisplayBoard(char board[ROWS][COLS], int row, int col)
- {
- int i = 0;
- printf("-----------扫雷游戏-----------\n");
- //打印棋盘序号
- for (i = 0; i <= row; i++)
- {
- printf("%d ", i);
- }
- printf("\n");
- //打印9*9的棋盘
- for (i = 1; i <= row; i++)
- {
- printf("%d ", i);
- int j = 0;
- for (j = 1; j <= col; j++)
- {
- printf("%c ", board[i][j]);
- }
- printf("\n");
- }
- printf("-----------扫雷游戏-----------\n");
- }
-
- //布置雷
- void SetMine(char mine[ROWS][COLS], int row, int col)
- {
- int count = EASY_COUNT;
-
- while (count)
- {
- int x = rand() % row + 1;
- int y = rand() % col + 1;
- if (mine[x][y] == '0')//如果该位置无雷才在这个位置放雷
- {
- mine[x][y] = '1';
- count--;
- }
- }
-
-
- }
-
- //实现GetMineCount数组
- static int GetMineCount(char mine[ROWS][COLS], int x, int y)
- {
- return(mine[x - 1][y] +
- mine[x - 1][y - 1] +
- mine[x][y - 1] +
- mine[x + 1][y - 1] +
- mine[x + 1][y] +
- mine[x + 1][y + 1] +
- mine[x][y + 1] +
- mine[x - 1][y + 1] - 8 * '0');
- }
- //排查雷
- void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
- {
- int x = 0;
- int y = 0;
- int win = 0;
- while (win
//根据雷和非雷的数量关系限制循环次数
- {
- printf("请输入要排查的坐标:>");
- scanf("%d %d", &x, &y);
- //注意:x y 要在有效的排查范围(9*9)之内
- if (x >= 1 && x <= row && y >= 1 && y <= col)
- {
- //开始排查是否是雷
- if (mine[x][y] == '1')
- {
- printf("很遗憾,你被炸死了\n");
- DisplayBoard(mine, ROW, COL);
- break;
- }
- else
- {
- int count = GetMineCount(mine, x, y);
- show[x][y] = count + '0';
- DisplayBoard(show, ROW, COL);
- //count + 字符'0;变成对应的数字字符放到show数组里
- win++;
- }
-
- }
- else
- {
- printf("坐标非法,重新输入\n");
- }
- }
- if (win == row * col - EASY_COUNT)
- {
- printf("恭喜你,扫雷成功\n");
- DisplayBoard(mine, ROW, COL);
- }
-
- }
test.c:
- #define _CRT_SECURE_NO_WARNINGS
- #include
- #include "game.h"
-
- //扫雷页面都实现
- void menu()
- {
- printf("*********************************\n");
- printf("****** 1. play *****\n");
- printf("****** 0. exit *****\n");
- printf("*********************************\n");
- }
-
- void game()
- {
- //数组
- char mine[ROWS][COLS];//"0"
- char show[ROWS][COLS];//"*"
- InitBoard(mine, ROWS, COLS,'0');
- InitBoard(show, ROWS, COLS,'*');
- //棋盘打印
- //DisplayBoard(mine, ROW, COL); 雷的位置注释掉不打印出来,保持神秘感
- DisplayBoard(show, ROW, COL);
- //布置雷
- SetMine(mine, ROW, COL);
- //DisplayBoard(mine, ROW, COL);
- //排查雷
- FindMine(mine, show, ROW, COL);
-
- }
-
-
- int main()
- {
- int input = 0;
- srand((unsigned int)time(NULL));//强制转换成无符号整型
- do
- {
- menu();
- printf("请选择:<");
- scanf_s("%d", &input);
- switch (input)
- {
- case 1:
- printf("扫雷\n");
- game();//游戏代码模块化
- break;
- case 0:
- printf("退出游戏\n");
- break;
- default:
- printf("选择错误,请重新选择\n");
- break;
-
- }
- //case 1 和 default 都是非零
- //里层switch语句走完 就会走while语句
- } while (input);
- return 0;
- //如果while 后面为0,程序就会自动退出游戏
- //非0 1就打印扫雷 其他值则重新打印菜单让用户选择
-
- }
🌻今天的扫雷游戏就分享到这里啦~🌻
🌻喜欢就一键三连支持一下吧♥~🌻
🌻附上今天的日落图☺️🌻
🌻谢谢家人们!🌻