-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.cc
556 lines (529 loc) · 17.5 KB
/
board.cc
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include <iostream>
#include "board.h"
#include "levelfour.h"
#include "block.h"
#include "starblock.h"
using namespace std;
//Board consturctor
Board::Board(){
this->highScore = 0;
}
//intialize a board
void Board::init(bool textOnlyMode){
this->theBoard.clear();
this->textDisplay = make_shared<TextDisplay>(18, 11);
this->textOnly = textOnlyMode;
if (!this->textOnly){
this->graphicsDisplay = make_shared<GraphicsDisplay>();
}
for (int i = -3; i < 0; ++i){
auto p = make_shared<Line>(i);
p->attach(this->textDisplay);
if (!this->textOnly){
p->attach(this->graphicsDisplay);
}
this->theBoard.emplace_back(p);
}
for(int i = 0; i < 15; ++i){
auto p = make_shared<Line>(i);
p->attach(this->textDisplay);
if (!this->textOnly){
p->attach(this->graphicsDisplay);
}
this->theBoard.emplace_back(p);
}
this->blockOnHold = false;
this->currentScore = 0;
if (!this->textOnly){
graphicsDisplay->drawScore(this->currentScore);
graphicsDisplay->drawHighScore(this->highScore);
graphicsDisplay->drawLevel(this->currentLevel);
}
}
bool Board::sendCurrentBlock(std::shared_ptr<Block> block, bool switchBlock){
if (switchBlock){
int prevBlockTop = this->currentBlock->getTopPos();
int prevBlockBot = this->currentBlock->getBotPos();
this->clearActive(prevBlockTop, prevBlockBot);
this->notifyDisplay(prevBlockTop, prevBlockBot);
}
this->currentBlock = block;
int blockTopPos = this->currentBlock->getTopPos();
int blockBotPos = this->currentBlock->getBotPos();
bool success = this->tryPutDown(blockTopPos, blockBotPos);
if (!success){
return false;
}
this->notifyDisplay(blockTopPos, blockBotPos);
return true;
}
void Board::sendNextBlock(std::shared_ptr<Block> block){
this->nextBlock = block;
if (!this->textOnly){
graphicsDisplay->drawNext(this->nextBlock);
}
}
//public notify function to send notification to the observers
void Board::notifyDisplay(int topPos, int botPos){
for(int i = topPos + 3; i <= botPos + 3; ++i){
this->theBoard[i]->notifyObservers();
}
}
//clear all current blocks' activity
void Board::clearActive(int topPos, int botPos){
for (int i = topPos + 3; i <= botPos + 3; ++i){
this->theBoard[i]->clearActive();
}
}
//predicate function that try to put a block down, it success then
// mutate the board and return true, else nothing change and return false
bool Board::tryPutDown(int topPos, int botPos){
vector<shared_ptr<Line>> blockComponent = this->currentBlock->getComponent();
for (int i = topPos + 3; i <= botPos + 3; ++i){
bool success = this->theBoard[i]->merge(*blockComponent[i - topPos - 3]);
if (!success){
this->clearActive(topPos, botPos);
return false;
}
}
return true;
}
//if a row is ready to clear, clear it and add corresponding score and send notification
void Board::rowClearedAddScore(int numberOfLinesCleared){
int scoreAdd = (this->currentLevel + numberOfLinesCleared) * (this->currentLevel + numberOfLinesCleared);
this->currentScore += scoreAdd;
if (this->highScore < this->currentScore){
this->highScore = this->currentScore;
}
if (!this->textOnly){
graphicsDisplay->drawScore(this->currentScore);
graphicsDisplay->drawHighScore(this->highScore);
}
}
//move the current block on the Board
void Board::move(char dir, int times){
if (times > 14){
times = 14;
}
int blockTopPos = this->currentBlock->getTopPos();
int blockBotPos = this->currentBlock->getBotPos();
int initTopPos = blockTopPos;
int initBotPos = blockBotPos;
this->clearActive(blockTopPos, blockBotPos);
bool success = true;
switch(dir){
case 'l':{
for (int i = 0; i < times; ++i){
this->clearActive(blockTopPos, blockBotPos);
this->currentBlock->doMove('l');
success = this->tryPutDown(blockTopPos, blockBotPos);
if (!success){
break;
}
}
if (!success){
this->currentBlock->doMove('r');
success = this->tryPutDown(blockTopPos, blockBotPos);
}
break;
}
case 'r':{
for (int i = 0; i < times; ++i){
this->clearActive(blockTopPos, blockBotPos);
this->currentBlock->doMove('r');
success = this->tryPutDown(blockTopPos, blockBotPos);
if (!success){
break;
}
}
if (!success){
this->currentBlock->doMove('l');
success = this->tryPutDown(blockTopPos, blockBotPos);
}
break;
}
case 'd':{
for (int i = 0; i < times; ++i){
this->clearActive(blockTopPos, blockBotPos);
this->currentBlock->verticalMove('d');
blockTopPos = this->currentBlock->getTopPos();
blockBotPos = this->currentBlock->getBotPos();
success = this->tryPutDown(blockTopPos, blockBotPos);
if (!success){
break;
}
}
if (!success){
this->currentBlock->verticalMove('u');
blockTopPos = this->currentBlock->getTopPos();
blockBotPos = this->currentBlock->getBotPos();
success = this->tryPutDown(blockTopPos, blockBotPos);
}
break;
}
}
this->notifyDisplay(initTopPos, initBotPos);
this->notifyDisplay(blockTopPos, blockBotPos);
}
//rotate the current block on the Board
void Board::rotate(char dir, int times){
int blockTopPos = this->currentBlock->getTopPos();
int blockBotPos = this->currentBlock->getBotPos();
int initTopPos = blockTopPos;
int initBotPos = blockBotPos;
this->clearActive(blockTopPos, blockBotPos);
if (dir == 'l'){
for (int i = 0; i < times; ++i){
this->currentBlock->doRotate('l');
}
} else if (dir == 'r'){
for (int i = 0; i < times; ++i){
this->currentBlock->doRotate('r');
}
}
blockTopPos = this->currentBlock->getTopPos();
blockBotPos = this->currentBlock->getBotPos();
bool success = this->tryPutDown(blockTopPos, blockBotPos);
while (!success){
if (dir == 'l'){
this->currentBlock->doRotate('r');
} else if (dir == 'r'){
this->currentBlock->doRotate('l');
}
blockTopPos = this->currentBlock->getTopPos();
blockBotPos = this->currentBlock->getBotPos();
success = this->tryPutDown(blockTopPos, blockBotPos);
}
int notifyTopPos = initTopPos;
int notifyBotPos = initBotPos;
if (initTopPos > blockTopPos){
notifyTopPos = blockTopPos;
}
if (initBotPos < blockBotPos){
notifyBotPos = blockBotPos;
}
this->notifyDisplay(notifyTopPos, notifyBotPos);
}
//predicate function that check a line is full(ready to clear) or not
bool Board::checkForFullLine(int topPos, int botPos){
vector<int> linesToBePop;
for (int i = topPos + 3; i <= botPos + 3; ++i){
if (this->theBoard[i]->isFull()){
linesToBePop.emplace_back(i);
}
}
int numberOfLinesToBePop = linesToBePop.size();
if (numberOfLinesToBePop > 0) {
this->rowClearedAddScore(numberOfLinesToBePop);
if (this->highScore < this->currentScore){
this->highScore = this->currentScore;
}
for (auto lineNumber: linesToBePop){
this->theBoard[lineNumber]->setPopState(true);
this->theBoard[lineNumber]->notifyObservers();
this->theBoard.erase(this->theBoard.begin() + lineNumber);
auto p = make_shared<Line>();
p->attach(this->textDisplay);
if (!this->textOnly){
p->attach(this->graphicsDisplay);
}
auto start = this->theBoard.begin() + 3;
this->theBoard.insert(start, p);
}
int currentLineNumber = -3;
for (auto lines: this->theBoard){
if (currentLineNumber >= 0){
lines->setLineNumber(currentLineNumber);
lines->notifyObservers();
}
++currentLineNumber;
}
this->blockClearedAddScore();
return true;
} else {
return false;
}
}
//if a block is completly cleared, clear it and add corresponding score and send notification
void Board::blockClearedAddScore(){
vector<int> blocksToBePop;
int size = this->blockOnBoard.size();
int clearTimes = 0;
for (int i = 0; i < size; ++i){
if (this->blockOnBoard[i]->isCleared()){
blocksToBePop.emplace_back(i);
++clearTimes;
}
}
for (auto blockIndex: blocksToBePop){
this->currentScore += (1 + this->blockOnBoard[blockIndex]->getLevel()) * (1 + this->blockOnBoard[blockIndex]->getLevel());
}
while (clearTimes > 0){
int index;
int remainSize = this->blockOnBoard.size();
for (int i = 0; i < remainSize; ++i){
if (this->blockOnBoard[i]->isCleared()){
index = i;
}
}
this->blockOnBoard.erase(this->blockOnBoard.begin() + index);
--clearTimes;
}
if (this->highScore < this->currentScore){
this->highScore = this->currentScore;
}
if (!this->textOnly){
graphicsDisplay->drawScore(this->currentScore);
graphicsDisplay->drawHighScore(this->highScore);
}
}
//drop the current block on the Board
bool Board::drop(bool &rowCleared){
int blockTopPos = this->currentBlock->getTopPos();
int blockBotPos = this->currentBlock->getBotPos();
if (blockBotPos == 14){
for(int i = blockTopPos + 3; i <= blockBotPos + 3; ++i){
this->theBoard[i]->setAllInactive();
this->theBoard[i]->attach(this->currentBlock);
}
this->currentBlock->setTimesNeedToPop(blockBotPos - blockTopPos + 1);
this->blockOnBoard.emplace_back(this->currentBlock);
rowCleared = this->checkForFullLine(blockTopPos, blockBotPos);
return true;
}
this->clearActive(blockTopPos, blockBotPos);
int maxDownTime = 14 - blockBotPos;
this->move('d', maxDownTime);
blockTopPos = this->currentBlock->getTopPos();
blockBotPos = this->currentBlock->getBotPos();
if (blockTopPos < 0){
return false;
}
for(int i = blockTopPos + 3; i <= blockBotPos + 3; ++i){
this->theBoard[i]->setAllInactive();
this->theBoard[i]->attach(this->currentBlock);
}
this->currentBlock->setTimesNeedToPop(blockBotPos - blockTopPos + 1);
this->blockOnBoard.emplace_back(this->currentBlock);
rowCleared = this->checkForFullLine(blockTopPos, blockBotPos);
return true;
}
//getter function that return the lower position, easy to get best hint
int Board::getLowestPos(){
int blockTopPos = this->currentBlock->getTopPos();
int blockBotPos = this->currentBlock->getBotPos();
bool success;
for (int j = 0; j < 14; ++j){
this->clearActive(blockTopPos, blockBotPos);
this->currentBlock->verticalMove('d');
blockTopPos = this->currentBlock->getTopPos();
blockBotPos = this->currentBlock->getBotPos();
success = this->tryPutDown(blockTopPos, blockBotPos);
if (!success){
break;
}
}
if (!success){
this->currentBlock->verticalMove('u');
blockTopPos = this->currentBlock->getTopPos();
blockBotPos = this->currentBlock->getBotPos();
success = this->tryPutDown(blockTopPos, blockBotPos);
}
return blockBotPos;
}
//getter function that return the position of a block with lower position, easy to get best hint
int Board::getLowestPosOnDir(char dir, int &leftEnd){
int lowestPos = 0;
for (int i = 0; i < 11; ++i){
if (i == 0){
int currentPos = this->getLowestPos();
if (lowestPos < currentPos){
lowestPos = currentPos;
leftEnd = i;
}
} else {
this->currentBlock->doMove(dir);
int currentPos = this->getLowestPos();
if (lowestPos < currentPos){
lowestPos = currentPos;
leftEnd = i;
}
}
}
int blockTopPos = this->currentBlock->getTopPos();
int blockBotPos = this->currentBlock->getBotPos();
this->clearActive(blockTopPos, blockBotPos);
return lowestPos;
}
//predicate function that try to put a hint block down to the board, if it success
// then return true, else false
bool Board::putHintDown(int moveRightTimes, int rotateTimes, int moveDownTimes){
for (int i = 0; i < rotateTimes; ++i){
this->currentBlock->doRotate('r');
}
for (int i = 0; i < moveRightTimes; ++i){
this->currentBlock->doMove('r');
}
for (int i = 0; i < moveDownTimes; ++i){
if (this->currentBlock->getBotPos() == moveDownTimes){
break;
}
this->currentBlock->verticalMove('d');
}
int topPos = this->currentBlock->getTopPos();
int botPos = this->currentBlock->getBotPos();
this->hintTopPos = topPos;
this->hintBotPos = botPos;
this->currentBlock->setHint();
bool success = this->tryPutDown(topPos, botPos);
this->notifyDisplay(topPos, botPos);
return success;
}
//give the best hint based on the current board and current block
void Board::hint(){
int currentTopPos = this->currentBlock->getTopPos();
int currentBotPos = this->currentBlock->getBotPos();
const char blockType = this->currentBlock->getType();
const char direction = 'r';
LevelFour lv;
this->clearActive(currentTopPos, currentBotPos);
this->hintBlock = this->currentBlock;
this->currentBlock = lv.doGenerate(blockType);
int leftEnd[4] = {0};
int lowPos[4] = {0};
int height[4] = {0};
int bottomFill[4] = {0};
int hintPos = 0;
int hintEnd = 0;
int hintRotate = 0;
int hintHeight = 5;
int hintBottomFill = 0;
if (blockType == 'T' || blockType == 'J' || blockType == 'L'){
lowPos[0] = getLowestPosOnDir(direction, leftEnd[0]);
height[0] = this->currentBlock->getBotPos() - this->currentBlock->getTopPos();
bottomFill[0] = this->currentBlock->getBottomFill();
for (int i = 1; i < 4; ++i){
this->currentBlock = lv.doGenerate(blockType);
for (int j = 0; j < i; ++j){
this->currentBlock->doRotate('r');
height[i] = this->currentBlock->getBotPos() - this->currentBlock->getTopPos();
bottomFill[i] = this->currentBlock->getBottomFill();
}
lowPos[i] = getLowestPosOnDir(direction, leftEnd[i]);
}
} else {
lowPos[0] = getLowestPosOnDir(direction, leftEnd[0]);
height[0] = this->currentBlock->getBotPos() - this->currentBlock->getTopPos();
bottomFill[0] = this->currentBlock->getBottomFill();
this->currentBlock = lv.doGenerate(blockType);
this->currentBlock->doRotate('r');
lowPos[1] = getLowestPosOnDir(direction, leftEnd[1]);
height[1] = this->currentBlock->getBotPos() - this->currentBlock->getTopPos();
bottomFill[1] = this->currentBlock->getBottomFill();
}
for (int i = 0; i < 4; ++i){
if (hintPos < lowPos[i]){
hintPos = lowPos[i];
hintEnd = leftEnd[i];
hintRotate = i;
hintHeight = height[i];
hintBottomFill = bottomFill[i];
} else if (hintPos == lowPos[i] && hintHeight > height[i]){
hintPos = lowPos[i];
hintEnd = leftEnd[i];
hintRotate = i;
hintHeight = height[i];
hintBottomFill = bottomFill[i];
} else if (hintPos == lowPos[i] && hintHeight == height[i] && hintBottomFill < bottomFill[i]){
hintPos = lowPos[i];
hintEnd = leftEnd[i];
hintRotate = i;
hintHeight = height[i];
hintBottomFill = bottomFill[i];
}
}
this->currentBlock = lv.doGenerate(blockType);
this->putHintDown(hintEnd, hintRotate, hintPos);
this->currentBlock = this->hintBlock;
currentTopPos = this->currentBlock->getTopPos();
currentBotPos = this->currentBlock->getBotPos();
this->tryPutDown(currentTopPos, currentBotPos);
this->notifyDisplay(currentTopPos, currentBotPos);
}
//clear the hint off the board once the player move the block
void Board::clearHint(){
for (int i = this->hintTopPos + 3; i <= this->hintBotPos + 3; ++i){
this->theBoard[i]->clearHint();
}
this->notifyDisplay(this->hintTopPos, this->hintBotPos);
}
//drop corresponding star block at harder level(4)
void Board::dropStarBlock(bool &rowCleared){
LevelFour lv;
this->currentBlock = lv.genStarBlock();
this->drop(rowCleared);
}
//setter function that change current level
void Board::levelChange(int level){
this->currentLevel = level;
if (!this->textOnly){
graphicsDisplay->drawLevel(level);
}
}
//Extra feature: hold the current block and use the next block at current turn
// player can switch the holding block back and switch it
// with the current block at that turn
bool Board::hold(shared_ptr<Level> level){
if (this->blockOnHold){
int lastTopPos = this->currentBlock->getTopPos();
int lastBotPos = this->currentBlock->getBotPos();
int blockType = this->currentBlock->getType();
this->clearActive(lastTopPos, lastBotPos);
this->currentBlock = this->holdBlock;
this->holdBlock = level->doGenerate(blockType);
this->notifyDisplay(lastTopPos, lastBotPos);
int currentTopPos = this->currentBlock->getTopPos();
int currentBotPos = this->currentBlock->getBotPos();
this->tryPutDown(currentTopPos, currentBotPos);
this->notifyDisplay(currentTopPos, currentBotPos);
if(!this->textOnly){
this->graphicsDisplay->drawHold(this->holdBlock);
}
return true;
} else {
this->blockOnHold = true;
int lastTopPos = this->currentBlock->getTopPos();
int lastBotPos = this->currentBlock->getBotPos();
int blockType = this->currentBlock->getType();
this->clearActive(lastTopPos, lastBotPos);
this->holdBlock = level->doGenerate(blockType);
this->notifyDisplay(lastTopPos, lastBotPos);
if(!this->textOnly){
this->graphicsDisplay->drawHold(this->holdBlock);
}
return false;
}
}
void Board::sendLostToGraphic(){
this->graphicsDisplay->endGame();
}
//overload function opreator<< in order to cout a board
ostream &operator<<(ostream &out, const Board &board){
out << endl;
out << "Level: " << board.currentLevel << endl;
out << "Score: " << board.currentScore << endl;
out << "Hi Score: " << board.highScore << endl;
out << "-----------" << endl;
out << (*board.textDisplay);
out << "-----------" << endl;
out << "Next: " << endl;
out << (*board.nextBlock);
out << "Hold: " << endl;
if (board.blockOnHold){
out << (*board.holdBlock);
} else {
out << endl;
}
out << endl;
return out;
}