-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameMap.cpp
312 lines (301 loc) · 5.82 KB
/
GameMap.cpp
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
#include "GameMap.h"
//构造函数
GameMap::GameMap() //默认构造函数
{
this->_Sq = nullptr;
this->_GameCount = 0;
this->_WinCount=0;
this->_Mode = NOMODE;
this->_Length = 0;
this->_Width = 0;
this->_BombNumber = 0;
}
GameMap::GameMap(const int& _M)
{
this->CreateMap(_M);
this->_GameCount = 0;
this->_WinCount = 0;
}
GameMap::GameMap(const int& _W, const int& _L, const int& _N)
{
this->CreateMap(_W, _L, _N);
this->_GameCount = 0;
this->_WinCount = 0;
}
//析构函数
GameMap::~GameMap() //析构函数
{
if (this->_Sq != nullptr)
{
for (int i = 0; i < this->_Width; i++)
{
for (int j = 0; j < this->_Length; j++)
{
delete this->_Sq[i][j];
}
delete[] this->_Sq[i];
}
delete this->_Sq;
this->_Sq = nullptr;
this->_GameCount = 0;
this->_WinCount = 0;
this->_Mode = 0;
this->_Length = 0;
this->_Width = 0;
this->_BombNumber = 0;
}
}
//主动销毁地图
void GameMap::DeleteMap()
{
for (int i = 0; i < this->_Width; i++)
{
for (int j = 0; j < this->_Length; j++)
{
delete this->_Sq[i][j];
}
delete[] this->_Sq[i];
}
delete this->_Sq;
this->_Sq = nullptr;
//重设地图属性
this->_Mode = NOMODE;
this->_Length = 0;
this->_Width = 0;
this->_BombNumber = 0;
}
//构造地图
void GameMap::CreateMap(const int& _M) //Easy,Medium,Hard
{
switch (_M)
{
case 0: //简单模式
this->_GameCount++;
this->_Width = EASY;
this->_Length = EASY;
this->_BombNumber = EASYNUM;
this->_Mode = EASYMODE;
this->CreateMap(EASY, EASY, EASYNUM);
break;
case 1: //普通模式
this->_GameCount++;
this->_Width = MEDIUM;
this->_Length = MEDIUM;
this->_BombNumber = MEDIUMNUM;
this->_Mode = MEDIUMMODE;
this->CreateMap(MEDIUM, MEDIUM, MEDIUMNUM);
break;
case 2: //困难模式
this->_GameCount++;
this->_Width = MEDIUM;
this->_Length = HARD;
this->_BombNumber = HARDNUM;
this->_Mode = HARDMODE;
this->CreateMap(MEDIUM, HARD, HARDNUM);
break;
}
}
void GameMap::CreateMap(const int& _W,const int& _L,const int& _N) //DefaultMode
{
//检测游戏模式
if (this->_Mode == NOMODE)
{
this->_Mode = DEFAULTMODE;
}
//三级指针创建二维数组
this->_Sq = new Square * *[_W];
for (int i = 0; i < _W; i++)
{
this->_Sq[i] = new Square * [_L];
for (int j = 0; j < _L; j++)
{
Square * square = new Square(false);
this->_Sq[i][j] = square;
}
}
//放置地雷
this->PutBomb();
}
//放置地雷
void GameMap::PutBomb()
{
int i = 0;
srand((unsigned)time(NULL));
while (i<this->_BombNumber)
{
int x = rand() % this->_Width;
int y = rand() % this->_Length;
if (this->_Sq[x][y]->ShowB() == false)
{
i++;
this->_Sq[x][y]->ChangeB();
//修改周围地雷计数
SquareList* S = new SquareList(*this,x, y);
(*S)++;
delete S;
}
}
}
//显示地图
void GameMap::ShowMap()
{
cout.setf(ios_base::internal, ios_base::adjustfield);
cout << " ";
for (int i = 0; i < _Length; i++)
{
cout << setfill('0') << setw(2) << i << setfill(' ') << ' ';
}
cout << endl;
for (int i = 0; i < _Width; i++)
{
cout << setfill('0') << setw(2) << setbase(10)
<< i << setfill(' ') << ' ';
for (int j = 0; j < _Length; j++)
{
cout << *(this->_Sq[i][j]) << ' ';
}
cout << endl;
}
}
//SL地图
void GameMap::SaveMap()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
ofs << _Width << " " << _Length << " " << _Mode << " " << _BombNumber << endl;
for (int i = 0; i < _Width; i++)
{
for (int j = 0; j < _Length; j++)
{
ofs << *(this->_Sq[i][j]);
}
}
ofs.close();
}
void GameMap::LoadMap()
{
ifstream ifs;
ifs.open(FILENAME, ios_base::in);
int w, l, m, b;
ifs >> w >> l >> m >> b;
this->_Width = w; this->_Length = l; this->_Mode = m; this->_BombNumber = b;
//三级指针创建二维数组
this->_Sq = new Square * *[this->_Width];
for (int i = 0; i < this->_Width; i++)
{
this->_Sq[i] = new Square * [this->_Length];
int B, F, L, N;
for (int j = 0; j < this->_Length; j++)
{
ifs >> B >> F >> L >> N;
Square* square = new Square(B,F,L,N);
this->_Sq[i][j] = square;
}
}
this->_GameCount++;
//关闭地图
ifs.close();
}
//点亮格子
void GameMap::LightS(int i, int j)
{
try
{
if (i < 0 || i >= this->_Width || j < 0 || j >= this->_Length)
throw out_of_range(0);
if (_Sq[i][j]->ShowF()) return; //010N,1100 // 检查Flag
else
{
if (_Sq[i][j]->ShowB()) throw runtime_error(0); //1000 //检查Bomb
else
{
if (!_Sq[i][j]->ShowL()) //000N //检查Light
{
_Sq[i][j]->ChangeL();
if (_Sq[i][j]->ShowBN()) return; //N!=0 //检查BN
else //N==0
{
SquareList* S =new SquareList(*this,i, j);
S->Light(*this);
delete S;
}
}
else //001N
{
if (!_Sq[i][j]->ShowBN()) return; //0010
else //001N
{
SquareList* S = new SquareList(*this, i, j);
if (_Sq[i][j]->ShowBN() == S->Count()) //检查Flag数量与地雷数量是否相等
{
S->Light(*this);
delete S;
}
else { delete S; return; }
}
}
}
}
}
catch (const out_of_range&) { return; };
}
//标记格子
void GameMap::MarkS(int i, int j)
{
try
{
if (!this->_Sq[i][j]->ShowL())
{
this->_Sq[i][j]->ChangeF();
return;
}
}
catch (const std::exception&)
{
return;
}
}
//检测点亮数量
bool GameMap::Count()
{
int x = 0;
//记录点亮格子数量
for (int i = 0; i < this->_Width; i++)
for (int j = 0; j < this->_Length; j++)
if (this->_Sq[i][j]->ShowL())
x++;
return this->_Width * this->_Length - this->_BombNumber == x;
}
//显示地图属性
const int& GameMap::ShowGameCount() const
{
return this->_GameCount;
}
int& GameMap::ShowGameCount()
{
return this->_GameCount;
}
const int& GameMap::ShowWinCount() const
{
return this->_WinCount;
}
int& GameMap::ShowWinCount()
{
return this->_WinCount;
}
const int& GameMap::ShowMode() const
{
return this->_Mode;
}
const int& GameMap::ShowLength() const
{
return this->_Length;
}
const int& GameMap::ShowWidth() const
{
return this->_Width;
}
const int& GameMap::ShowBombNumber() const
{
return _BombNumber;
}