# 数组小练习（2）——扫雷

## 三、代码

Minesweeper.h（函数声明）

``````#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define ROWS ROW+2
#define COL 9
#define COLS COL+2

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

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,int count);//布置地雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int num);//探查地雷
``````

Minesweeper.cpp（游戏的函数具体实现）

``````#include"Minesweeper.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;
printf("-----------------------------------------n");
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");
}
printf("-----------------------------------------n");
}

void SetMine(char board[ROWS][COLS], int row, int col,int 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) {
int sum = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
sum += mine[i][j];
}
}
return sum - 9 * '0';
}

void ShowMine(char mine[ROWS][COLS],char show[ROWS][COLS],int x, int y) {
int count = GetMineCount(mine, x, y);
if (count == 0) {
show[x][y] = count + '0';
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 1 && i <= ROW && j >= 1 && j <= COL && show[i][j] == '*') {
ShowMine(mine, show, i, j);
}
}
}
}
else {
show[x][y] = count + '0';
}
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int num) {
int x = 0;
int y = 0;
int win = 0;
while (win<row*col-num) {
printf("请输入要排查的坐标：>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (mine[x][y] == '1') {
printf("n很遗憾，你被炸死了!n");
DisplayBoard(mine, row, col);
break;
}
else {
ShowMine(mine, show, x, y);
DisplayBoard(show, row, col);
win++;
}
}
else {
printf("坐标非法！n");
}
}
if (win == row * col - num) {
printf("恭喜你，排雷成功n");
DisplayBoard(mine, row, col);
}
}
``````

test.cpp（测试）

``````#include"Minesweeper.h"
printf("****************************************n");
printf("**********       1. play      **********n");
printf("**********       0. exit      **********n");
printf("****************************************n");
}
void Minesweeper() {
char mine[ROWS][COLS] = {0};//存放布置好的雷
char show[ROWS][COLS] = {0};//存放排查出来的雷的信息
InitBoard(mine, ROWS, COLS,'0');
InitBoard(show, ROWS, COLS,'*');
SetMine(mine, ROW, COL, 10);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL,10);
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));//用时间戳设置随机数的生成起点
do {
printf("请选择：>");
scanf("%d", &input);
switch (input) {
case 1:
Minesweeper();
break;
case 0:
printf("退出游戏n");
break;
default:
printf("选择错误n");
}
} while (input);
return 0;
}
``````

## 四、改进思路

1.增加传统扫雷游戏里的插红旗功能。

2.增加可视化。

3.增加游戏难度选择，增加游戏趣味性。

THE END

)">