# 用C语言实现扫雷小游戏

### 2. 编写思路

2.1 编写选择界面

```void Menu()
{
printf("##########################\n");
printf("##   1:Play    2:Exit   ##\n");
printf("##########################\n");
printf("请输入# ");
}```

2.2 编写执行程序

```int GetMines(char mine[][COL], int row, int col, int x, int  y)
{
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';
}```

2.3 编写设置雷的个数

```void SetMines(char mine_board[][COL], int row, int col)
{
int count = NUMS;  //设置雷
while (count){
int x = rand() % 10 + 1;
int y = rand() % 10 + 1;
if (mine_board[x][y] == '0'){
mine_board[x][y] = '1';
count--;
}
}
}```

2.4 游戏界面的设置

```static void ShowLine(int nums)
{
printf("---");
for (int i = 0; i < nums; i++){
printf("-");
}
printf("\n");
}
void ShowBoard(char show_board[][COL], int row, int col)
{
printf(" ");
for (int i = 1; i < row - 1; i++){   //行的数字
printf("  %2d", i);
}
printf("\n");
ShowLine(2 * col + col + 4);//这里传入的参数是由于‘-'占位原因，可以根据自己实际界面调整；
for (int i = 1; i < row - 1; i++){ //列的数组
printf("%2d|", i);
for (int j = 1; j < col - 1; j++){
printf(" %c |", show_board[i][j]);
}
printf("\n");
ShowLine(2 * col + col + 4);
}
}```

2.5 游戏逻辑的编写

```void Game()
{
char show_board[ROW][COL];
char mine_board[ROW][COL];
memset(show_board, '*', sizeof(show_board));
memset(mine_board, '0', sizeof(mine_board));
srand((unsigned long)time(NULL));

SetMines(mine_board, ROW, COL);
int count = (ROW - 2)*(COL - 2) - NUMS;
int x = 0;
int y = 0;
do{
ShowBoard(show_board, ROW, COL);
printf("请输入位置# ");
scanf("%d %d", &x, &y);
if (x<1 || x>10 || y<1 || y>10){
printf("输入越界，请重新输入！\n");
continue;
}
if (show_board[x][y] != '*'){//证明位置被排过了
printf("该位置已被排除，请重新输入！\n");
continue;
}
if (mine_board[x][y] == '1'){//没点到雷
break;
}
int num = GetMines(mine_board, ROW, COL, x, y);
show_board[x][y] = num + '0';
count--;

} while (count > 0);
if (count > 0){
printf("you loss!\n");
}
else{
printf("you win!\n");
}
printf("你输了，下面是雷区的分布\n");
ShowBoard(mine_board, ROW, COL);
printf("play again? \n");
}```

2.6 设定游戏开始or退出

```int main()
{
int quit = 0;
int select = 0;
while (!quit){
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 2:
quit = 1;
break;
default:
printf("输入有误，请重新输入\n");
}
}
system("pause");
return 0;
}```

### 3. 汇总代码

```#include
#include
#include
#include

#pragma warning(disable:4996)

#define ROW 12
#define COL 12
#define NUMS 20

{
printf("##########################\n");
printf("##   1:Play    2:Exit   ##\n");
printf("##########################\n");
printf("请输入# ");
}
void SetMines(char mine_board[][COL], int row, int col)
{
int count = NUMS;  //设置雷
while (count){
int x = rand() % 10 + 1;
int y = rand() % 10 + 1;
if (mine_board[x][y] == '0'){
mine_board[x][y] = '1';
count--;
}
}
}
int GetMines(char mine[][COL], int row, int col, int x, int  y)
{
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';
}

static void ShowLine(int nums)
{
printf("---");
for (int i = 0; i < nums; i++){
printf("-");
}
printf("\n");
}

void ShowBoard(char show_board[][COL], int row, int col)
{
printf(" ");
for (int i = 1; i < row - 1; i++){   //行的数字
printf("  %2d", i);
}
printf("\n");
ShowLine(2 * col + col + 4);

for (int i = 1; i < row - 1; i++){ //列的数组
printf("%2d|", i);
for (int j = 1; j < col - 1; j++){
printf(" %c |", show_board[i][j]);
}
printf("\n");
ShowLine(2 * col + col + 4);
}
}
void Game()
{
char show_board[ROW][COL];
char mine_board[ROW][COL];
memset(show_board, '*', sizeof(show_board));
memset(mine_board, '0', sizeof(mine_board));
srand((unsigned long)time(NULL));

SetMines(mine_board, ROW, COL);
int count = (ROW - 2)*(COL - 2) - NUMS;
int x = 0;
int y = 0;
do{
ShowBoard(show_board, ROW, COL);
printf("请输入位置# ");
scanf("%d %d", &x, &y);
if (x<1 || x>10 || y<1 || y>10){
printf("输入越界，请重新输入！\n");
continue;
}
if (show_board[x][y] != '*'){//证明位置被排过了
printf("该位置已被排除，请重新输入！\n");
continue;
}
if (mine_board[x][y] == '1'){//没点到雷
break;
}
int num = GetMines(mine_board, ROW, COL, x, y);
show_board[x][y] = num + '0';
count--;

} while (count > 0);
if (count > 0){
printf("you loss!\n");
}
else{
printf("you win!\n");
}
printf("你输了，下面是雷区的分布\n");
ShowBoard(mine_board, ROW, COL);
printf("play again? \n");
}

int main()
{
int quit = 0;
int select = 0;
while (!quit){
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 2:
quit = 1;
break;
default:
printf("输入有误，请重新输入\n");
}
}
system("pause");
return 0;
}```