引言
还记得小时候在Windows XP上玩的扫雷游戏吗?那个看似简单却充满策略的小游戏,曾陪伴了无数人的童年。今天,我将带你从零开始,用最基础的C语言重新实现这个经典游戏!无论你是编程新手还是想重温基础,这篇文章都将为你详细解析扫雷游戏的完整实现过程。
一、游戏设计思路
1.1 核心规则回顾
-
游戏在一个9×9的方格中进行
-
随机分布10个地雷
-
玩家点击格子,如果是雷则游戏结束
-
如果不是雷,显示周围8个格子中的雷数
-
如果周围没有雷,自动展开相邻的安全区域
-
标记所有雷的位置即可获胜
1.2 技术架构设计
┌─────────────────────────────────────┐
│ 主程序 (main.c) │
│ ├── 游戏循环控制 │
│ └── 用户输入处理 │
├─────────────────────────────────────┤
│ 游戏逻辑 (game.c) │
│ ├── 棋盘初始化 │
│ ├── 布雷算法 │
│ ├── 递归展开算法 │
│ └── 胜负判断逻辑 │
├─────────────────────────────────────┤
│ 头文件 (game.h) │
│ ├── 宏定义 │
│ ├── 函数声明 │
│ └── 数据结构 │
└─────────────────────────────────────┘
二、环境准备与项目结构
2.1 开发环境
-
编译器: GCC/MinGW 或 Visual Studio 2022
-
开发语言: C语言
-
难度级别: 初学者友好
2.2 项目文件结构
text
Minesweeper/
│
├── main.c # 主程序入口
├── game.h # 头文件,声明函数和常量
├── game.c # 游戏核心逻辑实现
└── README.md # 项目说明文档
三、代码实现详解
3.1 定义游戏常量(game.h)
// game.h
#pragma once
#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 mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int GetMineCount(char mine[ROWS][COLS], int x, int y);
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
int IsWin(char show[ROWS][COLS], int row, int col);
void menu();
void game();
设计要点:
-
使用ROWS和COLS比实际棋盘大2,这是为了处理边界格子时不需要特殊判断
-
宏定义使得游戏难度调整变得简单
3.2 主程序框架(main.c)
// main.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"
int main()
{
int input = 0;
srand((unsigned int)time(NULL)); // 设置随机种子
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;
}
3.3 菜单设计
void menu()
{
printf("******************************\\n");
printf("******* 扫雷游戏 *********\\n");
printf("******* 1.开始游戏 ********\\n");
printf("******* 0.退出游戏 ********\\n");
printf("******************************\\n");
}
3.4 游戏核心逻辑实现(game.c)
3.4.1 棋盘初始化
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;
}
}
}
代码解析:
-
使用双重循环遍历整个二维数组
-
set参数决定初始化的字符,雷盘用'0',显示盘用'*'
3.4.2 显示棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("\\n ");
for(int i = 1; i <= col; i++) {
printf("%2d ", i);
}
printf("\\n");
printf(" +");
for(int i = 1; i <= col; i++) {
printf("—");
}
printf("+\\n");
for(int i = 1; i <= row; i++) {
printf("%2d|", i);
for(int j = 1; j <= col; j++) {
printf(" %c ", board[i][j]);
}
printf("|\\n");
}
printf(" +");
for(int i = 1; i <= col; i++) {
printf("—");
}
printf("+\\n");
}
显示效果:
text
1 2 3 4 5 6 7 8 9
+————————-+
1| * * * * * * * * * |
2| * * * * * * * * * |
3| * * * * * * * * * |
4| * * * * * * * * * |
5| * * * * * * * * * |
6| * * * * * * * * * |
7| * * * * * * * * * |
8| * * * * * * * * * |
9| * * * * * * * * * |
+————————-+
3.4.3 随机布雷算法
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while(count) {
int x = rand() % row + 1; // 生成1-row的随机数
int y = rand() % col + 1; // 生成1-col的随机数
if(mine[x][y] == '0') {
mine[x][y] = '1'; // '1'表示有雷
count–;
}
}
}
算法特点:
-
使用rand()函数生成随机位置
-
确保同一个位置不会重复布雷
-
雷的位置用字符'1'表示,非雷位置用'0'表示
3.4.4 计算周围雷数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
// 周围8个位置的坐标偏移量
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int count = 0;
for(int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(mine[nx][ny] == '1') {
count++;
}
}
return count;
}
计算原理:
(-1,-1) (-1,0) (-1,1)
(0,-1) (x,y) (0,1)
(1,-1) (1,0) (1,1)
3.4.5 递归展开算法(核心难点)
c
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS],
int x, int y, int row, int col)
{
// 边界检查和终止条件
if(x < 1 || x > row || y < 1 || y > col) return;
if(show[x][y] != '*') return;
int count = GetMineCount(mine, x, y);
if(count > 0) {
show[x][y] = count + '0'; // 将数字转换为字符
} else {
show[x][y] = ' '; // 周围无雷显示空格
// 递归展开周围的8个方向
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
if(i == 0 && j == 0) continue; // 跳过自身
Expand(mine, show, x+i, y+j, row, col);
}
}
}
}
递归展开示意图:
text
初始状态: 第一次点击(5,5): 递归展开后:
********* ********* ***111 *
********* ********* ***1 1*
********* ********* 11 21111
********* ********* 1 *
********* ****1**** → 211
********* ********* 111
********* ********* 1
********* ********* 111
********* ********* 1
3.4.6 玩家排雷逻辑
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS],
int row, int col)
{
int x, y;
while(1) {
printf("请输入要排查的坐标(行 列): ");
scanf("%d %d", &x, &y);
if(x >= 1 && x <= row && y >= 1 && y <= col) {
if(mine[x][y] == '1') {
printf("很遗憾,你踩到雷了!\\n");
DisplayBoard(mine, ROW, COL); // 显示所有雷
break;
} else {
Expand(mine, show, x, y, row, col);
DisplayBoard(show, ROW, COL);
if(IsWin(show, row, col)) {
printf("恭喜你,排雷成功!\\n");
break;
}
}
} else {
printf("坐标非法,请重新输入!\\n");
}
}
}
3.4.7 胜负判断
int IsWin(char show[ROWS][COLS], int row, int col)
{
int count = 0;
for(int i = 1; i <= row; i++) {
for(int j = 1; j <= col; j++) {
if(show[i][j] == '*') {
count++;
}
}
}
return count == EASY_COUNT;
}
胜利条件:未翻开的格子数等于总雷数
四、完整代码整合
4.1 game.h 完整代码
#pragma once
#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 menu();
void game();
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 mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int GetMineCount(char mine[ROWS][COLS], int x, int y);
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col);
int IsWin(char show[ROWS][COLS], int row, int col);
4.2 game.c 完整代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu() { /* 菜单实现 */ }
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 mine[ROWS][COLS], int row, int col) { /* 布雷实现 */ }
int GetMineCount(char mine[ROWS][COLS], int x, int y) { /* 计算雷数实现 */ }
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col) { /* 递归展开实现 */ }
int IsWin(char show[ROWS][COLS], int row, int col) { /* 胜利判断实现 */ }
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { /* 排雷实现 */ }
void game()
{
char mine[ROWS][COLS] = {0};
char show[ROWS][COLS] = {0};
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
4.3 main.c 完整代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
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;
}
五、编译与运行
5.1 使用GCC编译
# 编译命令
gcc main.c game.c -o minesweeper
# 运行游戏
./minesweeper # Linux/Mac
minesweeper.exe # Windows
5.2 使用Visual Studio编译
新建C++控制台项目
添加上述三个文件
按F5编译运行
5.3 运行截图
text
******************************
******* 扫雷游戏 *********
******* 1.开始游戏 ********
******* 0.退出游戏 ********
******************************
请选择: 1
1 2 3 4 5 6 7 8 9
+————————-+
1| * * * * * * * * * |
2| * * * * * * * * * |
3| * * * * * * * * * |
4| * * * * * * * * * |
5| * * * * * * * * * |
6| * * * * * * * * * |
7| * * * * * * * * * |
8| * * * * * * * * * |
9| * * * * * * * * * |
+————————-+
请输入要排查的坐标(行 列): 5 5
六、功能扩展与优化建议
6.1 可以添加的功能
多难度选择
#define MID_COUNT 20 // 中级难度
#define HARD_COUNT 30 // 高级难度
计时功能
#include <time.h>
clock_t start, end;
double duration;
标记雷的位置
// 添加标记功能,用'F'表示标记
if(input == 'F') {
show[x][y] = 'F';
}
第一次不踩雷保证
// 在布雷时确保第一次点击的位置不是雷
if(first_click) {
while(mine[x][y] == '1') {
// 重新布雷
}
}
6.2 代码优化建议
使用结构体组织数据
typedef struct {
char board[ROWS][COLS];
int row;
int col;
int mineCount;
} GameBoard;
添加输入验证
int ValidateInput(int x, int y) {
return (x >= 1 && x <= ROW && y >= 1 && y <= COL);
}
改进递归算法效率
七、学习收获与总结
通过这个项目,你可以学习到:
C语言核心知识
-
二维数组的应用
-
函数的封装与调用
-
递归算法的实现
-
随机数生成
编程思维训练
-
问题分解能力
-
算法设计思维
-
调试和测试技巧
-
代码重构能力
游戏开发基础
-
游戏循环设计
-
用户交互处理
-
状态管理
-
胜负判断逻辑
八、常见问题解答
Q1: 为什么棋盘要比实际大一圈?
A: 这是为了在处理边界格子时,不需要额外的边界检查,简化递归算法。
Q2: 如何调整游戏难度?
A: 修改game.h中的EASY_COUNT宏定义,或添加多个难度级别。
Q3: 递归展开会导致栈溢出吗?
A: 在9×9的棋盘上不会,但如果棋盘很大且连续空白区域很多,可能需要使用栈结构代替递归。
Q4: 如何添加图形界面?
A: 可以使用SDL、EasyX或Qt等图形库,将字符界面替换为图形界面。
结语
通过这个扫雷项目的实现,我们不仅重温了经典游戏,更深入理解了C语言的核心概念和算法思想。编程就像扫雷一样,需要耐心、策略和逻辑思维。每一步的探索都可能带来新的发现,每一次的排错都是能力的提升。
希望这篇文章能帮助你更好地理解C语言编程,也欢迎你在评论区分享自己的实现心得和改进建议!


