本文实例为大家分享了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
|
#ifndef SAOLEI_H #define SAOLEI_H class Block { friend class Saoleigame; public : Block(); bool isShown(); void setnum( int ); int getnum(); bool isbomb(); protected : int num; bool flag_show; int x; int y; }; class Saoleigame { public : Saoleigame(); ~Saoleigame(); void _init_(); void gameStart(); void reflash(); void check( int x, int y); void click( int x, int y); void gameOver(); private : Block juzheng[100]; bool flag; int b[10]; unsigned int score; }; #endif |
以上是编写的头文件
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
|
#include<iostream> #include<cstdlib> #include<ctime> #include"Saolei.h" using namespace std; Saoleigame::Saoleigame() { _init_(); flag = true ; score = 0; } Saoleigame::~Saoleigame() { } void Saoleigame::_init_() { srand ( time (NULL)); for ( int i = 0; i < 10; i++ ) { b[i] = -1; } for ( int i = 0; i < 10; i ++ ) { bool temp_flag = false ; do { int temp = (unsigned int ) rand ()%100; for ( int j = 0; j < i; j ++) { if (temp == b[i]) { temp_flag = true ; } } if (!temp_flag) { b[i] = temp; } } while (temp_flag); } for ( int i = 0; i < 10; i++ ) { juzheng[b[i]].setnum(-1); } for ( int i = 0; i < 10; i ++ ) { for ( int j = 0; j < 10;j++) { juzheng[i*10+j].x = i+1; juzheng[i*10+j].y = j+1; } } for ( int m = 0; m < 10; m ++ ) { for ( int n = 0; n < 10 ; n++ ) { check(m + 1, n + 1); } } } void Saoleigame::check( int x, int y) { if (juzheng[(x - 1)*10 + (y - 1)].num == -1) return ; int trans = (x - 1)*10 + (y - 1); int number = 0; for ( int i = -1; i < 2; i ++ ) { for ( int j = -1; j < 2; j ++) { if (!(x + i<= 0 && x + i >= 10 || j + y <= 0 && y + j >= 10)) { if (juzheng[(x + i - 1)*10 + (y + j - 1)].num == -1) number ++; } } } juzheng[(x - 1)*10 + (y - 1)].setnum(number); } void Saoleigame::click( int x, int y) { if (juzheng[(x- 1)*10 + (y - 1)].num == 0) { for ( int i = -1; i < 2; i ++ ) { for ( int j = -1; j < 2; j ++) { if (!((x + i<= 0 || x + i > 10 )|| (j + y <= 0 || y + j > 10)) && !(i == 0&& j ==0) && !juzheng[(x+i- 1)*10 + (y +j- 1)].flag_show){ juzheng[(x+i- 1)*10 + (y +j- 1)].flag_show = true ; click(x + i, y + j); } } } } juzheng[(x- 1)*10 + (y - 1)].flag_show = true ; return ; } void Saoleigame::gameStart() { do { reflash(); int x, y; cout<< "input the position: " ; cin >> x>> y; if (juzheng[(x-1)*10 + (y-1)].isbomb()) { gameOver(); return ; } else { click(x , y); } } while (flag); } void Saoleigame::reflash() { system ( "cls" ); score = 0; cout<< " 扫雷" <<endl; cout<< " 1 2 3 4 5 6 7 8 9 10" <<endl; cout<< " -------------------" <<endl; for ( int i = 0; i < 100; i ++ ) { if (i%10 == 0) { if (i /10 + 1 == 10)cout<<10<< "|" ; else cout<<i /10 + 1<< " |" ; } if (juzheng[i].isShown()) { if (juzheng[i].isShown() && (juzheng[i].getnum())!=-1) { score ++; } if ((juzheng[i].getnum())==-1)cout << "*" << "|" ; else cout <<juzheng[i].getnum()<< "|" ; } else { cout<< " |" ; } if ((i+1)%10 == 0)cout<<endl; } cout<< " -------------------" <<endl; cout<< "score:" <<(score*100)/95<<endl; } void Saoleigame::gameOver() { for ( int i = 0 ; i < 10 ; i++ ) { juzheng[b[i]].flag_show = true ; } reflash(); cout<< "Game Over" <<endl<<endl; flag = false ; } Block::Block() { flag_show = false ; num = 0; } bool Block::isShown() { return flag_show; } void Block::setnum( int _num) { num = _num; } int Block::getnum() { return num; } bool Block::isbomb() { return num == -1; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。