一、游戏界面
扫雷的棋盘是9*9的,随机布置10个雷,输入排查的的标,会显示周围8个区域雷的总数,踩到雷则会退出游戏,需要把10个雷都找出来,游戏才能结束。
二、设计分析
本次设计需要在9*9的棋盘上布置雷的信息和排查雷,为了方便利用两个二维数组,mine和show,mine用于存放随机布置的雷,初始为字符‘0’,布置雷用字符‘1’表示。通过show来显示排查雷的信息,初始化为‘*’,选择排查坐标后显示周围雷的个数,在统计坐标周围的雷的个数,可能会出线数组越界的问题,因此扩大该数组,扩建成11*11的数组。
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
2 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | |
3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
4 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | |
5 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | |
6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
7 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | |
8 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
9 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | |
10 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
1 | * | * | * | * | * | * | * | * | * | |
2 | * | * | * | * | * | * | * | * | * | |
3 | * | * | * | * | * | * | * | * | * | |
4 | * | * | * | * | * | * | * | * | * | |
5 | * | * | * | * | * | * | * | * | * | |
6 | * | * | * | 3 | * | * | * | * | * | |
7 | * | * | * | * | * | * | * | * | * | |
8 | * | * | * | * | * | * | * | * | * | |
9 | * | * | * | * | * | 1 | * | * | * | |
10 |
三、代码实现
分为三个文件,test.c、game.c和game.h。test.c写游戏的测试逻辑,game.c写对函数的定义,game.h写对函数的声明以及写所需的数据类型。
在main函数中写出函数的框架,根据输入的input,继而进行接下去的游戏操作。
int main()
{
int input = 0;
//srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏退出\n");
break;
default:
printf("输入错误,重新选择\n");
break;
}
} while (input);
return 0;
}
menu()菜单函数
void menu()
{
printf("**************\n");
printf("****1.play****\n");
printf("****0.exit****\n");
printf("**************\n");
}
game()函数
定义二维数组的列和行
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
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);
DisplayBoard(show, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
①首先要初始化mine和show数组,mine初始化为'0',show初始化为'*'。具体代码如下。
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;
}
}
}
②布置雷SetMine()
为了产生1~9的随机数,rand()%9产生0~8,加上1即为1~9。
srand((unsigned int)time(NULL));
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
③打印二维数组DisplayBoard()
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");
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");
}
}
④统计雷的个数Get_mine_count()
(x-1,y-1) | (x-1,y) | (x-1,y-1) |
(x,y-1) | (x,y) | (x,y+1) |
(x+1,y-1) | (x+1,y) | (x+1,y+1) |
int Get_mine_count(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';
}
依据ASCII值,字符‘1’减字符 ‘0’为1,因此把周围八个坐标的字符相加减去0即为雷的个数。
⑤排查雷
根据输入的坐标(x,y),在布置好雷的mine的矩阵下,利用统计该坐标周围雷的个数,通过show显示出来。变量win是矩阵中不是雷的数量,只有全部找到,即当win==9*9-10时,才会排雷成功。
#define EASY_COUNT 10
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0, 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 (mine[x][y] == '1')
{
printf("Boom 你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
if (mine[x][y] == '0')
{
int count=Get_mine_count(mine,x,y);//统计该坐标周围雷的个数
show[x][y] = count+'0';
win++;
DisplayBoard(show, ROW, COL);
}
}
}
else
{
printf("坐标输入非法,重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
四、完整代码如下
test.c文件
#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] = { '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);
DisplayBoard(show, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏退出\n");
break;
default:
printf("输入错误,重新选择\n");
break;
}
} while (input);
return 0;
}
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)
{
int i = 0;
printf("-------扫雷------\n");
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 = EASY_COUNT;
while (count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 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, 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 (mine[x][y] == '1')
{
printf("Boom 你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
if (mine[x][y] == '0')
{
int count=Get_mine_count(mine,x,y);//统计该坐标周围雷的个数
show[x][y] = count+'0';
win++;
DisplayBoard(show, ROW, COL);
}
}
}
else
{
printf("坐标输入非法,重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
int Get_mine_count(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';
}
game.h文件
#include <stdio.h>
#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 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 Get_mine_count(char mine[ROWS][COLS],int x,int y);