目录
1. 打印菜单
先根据菜单进行选择,开始游戏和退出游戏。
2. 多文件分装程序
test.c文件用于程序设计;
game.c用于函数定义;
game.h用于函数声明。
3. 各功能模快化实现
创建两个二维数组,一个mine数组存放布置好的雷的信息,另外⼀个show数组存放排查出的雷的信息。为了便于更改棋盘的大小,这里需要定义字符型常量。全部定义的字符型常量在后面。
3.1 初始化棋盘
3.2 打印棋盘
3.3 布置雷
首先了解扫雷的规则,玩家选择一个坐标,如果该坐标不是雷,就统计该坐标周围的8个坐标有几个雷,然后在玩家棋盘中显示其周围的雷的总数。
9*9的棋盘在边缘检测时会产生越界的问题,那么可以采用11*11的棋盘去初始化,埋雷的位置限制在9*9的棋盘中。
字符型常量定于如下:
3.4 排查雷
这部分特别需要注意的是两个棋盘之间的联系。判断周围雷的数量在mine数组中进行,打印输出雷的信息在show数组中进行。
以上便是游戏各模块的实现。
3.5 游戏逻辑安排
游戏的各个模块已经基本实现,接下来需要将各个模块进行组装,使游戏顺利运行。
4. 整体代码呈现
game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MineNum 10
//初始化棋盘
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char arr[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char arr[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col);
game.c
#include "game.h"
//初始化棋盘
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
arr[i][j] = set;
}
}
//打印棋盘
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
printf("------------扫雷游戏------------\n");
for (int i = 0; i <= row; i++)
printf("%3d", i);//打印列号
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%3d", i);//打印行号
for (int j = 1; j <= col; j++)
printf("%3c", arr[i][j]);
printf("\n");
}
}
//布置雷
void SetMine(char arr[ROWS][COLS], int row, int col)
{
int count = MineNum;
while (count)
{
int x = rand() % row + 1;//使用随机数来进行布雷,与srand配合使用来实现真正的随机。
int y = rand() % col + 1;//row,col等于9,随机数余9范围是0~8,加1范围就变成了1~9。
if (arr[x][y] == '0')
{
arr[x][y] = '1';//用字符1表示雷
count--;
}
}
}
//排查雷
//int NearMine(char mine[ROWS][COLS], int x, int y)
//{//所排查的坐标不是雷时,统计该坐标周围的8个坐标有几个雷
// 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');
//}
int NearMine(char mine[ROWS][COLS], int x, int y)
{//所排查的坐标不是雷时,统计该坐标周围的8个坐标有几个雷
int count = 0;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
count += (mine[x][y] - '0');
}
return count;
}
void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - MineNum)
{
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
{
int count = NearMine(mine, ROW, COL);//函数返回值是一个整型数字
show[x][y] = count + '0';//以字符型数字打印输出,加上'0'转变为对应的ASCLL值
DisplayBoard(show, ROW, COL);//没有踩到雷继续打印棋盘
win++;
}
}
else
printf("该坐标已排查,请重新输入\n");
}
else
printf("坐标越界,请重新输入\n");
}
}
test.c
#include "game.h"
void menu()
{
printf("\n");
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(show, mine, ROW, COL);
}
void test()
{
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);
}
int main()
{
test();
return 0;
}