# 一、分析与实现

## 1.设计棋盘

``````//初始化棋盘
void InitChess(char chess[ROWS][COLS], int rows, int cols, char sign)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
chess[i][j] = sign;
}
}
printf("初始化棋盘成功!n");
}
``````

``````//打印棋盘
void DisplayChess(char chess[ROWS][COLS], int row, int col)
{
int i = 0;
printf("   ");
for (i = 1; i <= row; i++)
{
printf("  %d ", i);
}
printf("n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("   ");
for (j = 1; j <= col; j++)
{
printf("+---");
}
printf("+n");
printf(" %d ", i);
for (j = 1; j <= col; j++)
{
printf("| %c ", chess[i][j]);
}
printf("|n");
}
int j = 0;
printf("   ");
for (j = 1; j <= col; j++)
{
printf("+---");
}
printf("+n");
}
``````

## 2.放置雷以及排雷

``````//放置雷
void SetMine(char chess[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (chess[x][y] == '0')
{
chess[x][y] = '1';
count--;
}
}
printf("布置雷成功！n");
}
``````

``````//扫雷
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = 0;
while (count < 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("恭喜你，你可以投胎了！n");
DisplayChess(mine, row, col);
break;
}
else
{
int mine_count = Get_Mine_Count(mine, x, y);
show[x][y] = '0' + mine_count;
DisplayChess(show, row, col);
count++;
}
}
else
{
printf("坐标非法，请重新输入!n");
}
}
if (count == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功！n");
DisplayChess(mine, row, col);
}
}
``````

# 三、源码

game.h:

``````#pragma once

#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define EASY_COUNT 10

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

void InitChess(char chess[ROWS][COLS], int rows, int cols, char sign);
void DisplayChess(char chess[ROWS][COLS], int row, int col);
void SetMine(char chess[ROWS][COLS], int row, int col);
void SweepMine(char chess[ROWS][COLS], char show[ROWS][COLS], int row, int col);
``````

game.c:

``````#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

//初始化棋盘
void InitChess(char chess[ROWS][COLS], int rows, int cols, char sign)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
chess[i][j] = sign;
}
}
printf("初始化棋盘成功!n");
}
//打印棋盘
void DisplayChess(char chess[ROWS][COLS], int row, int col)
{
int i = 0;
printf("   ");
for (i = 1; i <= row; i++)
{
printf("  %d ", i);
}
printf("n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("   ");
for (j = 1; j <= col; j++)
{
printf("+---");
}
printf("+n");
printf(" %d ", i);
for (j = 1; j <= col; j++)
{
printf("| %c ", chess[i][j]);
}
printf("|n");
}
int j = 0;
printf("   ");
for (j = 1; j <= col; j++)
{
printf("+---");
}
printf("+n");
}
//放置雷
void SetMine(char chess[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (chess[x][y] == '0')
{
chess[x][y] = '1';
count--;
}
}
printf("布置雷成功！n");
}
static int Get_Mine_Count(char chess[ROWS][COLS], char show[ROWS][COLS], int flag[ROWS][COLS], int x, int y, int* pcount)
{
if (x >= 1 && x <= ROW && y >= 1 && y <= COL && flag[x][y] != 1)
{
int mine_count = chess[x - 1][y - 1] +
chess[x - 1][y] +
chess[x - 1][y + 1] +
chess[x][y - 1] +
chess[x][y + 1] +
chess[x + 1][y - 1] +
chess[x + 1][y] +
chess[x + 1][y + 1] - 8 * '0';
flag[x][y] = 1;
show[x][y] = '0' + mine_count;
(*pcount)++;
if (0 == mine_count)
{
Get_Mine_Count(chess, show, flag, x - 1, y - 1, pcount);
Get_Mine_Count(chess, show, flag, x - 1, y, pcount);
Get_Mine_Count(chess, show, flag, x - 1, y + 1, pcount);
Get_Mine_Count(chess, show, flag, x, y - 1, pcount);
Get_Mine_Count(chess, show, flag, x, y + 1, pcount);
Get_Mine_Count(chess, show, flag, x + 1, y - 1, pcount);
Get_Mine_Count(chess, show, flag, x + 1, y, pcount);
Get_Mine_Count(chess, show, flag, x + 1, y + 1, pcount);
}

return mine_count;
}
//else
//{
//	return 0;
//}
}
//扫雷
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = 0;
int flag[ROWS][COLS] = { 0 };
while (count < 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("恭喜你，你可以投胎了！n");
DisplayChess(mine, row, col);
break;
}
else
{
int mine_count = Get_Mine_Count(mine, show, flag, x, y, &count);
//show[x][y] = '0' + mine_count;

DisplayChess(show, row, col);
}
}
else
{
printf("坐标非法，请重新输入!n");
}
}
if (count == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功！n");
DisplayChess(mine, row, col);
}
}
``````

game.c文件主要包括了函数的设计与实现。细心的读者可以发现有一个Get_Mine_Count函数没有放到头文件中声明，是因为这个函数只需要在game.c文件中调用其他函数时使用，并不需要声明出来。

test,c

``````#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void game()
{
//创建两个棋盘
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
InitChess(mine, ROWS, COLS, '0');
InitChess(show, ROWS, COLS, '*');

//打印棋盘
//DisplayChess(mine, ROW, COL);
DisplayChess(show, ROW, COL);

//放置雷
SetMine(mine, ROW, COL);
//DisplayChess(mine, ROW, COL);

//排雷
SweepMine(mine, show, ROW, COL);
}
{
printf("**********************n");
printf("******* 1.play *******n");
printf("******* 0.exit *******n");
printf("**********************n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
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;
}
``````

test.c文件中包含了main函数，是整个程序的入口处。

THE END

)">