-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
338 lines (318 loc) · 10.6 KB
/
Player.cpp
File metadata and controls
338 lines (318 loc) · 10.6 KB
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
#include "Player.h"
#include "Azul.h"
#include "Utils.h"
#include <cctype>
#include <iostream>
// helper struct and functions for pickTiles
struct TileSet {
char color;
int count;
};
int chooseFactory(const Azul&);
TileSet takeTilesFromCenter(Azul&);
TileSet takeTilesFromFactory(Azul&, char* factory);
void placeTakenTiles(Player&, Azul&, TileSet);
// helper functions for placeTilesToWall
int calcPointsForWallTile(const char[5][5], int row, int col);
bool isColorInWallColumn(char color, const char[5][5], int col);
void pickTiles(Player& p, Azul& game)
{
TileSet tiles;
for (;;) {
displayBoard(p);
std::cout << "Player " << p.id
<< ", take tiles from a factory or the center pile:\n";
displayFactories(game);
int factoryId = chooseFactory(game);
if (factoryId == 0) {
tiles = takeTilesFromCenter(game);
if (game.startPlayerMarker && tiles.count > 0) {
// take startPlayerMarker too
game.startingPlayer = p.id - 1;
game.startPlayerMarker = false;
dropToFloor(p, game, 'X');
}
} else {
tiles = takeTilesFromFactory(game, game.factories[factoryId - 1]);
}
if (tiles.count == 0) {
std::cout << "Error: There are no tiles of that color there.\n"
"Press Enter / Return and choose again.\n";
cinIgnore();
} else
break;
}
placeTakenTiles(p, game, tiles);
std::cout << "Player board after placement:\n";
displayBoard(p);
std::cout << "Press Enter / Return to finish your turn.";
cinIgnore();
}
int chooseFactory(const Azul& game)
{
for (;;) {
std::cout << "Enter the number of the factory, "
"or 0 to choose from the center:\n";
int factoryId = readBoundedInt(0, getFactoryCount(game));
if (factoryId > 0 && game.factories[factoryId - 1][0] == ' ')
std::cout << "Error: Factory is empty.\n";
else if (factoryId == 0 && isCenterPileEmpty(game))
std::cout << "Error: The center pile has no colored tiles.\n";
else
return factoryId;
}
}
TileSet takeTilesFromCenter(Azul& game)
{
TileSet tiles;
std::cout << "Choose a color (A-E):\n";
tiles.color = toupper(readBoundedChar('A', 'E'));
tiles.count = game.centerPile[tiles.color - 'A'];
game.centerPile[tiles.color - 'A'] = 0;
return tiles;
}
TileSet takeTilesFromFactory(Azul& game, char* factory)
{
TileSet tiles;
std::cout << "Choose a color (A-E):\n";
tiles.color = toupper(readBoundedChar('A', 'E'));
tiles.count = 0;
if (!containsChar(factory, 4, tiles.color)) {
return tiles;
}
for (int i = 0; i < 4; ++i) {
if (factory[i] == tiles.color)
++tiles.count;
else if (factory[i] != ' ') { // == ' ' if tiles almost ran out
++game.centerPile[factory[i] - 'A'];
}
factory[i] = ' ';
}
return tiles;
}
void placeTakenTiles(Player& p, Azul& game, TileSet tiles)
{
for (;;) {
displayBoard(p);
std::cout << "You took " << tiles.count << ' ' << tiles.color
<< (tiles.count > 1 ? " tiles.\n" : " tile.\n")
<< "Choose a line (1-5) or the floor (0) to place them:\n";
int lineId = readBoundedInt(0, 5);
if (lineId == 0) {
dropToFloor(p, game, tiles.color, tiles.count);
return;
}
char* line = p.lines[lineId - 1];
if (line[0] != tiles.color && line[0] != '-') {
std::cout << "Error: This line contains a different color.\n"
"Press Enter / Return and choose another line.";
cinIgnore();
continue;
}
if (containsChar(p.wall[lineId - 1], 5, tiles.color)) {
std::cout << "Error: The wall row already contains that color.\n"
"Press Enter / Return and choose another line.";
cinIgnore();
continue;
}
if (line[lineId - 1] != '-') {
std::cout << "Error: This line is already full.\n"
"Press Enter / Return and choose another line.";
cinIgnore();
continue;
}
// place tiles to the line
for (int i = 0; i < lineId && tiles.count > 0; ++i) {
if (line[i] == '-') {
line[i] = tiles.color;
--tiles.count;
}
}
dropToFloor(p, game, tiles.color, tiles.count); // drop remaining tiles
return;
}
}
void placeTilesToWall(Player& p, Azul& game)
{
for (int row = 0; row < 5; ++row) {
if (containsChar(p.lines[row], row + 1, '-')) // line is incomplete
continue;
displayBoard(p);
std::cout << "Player " << p.id << ", line " << row + 1
<< " is complete.\n";
const char color = p.lines[row][0];
bool canBePlaced = false;
for (int col = 0; col < 5 && !canBePlaced; ++col) {
canBePlaced = p.wall[row][col] == '.'
&& !isColorInWallColumn(color, p.wall, col);
}
if (!canBePlaced) {
std::cout << "Row " << row + 1
<< " has no valid places for this color.\n"
"The line must be placed on the floor, "
"press Enter / Return to continue.";
cinIgnore();
dropToFloor(p, game, color, row + 1);
for (int i = 0; i < row + 1; ++i) {
p.lines[row][i] = '-';
}
continue;
}
for (;;) {
std::cout << "Choose a column (1-5) to place the first tile ("
<< color << ") of this line there:\n";
int col = readBoundedInt(1, 5) - 1;
if (p.wall[row][col] != '.') {
std::cout
<< "Error: That place is already occupied in that row.\n";
continue;
}
if (isColorInWallColumn(color, p.wall, col)) {
std::cout
<< "Error: That column already contains that color.\n";
continue;
}
p.wall[row][col] = color;
game.discarded[color - 'A'] += row;
for (int i = 0; i < row + 1; ++i) {
p.lines[row][i] = '-';
}
int points = calcPointsForWallTile(p.wall, row, col);
p.points += points;
std::cout << "Player board after placement:\n";
displayBoard(p);
std::cout << "You got " << points << " point"
<< (points > 1 ? "s" : "") << " for the tile.\n"
<< "Press Enter / Return to continue.";
cinIgnore();
break;
}
}
}
int calcPointsForWallTile(const char wall[5][5], int row, int col)
{
int horizontalLength = 1, verticalLength = 1;
for (int i = col - 1; i >= 0 && wall[row][i] != '.'; --i)
++horizontalLength;
for (int i = col + 1; i < 5 && wall[row][i] != '.'; ++i)
++horizontalLength;
for (int i = row - 1; i >= 0 && wall[i][col] != '.'; --i)
++verticalLength;
for (int i = row + 1; i < 5 && wall[i][col] != '.'; ++i)
++verticalLength;
int scored = 0;
if (horizontalLength > 1)
scored += horizontalLength;
if (verticalLength > 1)
scored += verticalLength;
if (scored == 0)
return 1;
return scored;
}
bool isColorInWallColumn(char color, const char wall[5][5], int col)
{
for (int row = 0; row < 5; ++row) {
if (wall[row][col] == color)
return true;
}
return false;
}
void scoreFloor(Player& p)
{
int penalties = 0;
for (int i = 0; i < 7 && p.floor[i] != '_'; ++i) {
/* switch (i) {
case 0:
case 1:
penalties += 1;
break;
case 5:
case 6:
penalties += 3;
break;
default:
penalties += 2;
break;
}*/
penalties += (i + 1) / 3 + 1;
}
p.points -= penalties;
if (p.points < 0)
p.points = 0;
std::cout << "Player " << p.id
<< ", after subtracting the points for the floor line (-"
<< penalties << "), you have " << p.points << " point"
<< (p.points == 1 ? ".\n" : "s.\n")
<< "Press Enter / Return to end your turn.";
cinIgnore();
}
void displayBoard(const Player& p)
{
std::cout << "====== Player " << p.id << " ====== "
<< "Points: " << p.points << "\n"
<< " 1 2 3 4 5\n";
for (int row = 0; row < 5; ++row) {
for (int i = 4; i >= 0; --i) {
std::cout << ' ' << (i <= row ? p.lines[row][i] : ' ');
}
std::cout << ' ' << row + 1;
for (int col = 0; col < 5; ++col) {
std::cout << ' ' << p.wall[row][col];
}
std::cout << '\n';
}
for (int i = 0; i < 7; ++i) {
std::cout << " " << p.floor[i];
}
std::cout << '\n';
std::cout << " -1 -1 -2 -2 -2 -3 -3\n\n";
}
void dropToFloor(Player& p, Azul& game, char tile, int count)
{
for (int i = 0; i < 7 && count > 0; ++i) {
if (p.floor[i] == '_') {
p.floor[i] = tile;
--count;
}
}
// discard remaining
if ('A' <= tile && tile <= 'E')
game.discarded[tile - 'A'] += count;
}
int countFullRows(const Player& p)
{
int rows = 0;
for (int i = 0; i < 5; ++i) {
if (!containsChar(p.wall[i], 5, '.'))
++rows;
}
return rows;
}
void scoreBonusPoints(Player& p)
{
int rows = countFullRows(p);
int columns = 0, colors = 0, tilesOfColor[5] = { 0 };
for (int i = 0; i < 5; ++i) {
if (!isColorInWallColumn('.', p.wall, i))
++columns;
for (int j = 0; j < 5; ++j) {
const char color = p.wall[i][j];
if (color != '.') {
++tilesOfColor[color - 'A'];
if (tilesOfColor[color - 'A'] == 5)
++colors;
}
}
}
int bonus = 2 * rows + 7 * columns + 10 * colors;
p.points += bonus;
displayBoard(p);
std::cout << "Player " << p.id << " completed:\n"
<< rows << " row" << (rows == 1 ? ",\n" : "s,\n")
<< columns << " column" << (columns == 1 ? ",\n" : "s,\n")
<< colors << " color" << (colors == 1 ? ",\n" : "s,\n")
<< "scoring " << bonus << " total bonus points.\n"
<< "Final score: " << p.points << ".\n"
<< "Press Enter / Return to continue.";
cinIgnore();
}