-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cc
210 lines (189 loc) · 4.95 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
#include "board.h"
#include "level0Factory.h"
#include <cmath>
#include <iostream>
#include <map>
#include <memory>
using namespace std;
string Board::toString(bool includeCurrentBlock, bool ghost, bool blind)
{
char board[ROWS * COLS + 1];
for (int i = 0; i < ROWS * COLS; i++) {
board[i] = ' ';
}
if (ghost) {
// Make a copy of the current block and store it in a unique_ptr
unique_ptr<Block> ghostBlock = currentBlock->clone();
blocks.push_back(unique_ptr<Block>(ghostBlock.get())); // Copy the ghost block into the blocks vector
// Move the ghost block down until it hits something
while (validBoard(false)) {
ghostBlock->down();
}
ghostBlock->up();
// Draw the ghost block in lowercase
for (Position pos : ghostBlock->getPositions()) {
board[pos.y * COLS + pos.x] = tolower(ghostBlock->c);
}
blocks.pop_back();
ghostBlock.release(); // Release the ghost block from the unique_ptr since it already got freed when it was popped
}
for (auto &block : blocks) {
for (Position pos : block->getPositions()) {
board[pos.y * COLS + pos.x] = block->c;
}
}
if (includeCurrentBlock) {
for (Position pos : currentBlock->getPositions()) {
board[pos.y * COLS + pos.x] = currentBlock->c;
}
}
if(blind){
for(int rows = 3; rows < 12; rows++){
for(int cols = 3; cols < 9; cols++){
board[rows * COLS + cols] = '?';
}
}
}
board[ROWS * COLS] = '\0';
string s = board;
return s;
}
bool Board::validBoard(bool includeCurrentBlock)
{
// Check for overlap
vector<Position> positions;
if (includeCurrentBlock) {
for (Position pos : currentBlock->getPositions()) {
if (pos.x < 0 || pos.x >= Board::COLS || pos.y < 0 || pos.y >= Board::ROWS) {
return false;
}
positions.push_back(pos);
}
}
for (auto &block : blocks) {
for (Position pos : block->getPositions()) {
if (pos.x < 0 || pos.x >= Board::COLS || pos.y < 0 || pos.y >= Board::ROWS) {
return false;
}
for (Position p : positions) {
if (p == pos) {
return false;
}
}
positions.push_back(pos);
}
}
return true;
}
void Board::left(bool heavyEffect) {
currentBlock->left();
if (level == 3 || level == 4) {
currentBlock->down();
}
if(heavyEffect) {
currentBlock->down();
}
if (!validBoard()) {
if (level == 4 || level == 3) {
currentBlock->up();
}
if(heavyEffect){
currentBlock->up();
}
currentBlock->right();
}
}
void Board::right(bool heavyEffect) {
currentBlock->right();
if (level == 3 || level == 4) {
currentBlock->down();
}
if(heavyEffect) {
currentBlock->down();
}
if (!validBoard()) {
if (level == 4 || level == 3) {
currentBlock->up();
}
if(heavyEffect){
currentBlock->up();
}
currentBlock->left();
}
}
void Board::down(bool heavyEffect) {
currentBlock->down();
if (level == 4 || level == 3) {
currentBlock->down();
}
if (!validBoard()) {
if (level){
currentBlock->up();
}
currentBlock->up();
}
}
void Board::clockwise()
{
currentBlock->clockwise();
if (level == 4 || level == 3) {
currentBlock->down();
}
if (!validBoard()) {
if (level == 4 || level == 3) {
currentBlock->up();
}
currentBlock->counterClockwise();
}
}
void Board::counterClockwise()
{
currentBlock->counterClockwise();
if (level == 4 || level == 3) {
currentBlock->down();
}
if (!validBoard()) {
if (level == 4 || level == 3) {
currentBlock->up();
}
currentBlock->clockwise();
}
}
void Board::drop()
{
while (validBoard()) {
currentBlock->down();
}
currentBlock->up();
blocks.push_back(std::move(currentBlock));
currentBlock = std::move(nextBlock);
nextBlock.release();
}
// function to drop Star block
void Board::dropStar()
{
unique_ptr<Block> sb = make_unique<StarBlock>();
blocks.push_back(unique_ptr<Block>(sb.get()));
while (validBoard(false)) {
sb->down();
}
sb->up();
sb.release();
}
int Board::gc()
{
int scoreAddition = 0;
for (auto it = blocks.begin(); it != blocks.end();) {
if ((*it)->getOffsets().size() == 0) {
scoreAddition += pow<int, int>(((*it)->startingLevel + 1), 2);
it = blocks.erase(it);
} else {
++it;
}
}
if (scoreAddition > 0)
return scoreAddition;
else
return 0;
}
Board::Board() : turn_count(0), level(0) {}