对于C语言学习者来说,在完成C语言初级学习之后,扫雷游戏是一个很好的知识的总结和练习。
扫雷即在一个棋盘中,随机放入一定数量的雷,玩家通过输入坐标,得到坐标上的信息(以此点为中心四周8个格子内地雷数量的总和),来判断此格子是否有地雷,从而依照此法排查出整个棋盘上所有的地雷。
本项目采用9*9的棋盘,放入地雷的数量可以依照个人设置。
首先声明总的头文件
1
2
3
4
5
6
7
|
#ifndef _UTILI_H_ #define _UTILI_H_ #include<stdio.h> #include<time.h> //后面需要地雷的随机放置,需要时间函数 #endif /* _UTILI_H_ */ |
接下来我们需要对整个项目所有需要用的函数进行声明,放置在一个头文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#ifndef _GAME_H_ //条件编译 #define _GAME_H_ #include"utili.h" #define QUIT 0 //0退出 1 进入游戏 #define GAME 1 #define ROW 9 //定义9*9的扫雷界面 #define COL 9 #define ROWS ROW+2 //左右两边加1行1列,为了统计周围雷个数 #define COLS COL+2 #define GAME_LEVEL 20 void InitBoard( char board[ROWS][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); //找雷 void StartGame(); #endif /* _GAME_H_ */ |
整个游戏各个部分功能进行编写代码,分别实现不同的功能:
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#include"Game.h" void InitBoard( char board[ROWS][COLS], char set) //初始化棋盘,在后台执行扫雷任务时 { for ( int i=0; i<ROWS; ++i) //实际上是在11 * 11 的棋盘 上进行的 { for ( int j=0; j<COLS; ++j) { board[i][j] = set; } } } void DisplayBoard( char board[ROWS][COLS], int row, int col) { for ( int i=0; i<=row; ++i) //在界面显示时,我们的棋盘实际展示的时9 * 9的界面 { printf ( "%d " , i); } 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 mine[ROWS][COLS], int row, int col) { int count = GAME_LEVEL; srand ( time (0)); //通过随机数获得棋盘上随机的地雷放置 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' ; //有地雷的格子内0被赋值为1,但注意这个1为字符 count--; } } } int GetMineCount( char mine[ROWS][COLS], int x, int y) //获得以格子为中心周围8个格子地雷数 { 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 win = 0; int x, y; while (win < row*col-GAME_LEVEL) //判断赢的条件 { printf ( "请输入要排雷的位置:>" ); scanf ( "%d %d" , &x, &y); if (x<1||x>row || y<1 || y>col) { printf ( "输入的排雷位置非法,请重新输入.....\n" ); continue ; } if (mine[x][y] == '1' ) { printf ( "很遗憾,你被Over.\n" ); DisplayBoard(mine, ROW, COL); break ; } //统计雷的个数 int count = GetMineCount(mine, x, y); show[x][y] = count + '0' ; system ( "cls" ); DisplayBoard(show, ROW, COL); win++; } if (win >= row*col-GAME_LEVEL) { printf ( "恭喜你,排雷成功.\n" ); } } void StartGame() { //定义棋盘 char mine[ROWS][COLS]; char show[ROWS][COLS]; //初始化棋盘 InitBoard(mine, '0' ); InitBoard(show, '*' ); //埋雷 SetMine(mine, ROW, COL); //显示棋盘 DisplayBoard(show, ROW, COL); //扫雷 FindMine(mine, show, ROW, COL); } |
编写主函数:
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
|
#include"Game.h" int main( int argc, char *argv[]) { int select = 1; while (select) { printf ( "*************************************\n" ); printf ( "* 简 易 扫 雷 游 戏 *\n" ); printf ( "*************************************\n" ); printf ( "* [1] Play *\n" ); printf ( "* [0] Quit *\n" ); printf ( "*************************************\n" ); printf ( "请选择:>" ); scanf ( "%d" , &select); if (select == QUIT) break ; if (select != GAME) { printf ( "输入的操作有误,请重新输入......\n" ); continue ; } //开始游戏 StartGame(); } printf ( "游戏结束,拜拜.\n" ); return 0; } |
代码在VS2015上正常执行,整个游戏过程没出现问题。
游戏还可以有许多改进的地方,如增加计时功能,点到附件无雷的格子时展示所有相邻的无雷格子等,后续还可以丰富完善。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_59215611/article/details/124912093