C语言项目实战:数组与函数实现扫雷游戏

C语言项目实战:数组与函数实现扫雷游戏

1. 项目设计

使用两个二维数组:

  • mine:保存雷的真实分布;
  • show:保存玩家当前看到的棋盘。

在 9×9 棋盘外再增加一圈,实际数组为 11×11。这样统计边缘位置周围八格时,不必为每个方向单独判断越界。

2. 文件结构

1
2
3
4
minesweeper/
├── game.h # 宏、类型和函数声明
├── game.c # 游戏功能实现
└── test.c # 菜单与主流程

3. game.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef GAME_H
#define 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 EASY_COUNT 10

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);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

#endif

4. game.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
board[i][j] = set;
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("----- 扫雷 -----\n");
for (int j = 0; j <= col; ++j)
printf("%d ", j);
printf("\n");

for (int i = 1; i <= row; ++i)
{
printf("%d ", i);
for (int j = 1; j <= col; ++j)
printf("%c ", board[i][j]);
printf("\n");
}
}

void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count > 0)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
--count;
}
}
}

static int GetMineCount(char mine[ROWS][COLS], 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';
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0, y = 0;
int safe_count = 0;

while (safe_count < row * col - EASY_COUNT)
{
printf("请输入排查坐标:");
if (scanf("%d%d", &x, &y) != 2)
return;

if (x < 1 || x > row || y < 1 || y > col)
{
printf("坐标越界。\n");
continue;
}
if (show[x][y] != '*')
{
printf("该位置已经排查。\n");
continue;
}
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了。\n");
DisplayBoard(mine, row, col);
return;
}

show[x][y] = (char)(GetMineCount(mine, x, y) + '0');
++safe_count;
DisplayBoard(show, row, col);
}
printf("恭喜,排雷成功!\n");
}

5. test.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "game.h"

static void game(void)
{
char mine[ROWS][COLS];
char show[ROWS][COLS];

InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}

int main(void)
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
printf("1. 开始游戏 0. 退出游戏\n请选择:");
scanf("%d", &input);
if (input == 1)
game();
else if (input != 0)
printf("选择错误。\n");
} while (input != 0);
return 0;
}

编译:

1
gcc test.c game.c -o minesweeper

6. 可以继续扩展

  • 当周围没有雷时递归展开空白区域;
  • 增加插旗、取消插旗功能;
  • 支持初级、中级和高级难度;
  • 保证第一次点击不会踩雷;
  • 增加计时与排行榜;
  • 对所有输入进行完整校验。

7. 总结

扫雷项目把二维数组、函数、多文件组织和随机数串联起来。更重要的是,它展示了如何先设计数据结构和接口,再逐步实现具体功能。