我是靠谱客的博主 单薄羊,这篇文章主要介绍C语言: 扫雷小游戏---手把手基础教学,现在分享给大家,希望可以做个参考。

目录

准备:

正文:

1、在“test.h”中写出主体部分:

2、在头文件中声明menu函数,在“game.c”中定义函数:

3、构建一个game函数,方法同上。不同的是,需要往里面填充其他函数让游戏跑起来

4、创建一个初始化数组的函数

5、打印棋盘

6、随机布置地雷

6、接下来就是排雷了!

7、难度的选择设置



 

准备:

一个头文件,命名“game.h”。两个一个源文件,命名“game.c”和“test.c”。

头文件包含库文件:

复制代码
1
#include <stdio.h>

两个源文件包含头文件:

复制代码
1
#include "game.h"

正文:

1、在“test.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
int main() { int secret = 0; srand((unsigned int)time(NULL)); while (1) { menu(); scanf("%d", &secret); switch (secret) { case 1: printf("|----------------------------|n"); printf("|*********简单模式***********|n"); printf("|----------------------------|n"); printf("n"); game(secret); break; case 2: printf("|----------------------------|n"); printf("|*********困难模式***********|n"); printf("|----------------------------|n"); printf("n"); game(secret); break; case 3: printf("|-------------------------------------|n"); printf("|***********退出游戏,感谢支持*********|n"); printf("|-------------------------------------|n"); return 0; default: printf("输入有误n"); break; } } return 0; }

这里用while循环装入选择语句,如果输入非法数字就会一直循环。

2、在头文件中声明menu函数,在“game.c”中定义函数:

game.h:

复制代码
1
void menu();

game.c:

复制代码
1
2
3
4
5
6
7
8
void menu() { printf("***********************n"); printf("******1、简单模式******n"); printf("******2、困难模式******n"); printf("******3、退出游戏******n"); printf("***********************n"); }

这样就可以在"test.c"中调用这个函数啦!

3、构建一个game函数,方法同上。不同的是,需要往里面填充其他函数让游戏跑起来

创建两个数组,一个是给玩家看的藏雷的,一个是真实的。

复制代码
1
2
3
4
//存放布置好的雷 char mine[ROWS][COLS]; //存放排查好的雷的信息 char show[ROWS][COLS];

4、创建一个初始化数组的函数

这里先在“game.h”中宏定义一下行和列数:

复制代码
1
2
3
4
5
#define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2

由于是要统计周围的雷的个数,所以隐藏的棋盘要比展示的棋盘要大。上下左右各多一格,所以比展示的棋盘大2.

下面是函数部分: 

复制代码
1
2
3
4
5
6
7
8
9
10
void InitBoard(char arr[ROWS][COLS], int row, int col, char set) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { arr[i][j] = set; } } }

其中用双层for循环来将数组中的每一个元素设置为我们想要的字符。

接着在game函数中将展示棋盘的每一个元素设置为“*”,而隐藏棋盘每一个设置为"0"

复制代码
1
2
InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*');

5、打印棋盘

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("----------------------------------n"); for (i = 0; i <= col; i++) { printf("%d ", i); } printf("n"); for (i = 1; i <= row; i++) { printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("n"); } printf("----------------------------------n"); }

这里是利用for循环,打印一排数字,然后打印一排数组元素之后就换行,然后每行开头会打印列数,方便排雷时选取坐标。

6、随机布置地雷

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void SetMine(char board[ROWS][COLS], int row, int col,int MINE) { int count = MINE; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } }

count是布置地雷的个数

这里用随机数种子来随机布置地雷在随机的坐标下。所以得在int main里面包含一个

复制代码
1
srand((unsigned int)time(NULL));

也得包含头文件

复制代码
1
2
#include <stdlib.h> #include <time.h>

6、接下来就是排雷了!

创建一个排雷的函数

