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
|
//扫雷的类的定义 #pragma once class Game{ public : //开始游戏 void play(); //退出游戏 int quit(); //游戏规则 void rule(); private : //踩雷次数,作为失败条件 int error = 0; //分数 int score = 0; //最高分记录 int Rocord[5] = { 0,0,0,0,0 }; //地图 int map[40][40]; //地图的大小Size*Size int Size = 10; //容错 int fault_tolerant = 10; //困难程度 int _difficulty=1; //初始化 void reset(); //画地图 void drawGrid(); //查看格子的结果 void Cheak(); //判断是否游戏结束 int isWin(); //导入最高分记录 void get_Rocord(); //导出最高分记录 void put_Rocord(); //选择难度 int Selection_difficulty(); //加载界面 void loading(); }; |
然后是对类的函数的定义
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
//对Game类的成员函数的定义 #include "扫雷.h" #include<Windows.h> #include<iostream> #include<fstream> #include<time.h> #include <conio.h> #pragma warning(disable:4996) //这一行是为了能在 Visual Studio 2017内使用getch()函数 //定义最高分记录的存储地址 #define RocordPath "D:\\VS/扫雷最高分.txt" using namespace std; #define none "█" //定义5种情况,有雷和无雷,查看后的三种结果 enum players { Boom, None, Boom1, None1, Show1 }; //定义三种游戏难度 enum _Difficulty{Easy,General,Difficulty,Purgatory}; int D_size[4][2] = { {10,10} ,{15,8},{20,5},{30,3} }; //游戏规则的描述 void Game::rule() { loading(); //清屏 system ( "cls" ); cout << "\n\n\n\n" ; cout << "游戏规则:\n\n" ; cout << "1.当查看点为雷时,会显示“*”,并且将扣10分" << endl; cout << "2.当差看点不为雷且周围均无雷将显示周围所以格为“ ”(周围指的是相邻的8个格子)" << endl; cout << "3.当查看点不为雷,且周围存在雷时,将显示周围雷数" << endl; cout << "4.当踩到最大容错个数雷时游戏将失败" << endl; cout << "5.当不存在未查阅的非雷格时游戏胜利" << endl; cout << "\n\n\n\t\t\t\t30秒后自动退出该界面!" ; Sleep(30000); } //退出游戏 int Game::quit() { system ( "cls" ); //定义控制台屏幕初始坐标 COORD c = { 40, 13 }; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "游戏结束!!!" << endl; Sleep(1000); loading(); return 0; } //游戏模式 void Game::play() { //导入最高分记录 get_Rocord(); while ( true ) { //选择游戏难度 _difficulty=Selection_difficulty(); //默认游戏一直进行 int res = 1; //初始化 reset(); // drawGrid(); while ( true ) { //查看点 Cheak(); // drawGrid(); if (!isWin()) { if (score > Rocord[_difficulty])Rocord[_difficulty] = score; put_Rocord(); char s; cout << "是否再来一局?是(y|Y)/否(n|N)" << endl; cin >> s; if (s == 'y' || s == 'Y' )res = 1; else res = 0; break ; } } if (!res) break ; } } //更新(初始化) void Game::reset() { //数据初始化 score = 0; error = 0; //棋盘初始化 srand ( time (NULL)); for ( int i = 0; i < Size; i++) { for ( int j = 0; j < Size; j++) { int t = rand () % 2; if (t==1)map[j][i] = Boom; else map[j][i] = None; //cout << t<< " "; } //cout << endl; } } //画地图 void Game::drawGrid() { //清屏 system ( "cls" ); //定义控制台屏幕初始坐标 COORD c = { 0, 2 }; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); //棋局初始状态 for ( int i = 0; i <= Size; i++) { if (i < 10) cout << i << " " ; else cout << i; for ( int j = 0; j < Size; j++) { if (i == 0) { if (j < 9) cout << j + 1 << " " ; else cout << j + 1; } else cout << none; } cout << endl; } for ( int y = 0; y < Size; y++) { for ( int x = 0; x < Size; x++) { if (map[x][y] == Boom1|| map[x][y] == None1) { //光标位置坐标 COORD c = { x * 2 + 2, 3 + y }; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); //GetStdHandle函数获得句柄 string o; if (map[x][y] == Boom1) o = "* " ; if (map[x][y] == None1) o = " " ; cout << o; } if (map[x][y] == Show1) { int cnt = 0; for ( int i = x - 1; i <= x + 1; i++) { for ( int j = y - 1; j <= y + 1; j++) { if (i >= 0 && i < Size && j >= 0 && j < Size) { if (map[i][j] == Boom || map[i][j] == Boom1)cnt++; } } } //光标位置坐标 COORD c = { x*2+2, 3 + y }; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); //GetStdHandle函数获得句柄 cout << cnt << " " ; } } } c.Y = Size+3; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "当前分数是:" <<score<< "\n最高纪录是:" <<Rocord[_difficulty]<< "\n请输入查看格的坐标" << endl; } //查看点结果 void Game::Cheak() { int x = 0, y = 0; cin >> x >> y; x -= 1, y -= 1; while (map[x][y] == Boom1 || map[x][y] == None1 || map[x][y] == Show1 || x < 0 || x >= Size || y < 0 || y >= Size) { //定义控制台屏幕初始坐标 COORD c = { 0, 2 }; c.Y = Size+6; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "该格以检查过或不在棋盘内,请重新输入" << endl; cin >> x >> y; x -= 1, y -= 1; } if (map[x][y] == Boom) { map[x][y] = Boom1; score -= 10; error++; } else { score += 10; int cnt = 0; for ( int i = x - 1; i <= x + 1; i++) { for ( int j = y - 1; j <= y + 1; j++) { if (i >= 0 && i < Size && j >= 0 && j < Size) { if (map[i][j] == Boom || map[i][j] == Boom1)cnt++; } } } if (cnt == 0) { for ( int i = x - 1; i <= x + 1; i++) { for ( int j = y - 1; j <= y + 1; j++) { if (i >= 0 && i < Size && j >= 0 && j < Size) { map[i][j] = None1; } } } } else map[x][y] = Show1; } } //判断是否游戏结束 int Game::isWin() { int cnt = 0; for ( int i = 0; i < Size; i++) { for ( int j = 0; j < Size; j++) { if (map[i][j] == None)cnt++; } } if (cnt == 0) { system ( "cls" ); //定义控制台屏幕初始坐标 COORD c = { 50, 15 }; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "You Win!!!" << endl; return 0; } else if (error >= fault_tolerant) { system ( "cls" ); //定义控制台屏幕初始坐标 COORD c = { 50, 15 }; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "You Loss!!!" << endl; return 0; } else return 1; } //导入最高分记录 void Game::get_Rocord() { ifstream fin(RocordPath, ios::in); for ( int i = 0; i < 5; i++) { fin >> Rocord[i]; } } //导出最高分记录 void Game::put_Rocord() { ofstream fout(RocordPath, ios::out); for ( int i=0;i<5;i++) fout << Rocord[i] << endl; } //选择难度 int Game::Selection_difficulty() { //清屏 system ( "cls" ); //定义控制台屏幕初始坐标 COORD c = { 0, 6 }; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "\t\t\t\t\t\t1.简单 (10*10格 10容错)\n\n" << endl; cout << "\t\t\t\t\t\t2.一般 (15*15格 8容错)\n\n" << endl; cout << "\t\t\t\t\t\t3.困难 (20*20格 5容错)\n\n" << endl; cout << "\t\t\t\t\t\t4.炼狱 (30*30格 3容错)\n\n" << endl; cout << "\t\t\t\t\t\t5.自定义\n\n" << endl; cout << "\t\t\t\t\t\t请选择游戏难度:" ; int t = 1; cin >> t; while (t < 1 || t>5) { COORD c = { 0, 21 }; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "\t\t\t\t\t\t输入错误请重新输入:" << endl;; cin >> t; } switch (t) { case 1:Size = D_size[Easy][0], fault_tolerant = D_size[Easy][1]; break ; case 2:Size = D_size[General][0], fault_tolerant = D_size[General][1]; break ; case 3:Size = D_size[Difficulty][0], fault_tolerant = D_size[Difficulty][1]; break ; case 4:Size = D_size[Purgatory][0], fault_tolerant = D_size[Purgatory][1]; break ; case 5: { //清屏 system ( "cls" ); cout << "\n\n\n\n\n\t\t\t\t请输入地图尺码和最多踩雷失败数 (尺码在10-30,容错在10以内)" ; cout << "\t\t\t\t\t\t\t\t\t尺码:" ; cin >> Size; cout << "\n\t\t\t\t\t容错:" ; cin >> fault_tolerant; } break ; } loading(); return t; } void Game::loading() { COORD c = { 50,15 }; //设置控制台光标位置 int t = 6; while (t--) { system ( "cls" ); SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); if (t%3==0) cout << "loading..." << endl; if (t % 3 == 1) cout << "loading.." << endl; if (t % 3 == 2) cout << "loading." << endl; Sleep(500); } } |
最后就是主函数部分
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
|
//扫雷游戏的主函数 #include<iostream> #include<Windows.h> #include"扫雷.h" using namespace std; int main() { Game game; while ( true ) { int t, g = 1; system ( "cls" ); //定义控制台屏幕初始坐标 COORD c = { 30, 10 }; //设置控制台光标位置 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); cout << "欢迎来到 扫雷!!!\n\n\n\n\n\n" ; cout << "\t\t\t\t\t1.开始游戏\n\n\n\t\t\t\t\t2.阅读规则\n\n\n\t\t\t\t\t3.退出" << endl; cin >> t; switch (t) { case 1:game.play(); break ; case 2:game.rule(); break ; case 3:g=game.quit(); break ; } if (g == 0) break ; } return 0; } |
这是第一次写博客 也是第一次独立完成项目,有不足的地方,希望各位大牛指教。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_46058158/article/details/107463506