-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.cc
193 lines (160 loc) · 4.23 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
#include <cmath>
#include <string>
#include "board.h"
using namespace std;
const int width = 11;
const int height = 18;
Board::Board() : currBlock{nullptr},nextBlock{nullptr}, level{0}, lastClear{0}, blind{false}, heavy{false} {
init();
}
void Board::init(){
for(int i = 0; i < height; ++i){
vector<Cell *> vec;
for(int j = 0; j < width; ++j){
Cell * newCell = new Cell(i,j);
vec.push_back(newCell);
}
theGrid.push_back(vec);
}
}
void Board::clearBoard(){
delete currBlock; //Delete the currBlock pointer in the grid
currBlock = nullptr;
delete nextBlock;
nextBlock = nullptr;
for (auto & block : blockList) {
delete block;
}
blockList.clear();
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
delete theGrid[i][j]; //Delete the cell pointers in the grid
}
}
theGrid.clear();
}
bool Board::isRowFull(int row){
for(int j = 0; j < width; ++j){
if (!theGrid[row][j]->isFilled()){
return false;
}
}
return true;
}
void Board::clearRow(int row){
for(int j = 0; j < width; ++j){
theGrid[row][j]->setLetter("");
}
}
int Board::dropBlock(int & linesCleared){ // Called from AbstractGame
int numLines = 0;
int scoredPoints = 0;
currBlock->drop();
blockList.push_back(currBlock);
int blockRow = currBlock->getBottomLeft()->getRow();
while(isRowFull(blockRow)){
numLines += 1; //Increment number of lines
copyRow(blockRow - 1, blockRow);
scoredPoints += updateBlockList();
for(int i = blockRow - 1; i > 2; --i){
copyRow(i - 1, i);
}
}
//Make sure the dropped currBlock doesn't get deleted
currBlock = nullptr;
// reset special actions
blind = false;
heavy = false;
if (numLines > 0){
if (level == 4) {
lastClear = 0;
}
linesCleared = numLines;
scoredPoints += pow(numLines + level, 2);
return scoredPoints;
}else{
if (level == 4) {
lastClear++;
}
return 0;
}
}
Board::~Board(){
this->clearBoard();
}
string Board::getLine(int row){
string res = "";
for(int j = 0; j < width; ++j){
if (blind && row >= 2 && row <= 11 && j >= 2 && j <= 8) {
res += "?";
} else if (theGrid[row][j]->isFilled()) {
res += theGrid[row][j]->getLetter();
} else {
res += ".";
}
}
return res;
}
Block * Board::getCurrentBlock(){
return currBlock;
}
Block * Board::getNextBlock() {
return nextBlock;
}
void Board::copyRow(int firstRow,int secondRow){
for(int j = 0; j < width;++j){
//Copies data of first row into second row
theGrid[secondRow][j]->copyData(theGrid[firstRow][j]);
}
clearRow(firstRow);
}
int Board::updateBlockList(){
int scoredPoints = 0;
for(int i = 0; i < blockList.size();++i ) {
int blockScore = blockList[i]->updateBlock();
if(blockScore > 0){
delete blockList[i];
blockList.erase(blockList.begin()+i);
i -= 1;
}
scoredPoints += blockScore;
}
return scoredPoints;
}
void Board::setLevel(int newLevel){
level = newLevel;
lastClear = 0;
}
void Board::createBlock(Block * newBlock){
if (!nextBlock) { // The very first time, nextBlock is not initialized, so set it
nextBlock = newBlock;
return;
}
delete currBlock;
currBlock = nextBlock;
nextBlock = newBlock;
currBlock->init(theGrid[3][0], theGrid);
}
void Board::replaceBlock(Block * newBlock) {
//Used when we want to replace the current undropped currBlock
delete currBlock;
currBlock = newBlock;
if (currBlock->getType() == "*") { // if level 4 block
currBlock->init(theGrid[3][5], theGrid);
} else {
currBlock->init(theGrid[3][0], theGrid);
}
}
void Board::setBlind() {
blind = true;
}
void Board::setHeavy() {
heavy = true;
}
int Board::getHeavyInt() {
return heavy ? 2 : 0;
}
bool Board::shouldDropStar() {
// drop a block if level 4 and last clear was a multiple of 5
return (level == 4) && (lastClear % 5 == 0);
}