-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
242 lines (200 loc) · 6.35 KB
/
Board.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
#include "Board.hpp"
#include <utility>
#include <iostream>
Board::Board()
{
}
Board::Board(int width, int height)
{
this->width = width;
this->height = height;
matrix = Matrix(height, vector<Block*>(width));
}
Board::~Board()
{
for (auto it = blocks.begin(); it != blocks.end(); ++it)
delete *it;
}
Block* Board::find(string id) const
{
auto it = ids.find(id);
if (it != ids.end())
return it->second;
else
return NULL;
}
Block* Board::push(Block *a, Block *b)
{
blocks.insert(a);
blocks.insert(b);
if (a->getId() == "")
throw string("The base block is not a valid block");
if (a->getId() == b->getId())
throw string("Can't push a block over itself");
if (a->fits(*b)) {
a->push(b);
if (b->getId() != "")
deleteBlock(b);
} else
throw string("Not enough space in " + a->getId());
return a;
}
Block* Board::pop(Block *base, Block *over)
{
if (base->getId() == "")
throw string("The base block is not a valid block");
if (over->getId() == "")
throw string("The over block is not a valid block");
bool deleted = base->pop(over);
if (not deleted)
throw string("The block isn't over");
ids[over->getId()] = NULL;
return base;
}
void Board::equal(string id, Block *block)
{
ids[id] = block;
blocks.insert(block);
block->setId(id);
}
Block* Board::place(int row, int column, int width, int height)
{
Block *block = new Block(width, height);
if (fitsInPosition(*block, row, column)) {
blocks.insert(block);
setBlock(block, row, column);
} else {
delete block;
string msg = "Can't put " + to_string(width) + "x" + to_string(height) +
" at " + to_string(row) + "," + to_string(column);
throw msg;
}
return block;
}
void Board::move(string id, string direction, int units)
{
auto itBlock = ids.find(id);
if (itBlock == ids.end() or itBlock->second == NULL)
throw string("Can't find block " + id);
auto it = positions.find(itBlock->second);
int row = (it->second).first;
int col = (it->second).second;
if (direction == NORTH or direction == WEST)
moveNorthOrWest(row, col, units, direction);
else if (direction == SOUTH or direction == EAST)
moveSouthOrEast(row, col, units, direction);
else
throw string("Direction " + direction + " is invalid");
}
void Board::setBlock(Block *b, int row, int column)
{
positions[b] = make_pair(row, column);
int finalRow = row + b->getHeight();
int finalColumn = column + b->getWidth();
for (int i = row; i < finalRow; ++i) {
for (int j = column; j < finalColumn; ++j)
matrix[i][j] = b;
}
}
void Board::deleteBlock(Block *b)
{
auto it = positions.find(b);
int row, col;
if (it != positions.end()) {
row = (it->second).first;
col = (it->second).second;
positions[b] = make_pair(-1, -1);
} else
throw "Can't find block int deleteBlock()";
for (int i = row; i < row + b->getHeight(); ++i) {
for (int j = col; j < col + b->getWidth(); ++j)
matrix[i][j] = NULL;
}
}
bool Board::fitsInPosition(const Block &block, int row, int column) const
{
bool fits = true;
if (row + block.getHeight() > height or column + block.getWidth() > width)
return false;
int i = row;
while (i < block.getHeight() && fits) {
int j = column;
while (j < block.getWidth() && fits) {
fits = matrix[i][j] == NULL;
++j;
}
++i;
}
return fits;
}
void Board::moveSouthOrEast(int row, int col, int units, string direction)
{
int width = (matrix[row][col])->getWidth();
int height = (matrix[row][col])->getHeight();
int finalRow = row, finalCol = col;
if (direction == SOUTH)
finalRow += units;
else
finalCol += units;
if (finalCol > this->width or finalRow > this->height)
throw "MOVE " + to_string(units) + direction + " is outside the Grid";
for (int i = height - 1; i >= 0; --i) {
for (int j = width - 1; j >= 0; --j) {
Block *newBlock = matrix[finalRow + i][finalCol + j];
if (newBlock != NULL)
throw "There is a block in final position while MOVE " + to_string(units) + " " + direction;
swap(matrix[row + i][col + j], matrix[finalRow + i][finalCol + j]);
}
}
positions[matrix[finalRow][finalCol]] = make_pair(finalRow, finalCol);
}
void Board::moveNorthOrWest(int row, int col, int units, string direction)
{
int width = (matrix[row][col])->getWidth();
int height = (matrix[row][col])->getHeight();
int finalRow = row, finalCol = col;
if (direction == WEST)
finalCol -= units;
else
finalRow -= units;
if (finalCol < 0 or finalRow < 0)
throw "MOVE " + to_string(units) + direction + " is outside the Grid";
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
Block *newBlock = matrix[finalRow + i][finalCol + j];
if (newBlock != NULL)
throw "There is a block in final position while MOVE " + to_string(units) + " " + direction;
swap(matrix[row + i][col + j], matrix[finalRow + i][finalCol + j]);
}
}
positions[matrix[finalRow][finalCol]] = make_pair(finalRow, finalCol);
}
void Board::printHeightMatrix() const {
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < matrix[0].size(); ++j) {
if (matrix[i][j] == NULL)
cout << 0 << " ";
else
cout << matrix[i][j]->totalHeight() << " ";
}
cout << endl;
}
}
ostream& operator<<(ostream& os, const Board &board)
{
os << "Board " + to_string(board.width) + "x" + to_string(board.height) << endl;
for (int i = 0; i < board.matrix.size(); ++i) {
for (int j = 0; j < board.matrix[0].size(); ++j) {
if (board.matrix[i][j] != NULL) {
if ((board.matrix[i][j])->getId() != "")
os << (board.matrix[i][j])->getId();
else
os << (board.matrix[i][j])->getHeight() << " + " << (board.matrix[i][j])->getWidth();
} else
os << "-";
os << "\t";
}
os << endl;
}
return os;
}