复制代码
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
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int MINE) { int x = 0; int y = 0; int win = 0; while (win<ROW*COL-MINE) { printf("请输入要排查的坐标n"); scanf("%d%d", &x, &y); if (x >= 1 && x < col && y>=1 && y < row) { if (mine[x][y] == '1') { printf("|----------------------------|n"); printf("|*****很遗憾,你被炸死了******|n"); printf("|----------------------------|n"); DisplayBoard(mine, ROW, COL); break; } else { //如果不是雷,统计周围有几个雷 int count = GetMyCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } } else { printf("输入的坐标有误n"); } } if (win == ROW * COL - MINE) { printf("恭喜你,通关了!n"); } }

思路:设置一个循环,当排雷的次数小数没有地雷坐标个数时,循环就会继续

           用if语句判断,如果你选中坐标有地雷,那么你就被炸死了。

           如果选择的是没有地雷的,那么就会统计周围的雷的个数,这时候就又需要创建一个函数来统计周围的地雷的个数。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
GetMyCount(char mine[ROWS][COLS],int x,int y) { //周围八个位置 return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0'; }

因为布置地雷用的是字符‘1’,查询ASCII码值表后可以发现,字符‘1’减去字符‘0’后的数值就是它本身。

所以用八个坐标的总值减去八个字符‘0’,得到的数字就是周围含有地雷的个数了。

7、难度的选择设置

将你选择的时候的数字传参传入game函数中当参数来选择游戏难度

在game函数的头部写入

复制代码
1
2
3
4
5
6
7
8
9
int MINE; if (difficulty == 1) { MINE = EASY; } else { MINE = HARD; }

如果你输入的是1,选择的简单模式,那么MINE就是EASY.否则就是HARD。这里又需要宏定义一下他们代表的数字了

复制代码
1
2
3
#define EASY 10 #define HARD 20

最后,就可以跑起来了。

 感谢观看

代码如下:

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
#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 10 #define HARD 20 //菜单 void menu(); //进入游戏 void game(); //初始化数组 void InitBoard(char arr[ROW][COL], int row, int col, char set); //打印地图 void DisplayBoard(char arr[ROWS][COLS], int row, int col, char set); //布置雷 void SetMine(char board[ROWS][COLS], int row, int col, int MINE); //排雷 void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int MINE); //统计周围几个雷 GetMyCount(char mine[ROWS][COLS]);

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
141
142
143
144
145
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" //游戏菜单 void menu() { printf("***********************n"); printf("******1、简单模式******n"); printf("******2、困难模式******n"); printf("******3、退出游戏******n"); printf("***********************n"); } //初始化数组 void InitBoard(char arr[ROWS][COLS], int row, int col, char set) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { arr[i][j] = set; } } } //打印棋盘 void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("----------------------------------n"); for (i = 0; i <= col; i++) { printf("%d ", i); } printf("n"); for (i = 1; i <= row; i++) { printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("n"); } printf("----------------------------------n"); } void SetMine(char board[ROWS][COLS], int row, int col,int MINE) { int count = MINE; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } //统计雷 GetMyCount(char mine[ROWS][COLS],int x,int y) { //周围八个位置 return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0'; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int MINE) { int x = 0; int y = 0; int win = 0; while (win<ROW*COL-MINE) { printf("请输入要排查的坐标n"); scanf("%d%d", &x, &y); if (x >= 1 && x < col && y>=1 && y < row) { if (mine[x][y] == '1') { printf("|----------------------------|n"); printf("|*****很遗憾,你被炸死了******|n"); printf("|----------------------------|n"); DisplayBoard(mine, ROW, COL); break; } else { //如果不是雷,统计周围有几个雷 int count = GetMyCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } } else { printf("输入的坐标有误n"); } } if (win == ROW * COL - MINE) { printf("恭喜你,通关了!n"); } } void game(int difficulty) { int MINE; if (difficulty == 1) { MINE = EASY; } else { MINE = HARD; } //存放布置好的雷 char mine[ROWS][COLS]; //存放排查好的雷的信息 char show[ROWS][COLS]; InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); DisplayBoard(show, ROW, COL); //布置好的雷的地图不能轻易打印 //DisplayBoard(mine, ROW, COL); //布置雷 SetMine(mine, ROW, COL, MINE); FindMine(mine, show, ROW, COL,MINE); }

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
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" int main() { int secret = 0; srand((unsigned int)time(NULL)); while (1) { menu(); scanf("%d", &secret); switch (secret) { case 1: printf("|----------------------------|n"); printf("|*********简单模式***********|n"); printf("|----------------------------|n"); printf("n"); game(secret); break; case 2: printf("|----------------------------|n"); printf("|*********困难模式***********|n"); printf("|----------------------------|n"); printf("n"); game(secret); break; case 3: printf("|-------------------------------------|n"); printf("|***********退出游戏,感谢支持*********|n"); printf("|-------------------------------------|n"); return 0; default: printf("输入有误n"); break; } } return 0; }

最后

以上就是单薄羊最近收集整理的关于C语言: 扫雷小游戏---手把手基础教学的全部内容,更多相关C语言:内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部