我是靠谱客的博主 精明蜜粉,这篇文章主要介绍利用C语言实现扫雷游戏,现在分享给大家,希望可以做个参考。

通过一段时间的C语言学习,想必小伙伴们也想跃跃欲试的编写一些小程序,这个扫雷简易游戏,非常适合C语言初学者去实践。

实现扫雷,首先要有两个棋盘,一个棋盘放置着雷的信息,另个用于展示到屏幕上;然后就是玩家输入坐标排雷,如果输入的坐标有雷,就游戏结束,要是没有雷就在展示棋盘对应的位置,显示出周边雷的个数;最后排查完所有坐标并且不被雷炸死,就是胜利。

以上是扫雷的基本逻辑,现在开始,一步一步的实现。

1.初始化棋盘

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

mine就是放雷的棋盘,show就是展示出来棋盘。我们把mine都初始化成‘0',雷为‘1',show

都初始化为‘*'。

2.布置雷

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT;//10个雷 while (count) { //1. 生成随机下标 int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } }

3.排查雷

排查雷是本游戏关键,也稍有难度,首先,需要将俩个棋盘都传过去,每次输入坐标都会去雷盘里找,如果是雷就游戏结束,不是雷就会使对应的展示盘的位置 展现出周围的雷数。

复制代码
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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0; while (win<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"); DisplayBoard(mine, ROW, COL); break; } else { int count = GetMineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } } else { printf("坐标非法,重新输入n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功n"); DisplayBoard(mine, ROW, COL); } }

看(x,y)周围雷数的函数:

复制代码
1
2
3
4
5
6
7
8
9
10
11
int GetMineCount(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.头文件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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);

2.游戏代码

复制代码
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
#include "game.h" void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) { board[i][j] = set; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; printf("------------------------n"); for (i = 0; i <= 9; i++) { printf("%d ", i); } printf("n"); for (i = 1; i <= row; i++) { int j = 0; 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 count = EASY_COUNT; while (count) { //1. 生成随机下标 int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } } int GetMineCount(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 x = 0; int y = 0; int win = 0; while (win<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"); DisplayBoard(mine, ROW, COL); break; } else { int count = GetMineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } } else { printf("坐标非法,重新输入n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功n"); DisplayBoard(mine, ROW, COL); } }

3.总程序

复制代码
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
#include "game.h" void menu() { printf("********************************n"); printf("********* 1. play ********n"); printf("********* 0. exit ********n"); printf("********************************n"); } void game() { char mine[ROWS][COLS] = { 0 };//存放雷的信息 char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息 //初始化一下棋盘 InitBoard(mine, ROWS, COLS, '0');//'0' InitBoard(show, ROWS, COLS, '*');//'*' //布置雷 SetMine(mine, ROW, COL); DisplayBoard(show, ROW, COL); //排查雷 FindMine(mine, show, ROW, COL); } 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; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是精明蜜粉最近收集整理的关于利用C语言实现扫雷游戏的全部内容,更多相关利用C语言实现扫雷游戏内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部