C语言扫雷
1. 扫雷游戏分析和设计
1.1 扫雷游戏的功能说明
- 使⽤控制台实现经典的扫雷游戏
- 游戏可以通过菜单实现继续玩或者退出游戏
- 扫雷的棋盘是9 * 9的格⼦
- 默认随机布置10个雷
- 可以排查雷
- 如果位置不是雷,就显⽰周围有⼏个雷
- 如果位置是雷,就炸死游戏结束
- 把除10个雷之外的所有雷都找出来,排雷成功,游戏结束
1.2 游戏的分析和设计
1.2.1 数据结构的分析
扫雷的过程中,布置的雷和排查出的雷的信息都需要存储,所以我们需要⼀定的数据结构来存储这些信息。
因为我们需要在9 * 9的棋盘上布置雷的信息和排查雷,我们⾸先想到的就是创建⼀个9 * 9的数组来存放信息。
那如果这个位置布置雷,我们就存放1,没有布置雷就存放0.
假设我们排查(2,5)这个坐标时,我们访问周围的⼀圈8个⻩⾊位置,统计周围雷的个数是1
假设我们排查(8,6)这个坐标时,我们访问周围的⼀圈8个⻩⾊位置,统计周围雷的个数时,最下⾯的三个坐标就会越界,为了防⽌越界,我们在设计的时候,给数组扩⼤⼀圈,雷还是布置在中间的9 * 9的坐标上,周围⼀圈不去布置雷就⾏,这样就解决了越界的问题。所以我们将存放数据的数组创建成11 * 11是⽐较合适。
再继续分析,我们在棋盘上布置了雷,棋盘上雷的信息(1)和⾮雷的信息(0),假设我们排查了某⼀个位置后,这个坐标处不是雷,这个坐标的周围有1个雷,那我们需要将排查出的雷的数量信息记录存储,并打印出来,作为排雷的重要参考信息的。那这个雷的个数信息存放在哪⾥呢?如果存放在布置雷的数组中,这样雷的信息和雷的个数信息就可能或产⽣混淆和打印上的困难。
这⾥我们肯定有办法解决,⽐如:雷和⾮雷的信息不要使⽤数字,使⽤某些字符就⾏,这样就避免冲突了,但是这样做棋盘上有雷和⾮雷的信息,还有排查出的雷的个数信息,就⽐较混杂,不够⽅便。
这⾥我们采⽤另外⼀种⽅案,我们专⻔给⼀个棋盘(对应⼀个数组mine)存放布置好的雷的信息,再给另外⼀个棋盘(对应另外⼀个数组show)存放排查出的雷的信息。这样就互不⼲扰了,把雷布置到mine数组,在mine数组中排查雷,排查出的数据存放在show数组,并且打印show数组的信息给后期排查参考。
同时为了保持神秘,show数组开始时初始化为字符 ‘*’,为了保持两个数组的类型⼀致,可以使⽤同⼀套函数处理,mine数组最开始也初始化为字符’0’,布置雷改成’1’。如下如:
对应的数组应该是:
char mine[11][11] = {0};//⽤来存放布置好的雷的信息
char show[11][11] = {0};//⽤来存放排查出的雷的个数信息
1.2.2 ⽂件结构设计
我们设计三个⽂件:
test.c //⽂件中写游戏的测试逻辑
game.c //⽂件中写游戏中函数的实现等
game.h //⽂件中写游戏需要的数据类型和函数声明等
我们先实现一下游戏的整体框架
#include <stdio.h>
void menu()
{
printf("****************************\n");
printf("****** 1. paly ******\n");
printf("****** 0. exit ******\n");
printf("****************************\n");
}
int main()
{
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("扫雷游戏\n");
break;
case 0:
printf("退出游戏\n");
break;
default :
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
运行结果如图:
我们把游戏的实现封装成一个game函数,要实现这个扫雷游戏我们需要两个数组
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS ROW + 2
void game()
{
//定义两个数组
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//实现扫雷游戏的功能
//...
}
之后我们要写一个函数,初始化这两个数组
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)
{
printf("-----扫雷游戏-----\n");
int i = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
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");
}
}
运行结果如图:
之后我们就可以设置10个雷了
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = MAX_Mine;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
结果如图:
我们把雷设置好了之后就要开始排雷了
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
printf("请输入你要排查的坐标:>");
scanf("%d %d", &x, &y);
int n = 0;
if ((x >= 1 && x <= 9) && (y >= 1 && y <= 9))
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(show, row, col);
break;
}
else
{
//不是雷就统计该坐标周围有几个雷
//...
}
}
else
{
printf("该坐标已被排查,请重新选择\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
这里我们写一个函数统计要排查的坐标周围有几个雷
static int GetMineCount(char board[ROWS][COLS], int x, int y)
{
int count = 0;
int i = 0;
for (i = -1; i <= 1; i++)
{
int j = 0;
for (j = -1; j <= 1; j++)
{
if (board[x + i][y + j] == '1')
{
count++;
}
}
}
return count;
}
2. 扫雷的三个版本实现
2.1 方案一
我们可以把排雷的全过程,并且游戏胜利的判定全都放在FindMine函数里面实现,如果我们希望实现这个功能那么那么我们肯定布置排除一次雷所以我们需要写一个循环来实现多次排查,我们如果想实现游戏胜利的判断可参考上一篇猜数字游戏的实现,我们可以定义一个变量win每排查一次,win就增加一个,直到win的数量等于非雷的个数,就把所以非雷的位置排查完了,游戏就胜利了。
#define MAX_Mine 10
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 < row * col - MAX_Mine)
{
printf("请输入你要排查的坐标:>");
scanf("%d %d", &x, &y);
int n = 0;
if ((x >= 1 && x <= 9) && (y >= 1 && y <= 9))
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
int n = GetMineCount(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("该坐标已被排查,请重新选择\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - MAX_Mine)
{
printf("恭喜你,游戏胜利\n");
}
}
运行结果:
全部代码实现:
//game.h
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS ROW + 2
#define MAX_Mine 10
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//设置雷
void SetMine(char board[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);
//判断输赢
int IsWin(char board[ROWS][COLS], int row, int col);
//game.c
#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)
{
printf("-----扫雷游戏-----\n");
int i = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
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");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = MAX_Mine;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
//printf("x = %d y = %d\n", x, y);
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
static int GetMineCount(char board[ROWS][COLS], int x, int y)
{
int count = 0;
int i = 0;
for (i = -1; i <= 1; i++)
{
int j = 0;
for (j = -1; j <= 1; j++)
{
if (board[x + i][y + j] == '1')
{
count++;
}
}
}
return count;
}
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 < row * col - MAX_Mine)
{
printf("请输入你要排查的坐标:>");
scanf("%d %d", &x, &y);
int n = 0;
if ((x >= 1 && x <= 9) && (y >= 1 && y <= 9))
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
system("cls");//清空屏幕
int n = GetMineCount(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("该坐标已被排查,请重新选择\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - MAX_Mine)
{
printf("恭喜你,游戏胜利\n");
}
}
//test.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h";
void menu()
{
printf("****************************\n");
printf("****** 1. paly ******\n");
printf("****** 0. exit ******\n");
printf("****************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
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()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default :
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
2.2 方案二
我们也可以写一个IsWin函数判断游戏是否胜利,每次排查后就通过这个函数判断一次,
int IsWin(char board[ROWS][COLS], int row, int col)
{
int count = 0;
int i = 0;
for (i = 1; i <= row; i++)
{
int j = 0;
for (j = 1; j <= col; j++)
{
if (board[i][j] == '*')
{
count++;
}
}
}
return count;
}
这里只需要修改FindMine函数,并把IsWin函数的代码放在FindMine上面即可
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);
int n = 0;
if ((x >= 1 && x <= 9) && (y >= 1 && y <= 9))
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
//system("cls");
int n = GetMineCount(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
if (IsWin(show, ROW, COL) == MAX_Mine)
{
printf("恭喜你,游戏胜利\n");
break;
}
}
}
else
{
printf("该坐标已被排查,请重新选择\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
2.3 方案三
我们可让FindMine只实现一次排雷的过程,然后在外面写一个循环来实现多次排雷,并判断游戏是否胜利
这里要修改的地方比较多我们重新在写一个代码实现
//game.h
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS ROW + 2
#define MAX_Mine 10
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//设置雷
void SetMine(char board[ROWS][COLS], int row, int col);
//排查雷
int FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//判断输赢
int IsWin(char board[ROWS][COLS], int row, int col);
//game.c
#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)
{
printf("-----扫雷游戏-----\n");
int i = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
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");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = MAX_Mine;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
//printf("x = %d y = %d\n", x, y);
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
static int GetMineCount(char board[ROWS][COLS], int x, int y)
{
int count = 0;
int i = 0;
for (i = -1; i <= 1; i++)
{
int j = 0;
for (j = -1; j <= 1; j++)
{
if (board[x + i][y + j] == '1')
{
count++;
}
}
}
return count;
}
int IsWin(char board[ROWS][COLS], int row, int col)
{
int count = 0;
int i = 0;
for (i = 1; i <= row; i++)
{
int j = 0;
for (j = 1; j <= col; j++)
{
if (board[i][j] == '*')
{
count++;
}
}
}
return count;
}
int 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);
int n = 0;
if ((x >= 1 && x <= 9) && (y >= 1 && y <= 9))
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, row, col);
return 0;
}
else
{
system("cls");
int n = GetMineCount(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
return 1;
}
}
else
{
printf("该坐标已被排查,请重新选择\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
}
//test.c
#include "game.h";
void menu()
{
printf("****************************\n");
printf("****** 1. paly ******\n");
printf("****** 0. exit ******\n");
printf("****************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
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);
while (1)
{
if (FindMine(mine, show, ROW, COL) == 0)
{
break;
}
if (IsWin(show, ROW, COL) == MAX_Mine)
{
printf("恭喜你,游戏胜利\n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default :
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
代码功能测试,假如我们设置79个雷去排查