-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMosaic.cpp
339 lines (315 loc) · 8.62 KB
/
Mosaic.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
// Copyright 2020 Chou, Dechathaweewat, Hollis-London
#include "Mosaic.h"
#include <sstream>
#include <iostream>
Mosaic::Mosaic()
{
this->wall = Mosaic::InitWall();
this->pattern_lines = Mosaic::InitPatternLines();
this->floor_line = std::make_shared<TileLinkedList>();
this->overflow = std::make_shared<TileLinkedList>();
};
Mosaic::~Mosaic() {}
Mosaic::Mosaic(Wall &&wall, PatternLines &&pattern_lines,
FloorLinePtr &&floor_line)
: wall(std::move(wall)),
pattern_lines(std::move(pattern_lines)),
floor_line(std::move(floor_line))
{
this->overflow = std::make_shared<TileLinkedList>();
};
Mosaic::Mosaic(Mosaic &&other)
: wall(std::move(other.wall)),
pattern_lines(std::move(other.pattern_lines)),
floor_line(std::move(other.floor_line)),
overflow(std::move(other.overflow)){};
Wall Mosaic::InitWall()
{
Wall wall = Wall();
for (size_t i = 0; i < MOSAIC_DIM; i++)
{
wall.at(i) = std::array<TilePtr, MOSAIC_DIM>();
for (size_t j = 0; j < MOSAIC_DIM; j++)
{
wall.at(i).at(j) = nullptr;
}
}
return wall;
}
PatternLines Mosaic::InitPatternLines()
{
PatternLines pattern_lines = PatternLines();
for (size_t i = 0; i < MOSAIC_DIM; i++)
{
pattern_lines.at(i) = std::make_shared<TileLinkedList>();
}
return pattern_lines;
}
bool Mosaic::RowFull()
{
bool any_row_full = false;
for (size_t i = 0; i < MOSAIC_DIM && !any_row_full; i++)
{
bool row_full = true;
for (size_t j = 0; j < MOSAIC_DIM; j++)
{
if (this->wall.at(i).at(j) == nullptr)
row_full = false;
}
if (row_full)
any_row_full = true;
}
return any_row_full;
}
int colour_index(Colour colour, int i)
{
// get index of tile colour on wall row using python style modulo
int n = (int)Colour::B + i - colour;
int M = TILE_SZ - 1;
// python style modulo
return ((n % M) + M) % M;
}
int Mosaic::PatternScore()
{
int score = 0;
for (int i = 0; i < MOSAIC_DIM; i++)
{
TileLinkedListPtr pattern_line = this->pattern_lines.at(i);
if ((int)pattern_line->Size() == i + 1)
{
// pattern line is full
// plus 1 to score
score += 1;
Colour colour = pattern_line->GetColour();
int index = colour_index(colour, i);
// find horizontally linked adjacent tiles
std::array<TilePtr, MOSAIC_DIM> &wall_row = this->wall.at(i);
// forward
int a = 0;
for (a = index + 1; a < MOSAIC_DIM && wall_row.at(a) != nullptr; a++)
{
score += 1;
}
// backward
for (a = index - 1; a >= 0 && wall_row.at(a) != nullptr; a--)
{
score += 1;
}
// find vertically linked adjacent tiles
// upwards
for (a = i - 1; a >= 0 && this->wall.at(a).at(index) != nullptr; a--)
{
score += 1;
}
// downwards
for (a = i + 1; a < MOSAIC_DIM && this->wall.at(a).at(index) != nullptr;
a++)
{
score += 1;
}
// move tile to wall
TilePtr tile = pattern_line->RemoveBack();
wall_row.at(index) = std::move(tile);
// empty pattern_line into overflow
while (pattern_line->Size() > 0)
this->overflow->AddTile(pattern_line->RemoveFront());
}
}
return score;
}
int Mosaic::EndGameWallScore()
{
int score = 0;
// full rows
// full columns
// completed colours
std::array<int, MOSAIC_DIM> colour_tally = std::array<int, MOSAIC_DIM>();
for (size_t i = 0; i < MOSAIC_DIM; i++)
{
bool row_full = true;
bool col_full = true;
for (size_t j = 0; j < MOSAIC_DIM; j++)
{
if (this->wall.at(i).at(j) == nullptr)
row_full = false;
if (this->wall.at(j).at(i) == nullptr)
col_full = false;
int colour = (int)this->wall.at(i).at(j)->GetColour();
colour_tally.at(colour)++;
if (colour_tally.at(colour) == MOSAIC_DIM)
score += 10;
}
if (row_full)
score += ROW_FULL_BONUS;
if (col_full)
score += COL_FULL_BONUS;
}
return score;
}
void Mosaic::TryMove(Colour colour, unsigned int pattern_line,
TileLinkedListPtr tile_linked_list)
{
unsigned int line_size_limit = 0;
if (pattern_line != FLOOR_INDEX)
{
// f tile must go in floor line
if (colour == Colour::F)
throw std::runtime_error("F tile must go in floor line");
// pattern line must not be full
TileLinkedListPtr pattern_line_ptr = this->pattern_lines.at(pattern_line);
if (pattern_line_ptr->Size() >= pattern_line + 1)
throw std::runtime_error("pattern line is full");
// must be empty or the colour of the colour
// parameter
if (pattern_line_ptr->Size() > 0 && pattern_line_ptr->GetColour() != colour)
throw std::runtime_error("pattern line is different colour");
// colour must not be present in adjacent wall row
for (size_t i = 0; i < MOSAIC_DIM; i++)
{
TilePtr &cell = this->wall.at(pattern_line).at(i);
if (cell != nullptr && cell->GetColour() == colour)
throw std::runtime_error("wall already contains colour");
}
// set amount of tiles that can go into the pattern line
line_size_limit = pattern_line + 1 - pattern_line_ptr->Size();
}
// move is valid
// add tiles to selected pattern line until pattern line size limit is
// reached (will skip if line_size_limit is 0)
while (tile_linked_list->Size() > 0 && line_size_limit > 0)
{
this->pattern_lines.at(pattern_line)
->AddTile(tile_linked_list->RemoveFront());
line_size_limit--;
}
// add remaining tiles to floor line (will add all tiles to floor line if
// line size limit is 0) until floor line is full
while (tile_linked_list->Size() > 0 && this->floor_line->Size() < FLOOR_SZ)
{
this->floor_line->AddTile(tile_linked_list->RemoveFront());
}
// add remaining remaining to hidden TLL overflow to be emptied by Game
while (tile_linked_list->Size() > 0)
{
this->overflow->AddTile(tile_linked_list->RemoveFront());
}
}
int Mosaic::FloorPenalty()
{
this->first_turn = false;
int count = 0;
if (floor_line->Size() != 0)
{
for (unsigned int i = 0; i < floor_line->Size(); i++)
{
count += 1;
if (i > 1)
{
count += 1;
}
if (i > 4)
{
count += 1;
}
}
while (floor_line->Size() > 0)
{
TilePtr tile = floor_line->RemoveFront();
if (tile->GetColour() == Colour::F)
{
this->first_turn = true;
}
else
{
this->overflow->AddTile(std::move(tile));
}
// first player tile is dropped here
}
}
return count;
}
bool Mosaic::IsFirstPlayer() { return this->first_turn; }
FloorLinePtr Mosaic::GetFloorLine() { return this->floor_line; }
TileLinkedListPtr Mosaic::GetOverflow() { return this->overflow; }
string Mosaic::PatternLinesToString()
{
std::stringstream ss;
for (size_t i = 0; i < MOSAIC_DIM; i++)
{
std::string pl = this->pattern_lines.at(i)->ToSaveString();
while (pl.size() < i + 1)
pl = "." + pl;
ss << pl << std::endl;
}
return ss.str();
}
std::string Mosaic::WallToString()
{
std::stringstream ss;
// wall
for (size_t i = 0; i < MOSAIC_DIM; i++)
{
for (size_t j = 0; j < MOSAIC_DIM; j++)
{
if (this->wall.at(i).at(j) == nullptr)
{
ss << ".";
}
else
{
ss << this->wall.at(i).at(j)->ToString();
}
}
ss << std::endl;
}
return ss.str();
}
std::string Mosaic::FloorLineToString()
{
std::string fl = this->floor_line->ToSaveString();
while (fl.size() < FLOOR_SZ)
fl.append(".");
return fl + "\n";
}
const char *Mosaic::mos_wall[MOSAIC_DIM][MOSAIC_DIM] = {
{" B", " Y", " R", " U", " L"},
{" L", " B", " Y", " R", " U"},
{" U", " L", " B", " Y", " R"},
{" R", " U", " L", " B", " Y"},
{" Y", " R", " U", " L", " B"},
};
std::string Mosaic::ToString()
{
std::stringstream ss;
for (size_t i = 0; i < MOSAIC_DIM; i++)
{
ss << i + 1 << ": ";
// # Pattern Line #
// spacing offset
unsigned int offset = MOSAIC_DIM - (i + 1);
for (size_t j = 0; j < offset; j++)
ss << " ";
// empty tiles
unsigned int empty_count = (i + 1) - this->pattern_lines.at(i)->Size();
for (size_t j = 0; j < empty_count; j++)
ss << ". ";
ss << this->pattern_lines.at(i)->backgroundToString();
ss << "||";
// wall
for (size_t j = 0; j < MOSAIC_DIM; j++)
{
if (this->wall.at(i).at(j) == nullptr)
{
ss << mos_wall[i][j];
}
else
{
ss << " " << this->wall.at(i).at(j)->colouredBGToString();
}
}
ss << std::endl;
}
// floor line (broken)
ss <<"\u001b[31mbroken: \u001b[0m" << this->floor_line->ToString();
return ss.str();
}