在本例中,有两个二维数组,一个用来存储布置雷的信息,‘0’表示非雷,‘1’表示雷,不打印在屏幕上;另一个用来存储玩家扫雷的坐标并判断玩家输入坐标是否有雷,’*'表示还没有判断,打印在屏幕上。
程序分为三个部分:
//game.h头文件部分,用来声明函数,定义宏,库文件包含
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define ROW 9//行
#define COL 9//列
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 80
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);
//test.测试部分,包含扫雷小游戏主要设置部分
#include "game.h"
void meun()
{
printf("***************************\n");
printf("**** 1.play 2.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);
}
void test()
{
int input = 0;
srand((unsigned)time(NULL));//用时间戳定义rand产生随机值,只须调用一次
do
{
meun();
printf("请选择:>");
scanf("%d", &input);
switch(input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
game();
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
//game.c扫雷游戏实现的部分,程序主体
#include "game.h"
//棋盘初始化部分
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
//棋盘打印部分
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
//打印列号
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
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)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;//产生1-9的随机值
int y = rand() % col + 1;//产生1-9的随机值
if (board[x][y] == '0')//判断坐标是否布置雷
{
board[x][y] = '1';
count--;//坐标成功布置雷则雷的数量减一
}
}
}
}
//由ASCLL表可知
//'0' - '0' = 0
//'1' - '0' = 1
//'3' - '0' = 3
int get_mine_count(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';//用八个坐标的ASCLL码值相加减去8个'0'的ASCLL码值,可得十进制数,即雷的个数
}
//扫雷程序
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
//9*9-10 = 71
while (win < row * col - EASY_COUNT)
{
printf("请输入排查雷的坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//坐标合法
//1. 踩雷
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else //不是雷
{
//计算x,y坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("输入坐标非法,请重新输入!\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
当然,这个程序还有改进的余地,在这个程序中,只能一个个去查找雷,查一个坐标显示一个坐标,意即要找row * col - EASY_COUNT(本程序中9*9-10 = 71)个坐标,这难免有些麻烦。
改进思路:当玩家输入一个坐标时,判断该坐标周围八个坐标有没有雷,如果没雷继续对每个坐标的八个坐标进行查找,直到在周围八个坐标找到雷位置,没有雷的部分直接显示,有雷的部分将周围八个坐标有几个雷打印在坐标上。这里可以使用递归。