对于扫雷游戏,大家应该都很熟悉吧,下面让我们来看看它是如何实现的。
目录
一、游戏规则及设计思想
二、各功能的代码实现
1.创建菜单
2、主函数的实现
3、创建棋盘并初始化
4、打印棋盘
5、布置雷的位置 (埋雷)
6、排查雷
三、代码汇总
1、game.h文件
2、game.c文件
3、test.c文件
扫雷游戏规则:给出一张棋盘,根据我们自己输入的坐标位置来进行排查,如果该位置是雷,则游戏结束;如果不是雷,但是该位置四周有雷,该位置则显示周围雷的个数,如果四周也没有雷,则显示附近一片区域的雷的情况(这里我们用递归实现);继续排查,直到排除所有的雷为止。
设计思想:
void menu()
{
printf("*****************************\n");
printf("********** 1. play **********\n");
printf("********** 0. exit **********\n");
printf("*****************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
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;
}
创建时使用两个二维数组:
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
char mine[ROWS][COLS];//存放布置好的雷
char show[ROWS][COLS];//存放排查出的雷的信息
棋盘的初始化:
void InitBoard(char board[ROWS][COLS], int row, int col, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf("------扫雷游戏------\n");
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
//布置10个雷
//生成随机坐标,布置雷
int count = EASY_COUNT;
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;
while (win < row * col - EASY_COUNT)
{
printf("请输入要排查的坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
UnFold(mine, show, row, col, x, y, &win);
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("该位置已被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,扫雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
在这一过程中要统计周围雷的个数,具体实现如下:
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]-8*'0');
}
用递归进行区域的扩展的函数实现:
void UnFold(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y, int* pwin)
{
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
int count = GetMineCount(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';//自己不是雷,周围也没有雷,把这个地方设成空格
(*pwin)++;
int i = 0, j = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (show[i][j] == '*')
{
UnFold(mine, show, row, col, i, j, pwin);
}
}
}
}
else
{
show[x][y] = count + '0';
(*pwin)++;
}
}
}
#include
#include
#include
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int row, int col, 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);
#include"game.h"
void InitBoard(char board[ROWS][COLS], int row, int col, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf("------扫雷游戏------\n");
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
//布置10个雷
//生成随机坐标,布置雷
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]-8*'0');
}
void UnFold(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y, int* pwin)
{
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
int count = GetMineCount(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';//自己不是雷,周围也没有雷,把这个地方设成空格
(*pwin)++;
int i = 0, j = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (show[i][j] == '*')
{
UnFold(mine, show, row, col, i, j, pwin);
}
}
}
}
else
{
show[x][y] = count + '0';
(*pwin)++;
}
}
}
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 - EASY_COUNT)
{
printf("请输入要排查的坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
UnFold(mine, show, row, col, x, y, &win);
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("该位置已被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,扫雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
#include"game.h"
void menu()
{
printf("*****************************\n");
printf("********** 1. play **********\n");
printf("********** 0. exit **********\n");
printf("*****************************\n");
}
void game()
{
char mine[ROWS][COLS];//存放布置好的雷
char show[ROWS][COLS];//存放排查出的雷的信息
//初始化棋盘
//mine数组一开始全为'0'
//show数组一开始全为'*'
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
DisplayBoard(show, ROW, COL);
//1、布置雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//2、排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
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;
}