我是靠谱客的博主 羞涩海燕,这篇文章主要介绍C语言实现经典扫雷游戏流程,现在分享给大家,希望可以做个参考。

扫雷小游戏简介

想必很多人小时候电脑没网的时候都玩儿过这个经典的小游戏,也都被它折磨过。其实这个游戏很简单,通过点击相应位置显示的数字来确定周围雷的数量,在避免踩到雷的同时找出所有的雷就能获得胜利。


这次我们用C语言来实现一个简单的扫雷小游戏。

一、分析与实现

1.设计棋盘

要玩儿扫雷游戏,我们首先应该有一个棋盘。这个棋盘中的雷应该是在开始玩儿游戏的时候就已经布置好了,不能随意变化。但是呢又不能给玩家看到雷的位置,所以呢,我们应该有两个棋盘,一个显示给玩家,一个给用来给设计者查看。

有了棋盘之后首先要进行初始化:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//初始化棋盘 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"); }

之后呢我们可以将设计好的棋盘打印出来看一看是否符合心意:

复制代码
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
//打印棋盘 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"); }

这是设计的一个简易的9X9的小棋盘,*号代表这个位置还没有被探查过,大家可以根据自己的喜好更改棋盘大小。

2.放置雷以及排雷

设计好棋盘之后,我们就需要向棋盘中布置雷了,数量可以根据自己的喜好来定,但是不要超出棋盘的范围。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//放置雷 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"); }

这里放置雷的方式是用伪随机数来实现的,通过产生伪随机数来确定放置雷的具体坐标。
雷布置好以后,就到了对玩家来说最重要的部分:扫雷。

复制代码
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
//扫雷 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); } }

扫雷功能包含的内容相对较多,玩家可以根据提示来选择坐标进行排雷,当然,坐标应该是一个合理的坐标,如果出界了那当然是不行滴。
每次排雷之后会进行检测,是踩到了雷,还是没有踩到雷,如果没有就继续排雷,如果踩到了就GG了。

二、扫雷小游戏演示

这里我为了方便排雷,把雷的雷的存储的数组也打印了出来,而且这次的排雷是失败的,失败之后会让你重新选择是否游戏。

这是一次成功的排雷,成功的方式就是找出所有的不是雷的坐标,之后无论是成功或者失败都会打印出雷存储的数组来查看。

下面是优化以后的扫雷游戏

大家可以发现,当选择了一个周围没有雷的坐标之后会向周围发散,递归的打印所有的周围没有雷的位置及这个位置周围的雷的数量。

三、源码

我这里使用了三个文件来存储扫雷代码。

game.h:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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.h头文件放了要使用函数的头文件,函数的声明,以及一些宏定义

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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#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

复制代码
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
#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); } void menu() { printf("**********************n"); printf("******* 1.play *******n"); printf("******* 0.exit *******n"); printf("**********************n"); } void test() { 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); } int main() { test(); return 0; }

test.c文件中包含了main函数,是整个程序的入口处。
如果有需要的可以使用该源代码。

总结

这只是一个简单的扫雷小游戏的实现形式,它存在着一些很不方便的地方,比如排雷的时候并不能像我们在电脑上玩儿的那样,有时候会直接扫出一大片空白来,这就是这个扫雷小游戏的一个不足之处,当然,不好的地方有很多,欢迎各位看官指正!

这次的更新已经将扫雷的功能做了优化,类似于电脑上玩的扫雷游戏。当选择了一个周围没有雷的坐标之后会向周围发散,递归的打印所有的周围没有雷的位置及这个位置周围的雷的数量。大家可以探讨探讨,一定还有更好的设计方法

到此这篇关于C语言实现经典扫雷游戏流程的文章就介绍到这了,更多相关C语言 扫雷内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是羞涩海燕最近收集整理的关于C语言实现经典扫雷游戏流程的全部内容,更多相关C语言实现经典扫雷游戏流程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(120)

评论列表共有 0 条评论

立即
投稿
返回
顶部