#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
printf("====================n");
printf("=======1.扫雷=======n");
printf("=======0.退出=======n");
printf("====================n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//存放布置好的雷的信息
char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息
//初始化数组的内容,mine数组没有雷,都是‘0’,show数组没排雷,都是‘*’
InitBoard(mine, ROWS, COLS,'0');
InitBoard(show, ROWS, COLS, '*');
//设置雷
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, 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;
}
函数声明
#pragma once #include函数实现#include #include #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EAST_COUNT 10 void InitBoard(char board[ROWS][COLS], int rows, int cols, char n); 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);
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char n)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
board[i][j] = n;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("=====扫雷游戏=====n");
for (j = 0; j <= col; j++) {
printf("%d ", j);
}
printf("n");
for (i = 1; i <= row; i++) {
printf("%d ", i);
for (j = 1; j <= col; j++) {
printf("%c ", board[i][j]);
}
printf("n");
}
printf("=====扫雷游戏=====n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EAST_COUNT;
while (count) {
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0') {
board[x][y] = '1';
count--;
}
}
}
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
return (board[x - 1][y] +
board[x - 1][y - 1] +
board[x][y - 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] +
board[x][y + 1] +
board[x - 1][y + 1] - 8 * '0');
}
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 - EAST_COUNT) {
printf("请输入要排查的坐标:");
scanf("%d %d",&x,&y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
//重复坐标
if (show[x][y] != '*') {
printf("该坐标被排查过了,不能重复排查n");
}
else {
//如果是雷
if (mine[x][y] == '1') {
printf("很遗憾你被炸死了n");
DisplayBoard(mine, ROW, COL);
break;
}
//如果不是雷
else {
//统计mine数组中x,y坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';//转换成数字字符
DisplayBoard(show, ROW, COL);
}
}
}
else {
printf("输入的坐标非法,请重新输入n");
}
}
if (win == row * col - EAST_COUNT) {
printf("恭喜你,排雷成功n");
DisplayBoard(mine, ROW, COL);
}
}
思路分析
流程讲解用两个二维数组来实现。mine数组来放置雷,show数组来显示该位置有几个雷。
- 游戏菜单
游戏菜单用menu函数打印,主体实现输入1是玩游戏,输入0是退出游戏,输入其他则循环判断是否玩游戏,并提示请输入正确选项。
- 初始化棋盘
mine数组还没有地雷,都是’0’;show数组没有排雷,都是’*’
- 设置雷
mine数组中随机存放EASY_COUNT个雷
- 排查雷
实例操作排查分三种情况:
a. 该位置是雷,游戏结束
b. 该位置不是雷,显示周围八个位子雷的数量
c. 除了雷以外位置都排查完了



