-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgame_model.cpp
460 lines (390 loc) · 12.5 KB
/
game_model.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include "game_model.h"
// 游戏逻辑模型,与界面分离
GameModel::GameModel() :
gameStatus(PLAYING),
gameLevel(BASIC)
{
}
GameModel::~GameModel()
{
if(gameMap)
{
free(gameMap);
free(hintArray);
gameMap = NULL;
}
}
void GameModel::startGame(GameLevel level)
{
// 用C的方式初始化数组,怀旧一下~
gameMap = (int *)malloc(sizeof(int) * MAX_ROW * MAX_COL);
memset(gameMap, 0, MAX_ROW * MAX_COL);
for (int i = 0; i < MAX_ROW * MAX_COL; i++) // 必须这样赋初值,memset容易出问题
gameMap[i] = 0;
hintArray = (int *)malloc(sizeof(int) * 4);
memset(hintArray, 0, 4);
for (int i = 0; i < 4; i++)
hintArray[i] = -1;
gameStatus = PLAYING;
gameLevel = level;
int gameLevelNum;
switch (gameLevel)
{
case BASIC:
gameLevelNum = kBasicNum;
break;
case MEDIUM:
gameLevelNum = kMediumNum;
break;
case HARD:
gameLevelNum = kHardNum;
}
// 填充方块标号
int iconID = 0;
for(int i = 0; i < gameLevelNum; i += 2)
{
// 每次填充连着的两个,图片用尽了就循环
gameMap[i] = iconID % MAX_ICON + 1;
gameMap[i + 1] = iconID % MAX_ICON + 1;
iconID++;
}
// 打乱方块
srand((unsigned)time(0));
for(int i = 0; i < MAX_ROW * MAX_COL; i++)
{
int randomID = rand() % (MAX_ROW * MAX_COL);
std::swap(gameMap[i], gameMap[randomID]);
}
// 初始化判断模式
isFrozenMode = false;
// 初始化绘制点
paintPoints.clear();
}
// 重载
void GameModel::startGame()
{
startGame(gameLevel);
}
int *GameModel::getGameMap()
{
return gameMap;
}
bool GameModel::isFrozen()
{
// 暴力法,所有方块两两判断是否可以连接
// 每次消除后做一次判断
// 其实在这个过程中记录提示
for (int i = 0; i < MAX_ROW * MAX_COL - 1; i++)
for( int j = i + 1; j < MAX_ROW * MAX_COL; j++)
{
int srcX = i % MAX_COL;
int srcY = i / MAX_COL;
int dstX = j % MAX_COL;
int dstY = j / MAX_COL;
// 只要能找到可以连接的就不为僵局
isFrozenMode = true;
if (isCanLink(srcX, srcY, dstX, dstY))
{
// 记录第一个可以连接的hint
hintArray[0] = srcX;
hintArray[1] = srcY;
hintArray[2] = dstX;
hintArray[3] = dstY;
isFrozenMode = false;
return false;
}
}
isFrozenMode = false;
return true;
}
bool GameModel::isWin()
{
for (int i = 0; i < MAX_ROW * MAX_COL; i++)
{
if (gameMap[i])
return false;
}
gameStatus = WIN;
return true;
}
int *GameModel::getHint()
{
return hintArray;
}
// 最重要的判断连接算法
bool GameModel::canLinkDirectly(int srcX, int srcY, int dstX, int dstY)
{
// 竖线
if (srcX == dstX)
{
if (srcY > dstY)
std::swap(srcY, dstY);
for (int y = srcY + 1; y < dstY; y++)
if (gameMap[MAX_COL * y + srcX])
return false;
if (!isFrozenMode) // 这里有坑,注意了
{
// 记录点和路线
PaintPoint p1(srcX, srcY), p2(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
}
return true;
}
// 横线
if (srcY == dstY)
{
if (srcX > dstX)
std::swap(srcX, dstX);
for (int x = srcX + 1; x < dstX; x++)
if (gameMap[MAX_COL * srcY + x])
return false;
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
}
return true;
}
return false;
}
bool GameModel::canLinkWithOneCorner(int srcX, int srcY, int dstX, int dstY)
{
if (srcX > dstX)
{
// 统一化,方便后续处理
std::swap(srcX, dstX);
std::swap(srcY, dstY);
}
// 先确定拐点,再确定直连线路,2种情况,4个点,每种情况逐个试,所以多个if顺序执行
if (dstY > srcY)
{
if (gameMap[srcY * MAX_COL + dstX] == 0)
{
// 右上角
if (canLinkDirectly(srcX, srcY, dstX, srcY) && canLinkDirectly(dstX, srcY, dstX, dstY))
{
// 只有连接模式才记录点
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(dstX, srcY), p3(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
}
return true;
}
}
if (gameMap[dstY * MAX_COL + srcX] == 0)
{
// 左下角
if (canLinkDirectly(srcX, srcY, srcX, dstY) && canLinkDirectly(srcX, dstY, dstX, dstY))
{
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(srcX, dstY), p3(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
}
return true;
}
}
}
else
{
if (gameMap[dstY * MAX_COL + srcX] == 0)
{
// 左上角
if (canLinkDirectly(srcX, srcY, srcX, dstY) && canLinkDirectly(srcX, dstY, dstX, dstY))
{
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(srcX, dstY), p3(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
}
return true;
}
}
if (gameMap[srcY * MAX_COL + dstX] == 0)
{
// 右下角
if (canLinkDirectly(srcX, srcY, dstX, srcY) && canLinkDirectly(dstX, srcY, dstX, dstY))
{
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(dstX, srcY), p3(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
}
return true;
}
}
}
return false;
}
bool GameModel::canLinkWithTwoCorner(int srcX, int srcY, int dstX, int dstY)
{
if (srcX > dstX)
{
// 统一化,方便后续处理
std::swap(srcX, dstX);
std::swap(srcY, dstY);
}
// 两种情况,横向垂线和竖向垂线,以src点作为基准遍历,双折线由直线和一个拐点的折线构成
// 常规情况
for (int y = 0; y < MAX_ROW; y++)
{
if (y != srcY && y != dstY)
{
if (gameMap[y * MAX_COL + srcX] == 0
&& canLinkDirectly(srcX, srcY, srcX, y)
&& canLinkWithOneCorner(srcX, y, dstX, dstY))
{
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(srcX, y), p3(dstX, y), p4(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
paintPoints.push_back(p4);
}
return true;
}
}
}
for (int x = 0; x < MAX_COL; x++)
{
if (x != srcX && x != dstX)
{
if (gameMap[srcY * MAX_COL + x] == 0
&& canLinkDirectly(srcX, srcY, x, srcY)
&& canLinkWithOneCorner(x, srcY, dstX, dstY))
{
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(x, srcY), p3(x, dstY), p4(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
paintPoints.push_back(p4);
}
return true;
}
}
}
// 边缘情况,从外边缘连接,注意方块不一定在边缘,(分开写便于记录路径)
if ((srcX == 0 || gameMap[srcY * MAX_COL + 0] == 0 && canLinkDirectly(srcX, srcY, 0, srcY))
&& (dstX == 0 || gameMap[dstY * MAX_COL + 0] == 0 && canLinkDirectly(0, dstY, dstX, dstY)))
{
// 左
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(-1, srcY), p3(-1, dstY), p4(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
paintPoints.push_back(p4);
}
return true;
}
if ((srcX == MAX_COL - 1 || gameMap[srcY * MAX_COL + MAX_COL - 1] == 0 && canLinkDirectly(srcX, srcY, MAX_COL - 1, srcY))
&& (dstX == MAX_COL - 1 || gameMap[dstY * MAX_COL + MAX_COL - 1] == 0 && canLinkDirectly(MAX_COL - 1, dstY, dstX, dstY)))
{
// 右
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(MAX_COL, srcY), p3(MAX_COL, dstY), p4(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
paintPoints.push_back(p4);
}
return true;
}
if ((srcY == 0 || gameMap[srcX] == 0 && canLinkDirectly(srcX, srcY, srcX, 0))
&& (dstY == 0 || gameMap[dstX] == 0 && canLinkDirectly(dstX, 0, dstX, dstY)))
{
// 上
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(srcX, -1), p3(dstX, -1), p4(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
paintPoints.push_back(p4);
}
return true;
}
if ((srcY == MAX_ROW - 1 || gameMap[(MAX_ROW - 1) * MAX_COL + srcX] == 0 && canLinkDirectly(srcX, srcY, srcX, MAX_ROW - 1))
&& (dstY == MAX_ROW - 1 || gameMap[(MAX_ROW - 1) * MAX_COL + dstX] == 0 && canLinkDirectly(dstX, MAX_ROW - 1, dstX, dstY)))
{
// 下
if (!isFrozenMode)
{
PaintPoint p1(srcX, srcY), p2(srcX, MAX_ROW), p3(dstX, MAX_ROW), p4(dstX, dstY);
paintPoints.clear();
paintPoints.push_back(p1);
paintPoints.push_back(p2);
paintPoints.push_back(p3);
paintPoints.push_back(p4);
}
return true;
}
return false;
}
bool GameModel::isCanLink(int srcX, int srcY, int dstX, int dstY)
{
// 首先判断点击的两个方块不是同一个不是空,且方块相同
// 判断方块是否可以连,可用于实际的连接消除和提示消除
// x表示横向索引,y表示纵向索引,从0开始
// 分3种情况往下找,每一种都可以用前面简单情况组合找到一种情况可以连通就返回true,并选用这种连接情况
if (gameMap[srcY * MAX_COL + srcX] == 0 || gameMap[dstY * MAX_COL + dstX] == 0)
return false;
if (srcX == dstX && srcY == dstY)
return false;
if(gameMap[MAX_COL * srcY + srcX] != gameMap[MAX_COL * dstY + dstX])
return false;
// 情况1:横向或者竖向可以直线连通
if (canLinkDirectly(srcX, srcY, dstX, dstY))
return true;
// 情况2:一次拐弯可以连通
if (canLinkWithOneCorner(srcX, srcY, dstX, dstY))
return true;
// 情况3:两次拐弯可以连通
if (canLinkWithTwoCorner(srcX, srcY, dstX, dstY))
return true;
return false;
}
// 点击方块进行连接操作
bool GameModel::linkTwoTiles(int srcX, int srcY, int dstX, int dstY)
{
// 成功连接就返回true否则false用于GUI里面判断
if(isCanLink(srcX, srcY, dstX, dstY))
{
// 值重置
gameMap[MAX_COL * srcY + srcX] = 0;
gameMap[MAX_COL * dstY + dstX] = 0;
return true;
}
return false;
}