This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoard.cpp
429 lines (374 loc) · 10.2 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
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
#include "Board.h"
Board::Board() {
m_numberOfTileInThisTurn = INITIAL_STATE;
m_orientationOfTileInThisTurn = INITIAL_STATE;
m_qwirkle = INITIAL_STATE;
}
bool Board::addTileAt(Coordinate &coordinate) {
bool addSuccess = false;
//If adding the first tile -> No condition
if (m_coordinate.size() == 0) {
m_numberOfTileInThisTurn++;
m_coordinate.push_back(coordinate);
addSuccess = true;
} else {
coordinate.relocateX(getWestBound());
coordinate.relocateY(getNorthBound());
//check if the coordinate is empty and have at least 1 surrounding tile
if (isAddable(coordinate)) {
m_numberOfTileInThisTurn++;
m_coordinate.push_back(coordinate);
if (m_numberOfTileInThisTurn == TWO_TILES_ADD) {
m_orientationOfTileInThisTurn = getAddOrientation();
}
if (isValidForOneCoordinateCondition()
&& isValsidForMultiplePlaceCondition()) {
addSuccess = true;
} else {
m_numberOfTileInThisTurn--;
if (m_numberOfTileInThisTurn == ONE_TILE_ADD) {
m_orientationOfTileInThisTurn = INITIAL_STATE;
}
m_coordinate.pop_back();
addSuccess = false;
}
}
}
return addSuccess;
}
Tile Board::getTileAt(Coordinate coordinate) {
Tile t = Tile();
for (unsigned int i = 0; i < m_coordinate.size(); ++i) {
if (m_coordinate[i] == coordinate)
t = m_coordinate[i].getTile();
}
return t;
}
int Board::getNorthBound() {
int length = MIN_NORTH;
for (Coordinate c : m_coordinate) {
if (c.getY() <= length) {
length = c.getY() - 1;
}
}
return length;
}
int Board::getSouthBound() {
int length = MIN_SOUTH;
for (Coordinate c : m_coordinate) {
if (c.getY() >= length) {
length = c.getY() + 1;
}
}
return length;
}
int Board::getWestBound() {
int length = MIN_WEST;
for (Coordinate c : m_coordinate) {
if (c.getX() <= length) {
length = c.getX() - 1;
}
}
return length;
}
int Board::getEastBound() {
int length = MIN_EAST;
for (Coordinate c : m_coordinate) {
if (c.getX() >= length) {
length = c.getX() + 1;
}
}
return length;
}
std::ostream &operator<<(std::ostream &out, Board &b) {
//Column index
out << " ";
int colIndex = 0;
for (int i = b.getWestBound(); i < b.getEastBound() + 1; ++i) {
std::string index = std::to_string(colIndex);
if (colIndex < 10) {
out << " " << index << " ";
} else {
out << " " << index.substr(0, 1) << index.substr(1, 1);
}
colIndex++;
}
//Dash seperator ---
out << "\n" << " -";
for (int i = b.getWestBound(); i < b.getEastBound() + 1; ++i) {
out << "---";
}
out << "\n";
char c = 'A';
for (int i = b.getNorthBound(); i < b.getSouthBound() + 1; ++i) {
std::string s = "";
s.push_back(c);
out << c << " |";
for (int j = b.getWestBound(); j < b.getEastBound() + 1; ++j) {
bool has = false;
for (Coordinate c : b.m_coordinate) {
if (c.getX() == j && c.getY() == i) {
out << c.getTile() << "|";
has = true;
}
}
if (!has) {
out << " |";
}
}
out << "\n";
c++;
}
return out;
}
bool Board::hasTileAt(Coordinate coordinate) {
bool has = false;
for (Coordinate c : m_coordinate) {
if (c == coordinate) {
has = true;
}
}
return has;
}
bool Board::hasNorth(Coordinate coordinate) {
coordinate.relocateY(-1);
return hasTileAt(coordinate);
}
bool Board::hasSouth(Coordinate coordinate) {
coordinate.relocateY(1);
return hasTileAt(coordinate);
}
bool Board::hasWest(Coordinate coordinate) {
coordinate.relocateX(-1);
return hasTileAt(coordinate);
}
bool Board::hasEast(Coordinate coordinate) {
coordinate.relocateX(1);
return hasTileAt(coordinate);
}
bool Board::isAddable(Coordinate coordinate) {
return !hasTileAt(coordinate)
&& (hasNorth(coordinate) || hasSouth(coordinate) || hasWest(coordinate)
|| hasEast(coordinate));
}
int Board::numOfNorthTile(Coordinate coordinate) {
int num = 0;
while (hasNorth(coordinate)) {
coordinate.relocateY(-1);
num++;
}
return num;
}
int Board::numOfSouthTile(Coordinate coordinate) {
int num = 0;
while (hasSouth(coordinate)) {
coordinate.relocateY(1);
num++;
}
return num;
}
int Board::numOfWestTile(Coordinate coordinate) {
int num = 0;
while (hasWest(coordinate)) {
coordinate.relocateX(-1);
num++;
}
return num;
}
int Board::numOfEastTile(Coordinate coordinate) {
int num = 0;
while (hasEast(coordinate)) {
coordinate.relocateX(1);
num++;
}
return num;
}
int Board::numOfColTile(Coordinate coordinate) {
return numOfNorthTile(coordinate) + numOfSouthTile(coordinate) + 1;
}
int Board::numOfRowTile(Coordinate coordinate) {
return numOfWestTile(coordinate) + numOfEastTile(coordinate) + 1;
}
int Board::rowPoint(Coordinate coordinate) {
int rowPoint =
(hasWest(coordinate) || hasEast(coordinate)) ?
numOfRowTile(coordinate) : 0;
if (rowPoint == 6) {
rowPoint += 6;
m_qwirkle = true;
}
return rowPoint;
}
int Board::colPoint(Coordinate coordinate) {
int colPoint =
(hasNorth(coordinate) || hasSouth(coordinate)) ?
numOfColTile(coordinate) : 0;
if (colPoint == 6) {
colPoint += 6;
m_qwirkle = true;
}
return colPoint;
}
int Board::getAddOrientation() {
int orientation = INVALID_ADD;
if (m_coordinate.end()[-1].getX() == m_coordinate.end()[-2].getX()) {
orientation = VERTICAL_ADD;
} else if (m_coordinate.end()[-1].getY() == m_coordinate.end()[-2].getY()) {
orientation = HORIZONTAL_ADD;
}
return orientation;
}
int Board::pointWhenPlayerPlaceOneTile() {
Coordinate coordinate = m_coordinate.back();
int cPoint = colPoint(coordinate);
int rPoint = rowPoint(coordinate);
int sum = cPoint + rPoint;
return (sum == 0) ? 1 : sum;
}
int Board::pointWhenPlayerPlaceHorizontalTiles() {
Coordinate coordinate = m_coordinate.back();
std::vector<Coordinate> newTileThisTurn(
m_coordinate.end() - m_numberOfTileInThisTurn, m_coordinate.end());
int rPoint = rowPoint(coordinate);
int cPoint = 0;
for (Coordinate c : newTileThisTurn) {
cPoint += colPoint(c);
}
return cPoint + rPoint;
}
int Board::pointWhenPlayerPlaceVerticalTiles() {
Coordinate coordinate = m_coordinate.back();
std::vector<Coordinate> newTileThisTurn(
m_coordinate.end() - m_numberOfTileInThisTurn, m_coordinate.end());
int cPoint = colPoint(coordinate);
int rPoint = 0;
for (Coordinate c : newTileThisTurn) {
rPoint += rowPoint(c);
}
return cPoint + rPoint;
}
int Board::totalPoint() {
int point = 0;
if (m_numberOfTileInThisTurn == ONE_TILE_ADD) {
point = pointWhenPlayerPlaceOneTile();
} else if (m_numberOfTileInThisTurn >= TWO_TILES_ADD) {
if (m_orientationOfTileInThisTurn == VERTICAL_ADD) {
point = pointWhenPlayerPlaceVerticalTiles();
} else if (m_orientationOfTileInThisTurn == HORIZONTAL_ADD) {
point = pointWhenPlayerPlaceHorizontalTiles();
}
}
return point;
}
void Board::resetValidation() {
m_numberOfTileInThisTurn = INITIAL_STATE;
m_orientationOfTileInThisTurn = INITIAL_STATE;
m_qwirkle = INITIAL_STATE;
}
bool Board::isQwirkle() {
return m_qwirkle;
}
std::vector<Tile> Board::colLine() {
std::vector<Tile> line;
line.reserve(6);
Coordinate c1 = m_coordinate.back();
while (numOfSouthTile(c1) > 0) {
c1.relocateY(1);
line.push_back(getTileAt(c1));
}
Coordinate c2 = m_coordinate.back();
while (numOfNorthTile(c2) > 0) {
c2.relocateY(-1);
line.push_back(getTileAt(c2));
}
line.push_back(m_coordinate.back().getTile());
return line;
}
std::vector<Tile> Board::rowLine() {
std::vector<Tile> line;
line.reserve(6);
Coordinate c1 = m_coordinate.back();
while (numOfWestTile(c1) > 0) {
c1.relocateX(-1);
line.push_back(getTileAt(c1));
}
Coordinate c2 = m_coordinate.back();
while (numOfEastTile(c2) > 0) {
c2.relocateX(1);
line.push_back(getTileAt(c2));
}
line.push_back(m_coordinate.back().getTile());
return line;
}
bool Board::verifyByLine(std::vector<Tile> line) {
bool sameColour = true;
bool sameShape = true;
bool identical = false;
//brute force approach
for (unsigned int i = 0; i < line.size(); ++i) {
for (unsigned int j = i + 1; j < line.size(); ++j) {
if (line[i] == line[j]) {
identical = true;
}
if (!line[i].isSameColour(line[j])) {
sameColour = false;
}
if (!line[i].isSameShape(line[j])) {
sameShape = false;
}
}
}
return !identical && (sameShape || sameColour);
}
bool Board::isValidForOneCoordinateCondition() {
return verifyByLine(rowLine()) && verifyByLine(colLine());
}
bool Board::validateHorizontalAdd() {
bool isValid = false;
if (((m_coordinate.end()[-1].getX() + 1 == m_coordinate.end()[-2].getX())
|| (m_coordinate.end()[-1].getX() - 1 == m_coordinate.end()[-2].getX()))
&& (m_coordinate.end()[-1].getY() == m_coordinate.end()[-2].getY())) {
isValid = true;
}
return isValid;
}
bool Board::validateVerticalAdd() {
bool isValid = false;
if (((m_coordinate.end()[-1].getY() + 1 == m_coordinate.end()[-2].getY())
|| (m_coordinate.end()[-1].getY() - 1 == m_coordinate.end()[-2].getY()))
&& (m_coordinate.end()[-1].getX() == m_coordinate.end()[-2].getX())) {
isValid = true;
}
return isValid;
}
bool Board::validateSecondTileCondition() {
bool isValid = false;
if (validateHorizontalAdd() || validateVerticalAdd()) {
isValid = true;
}
return isValid;
}
bool Board::validateMoreThanTwoTileCondition() {
bool isValid = false;
if (m_orientationOfTileInThisTurn == HORIZONTAL_ADD
&& validateHorizontalAdd()) {
isValid = true;
} else if (m_orientationOfTileInThisTurn == VERTICAL_ADD
&& validateVerticalAdd()) {
isValid = true;
}
return isValid;
}
bool Board::isValsidForMultiplePlaceCondition() {
bool isValid = false;
if (m_numberOfTileInThisTurn == TWO_TILES_ADD
&& validateSecondTileCondition()) {
isValid = true;
} else if (m_numberOfTileInThisTurn > TWO_TILES_ADD
&& validateMoreThanTwoTileCondition()) {
isValid = true;
} else if (m_numberOfTileInThisTurn == ONE_TILE_ADD) {
isValid = true;
}
return isValid;
}