扫雷小游戏作为初学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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169//game.h头文件 #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 9//定义了界面的长宽,可以直更改ROW,COL的值,避免了程序中数字重复出现 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASYCOUNT 10//定义了雷的个数,即游戏难度,也可以运用再测试时,比如在通关界面时,直接可以得到结果。 //初始化扫雷棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); void DispalyBoard(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); //game.c #define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)//该函数的作用是为数组初始化,设计展示的界面 { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = set//set可以更改为各种字符,随你喜欢; } } } void DispalyBoard(char board[ROWS][COLS], int row, int col)//打印数组函数 { int i = 0; int j = 0; for ( i = 0; i <=row; i++) { printf("%-2d", i); } printf("n"); for (i = 1; i <= row; i++) { printf("%d", i); for (j = 1; j <= col; j++) { printf("%2c", board[i][j]); } printf("n"); } } void SetMine(char mine[ROWS][COLS], int row, int col)//布置雷的函数 { int count = EASYCOUNT//难度,可以在头文件中更改; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y]=='0') { mine[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][y + 1] + mine[x+1][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-EASYCOUNT) { printf("请输入要排查的坐标:>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("很不幸,你被炸死了n"); DispalyBoard(mine, row, col); break; } else { int count = GetMineCount(mine,x,y); show[x][y] = count + '0'; DispalyBoard(show, row, col); win++; } if (win == row*col - EASYCOUNT) { printf("恭喜你通关了!!!n"); DispalyBoard(mine, row, col); } } else { printf("注意输入范围,请重新输入:>n"); } } } //paly.c #define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu()//菜单功能 { printf("********************n"); printf("**** 1.paly ****n"); printf("********************n"); printf("**** 0.out ****n"); } void game() { //雷的信息储存 //1.布置好的雷的信息: char mine[ROWS][COLS] = { 0 }; //2.排查出的雷的信息: char show[ROWS][COLS] = { 0 }; //初始化: InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS,'*'); //打印棋盘: //DispalyBoard(mine, ROW, COL); //DispalyBoard(show, ROW, COL); //布置雷: SetMine(mine, ROW, COL); //打印布置好的雷 //DispalyBoard(mine, ROW, COL); DispalyBoard(show, ROW, COL); //排查雷 FindMine(mine,show, ROW, COL); } void test() { srand((unsigned)time(NULL)); int input = 0; do { menu(); printf("请根据菜单选择:>n"); 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; }
菜单界面:
运行时的扫雷界面(参考坐标为玩家提供方便):
假如踩雷,游戏失败界面:
排雷完毕游戏通关的界面:
更多C++精彩小游戏请点击专题:经典游戏 进行学习
小编还为大家准备了精彩的专题:javascript经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是害羞日记本最近收集整理的关于C语言实现扫雷小游戏(适合初学者)的全部内容,更多相关C语言实现扫雷小游戏(适合初学者)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复