-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameEngine.cpp
More file actions
executable file
·518 lines (463 loc) · 15.7 KB
/
GameEngine.cpp
File metadata and controls
executable file
·518 lines (463 loc) · 15.7 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "GameEngine.h"
#include "Saver.h"
#include <iostream>
#include <algorithm>
#include <random>
GameEngine::GameEngine(Menu *menu) : players(),
factories(),
centerPile(),
bag(new TileList()),
lid(new TileList()),
menu(menu)
{
//Create factories
for (int i = 0; i < NUM_FACTORIES; ++i)
{
factories[i] = new Factory();
}
}
GameEngine::~GameEngine()
{
for (int i = 0; i < NUM_FACTORIES; ++i)
{
if (factories[i] != nullptr)
delete factories[i];
}
if (bag != nullptr)
delete bag;
if (lid != nullptr)
delete lid;
for (Player *player : players)
if (player != nullptr)
delete player;
}
bool GameEngine::playGame(char const *argv)
{
addPlayers();
fillBag(argv[0]);
playerTurnID = players[0];
return playGame();
}
bool GameEngine::playGame()
{
//Bool to track exit condition
bool exit = false;
while (!exit && !hasPlayerWon())
{
exit = playRound();
}
//Check if a player has won after each round
if (hasPlayerWon())
{
//Game is a draw
if (players[0]->getScore() == players[1]->getScore())
{
//Print results
menu->gameOver(players[0]->getName(), players[1]->getName(), players[0]->getScore());
}
//Set pointer to winning player
else
{
Player *winningPlayer = playerTurnID;
for (auto player : players)
{
if (player->getScore() > winningPlayer->getScore())
{
winningPlayer = player;
}
}
//Print results
menu->gameOver(winningPlayer);
}
}
return exit;
}
bool GameEngine::playRound()
{
//Track whether all tiles have been drawn
if (roundOver())
{
//Return FP tile to center
centerPile.push_back(FIRSTPLAYER);
for (int i = 0; i < NUM_FACTORIES; i++)
{
TileType temp[4] = {NOTILE, NOTILE, NOTILE, NOTILE};
for (int j = 0; j < FACTORY_SIZE; j++)
{
if (bag->size() == 0)
{
TileList *tmp = bag;
bag = lid;
lid = tmp;
tmp = nullptr;
delete tmp;
}
temp[j] = bag->removeFront();
}
//Refill factories
factories[i]->fill(temp);
}
}
bool exit = false;
menu->printMessage("=== Start Round ===");
while (!exit && !roundOver())
{
//Print player turn GUI
menu->handStart(playerTurnID->getName());
menu->printFactory(¢erPile);
for (int i = 0; i < NUM_FACTORIES; i++)
{
menu->printFactory(i + 1, factories[i]->toString());
}
menu->printMosaic(playerTurnID);
//Bool to check whether input command has been executed
bool inputDone = false;
//Handle user input
do
{
std::stringstream ss(menu->getInput());
if (!std::cin.eof())
{
string errorMessage = "Command not recognised";
string command;
ss >> command;
if (command == "turn")
{
errorMessage = "Invalid syntax for turn command";
unsigned int factoryNum = NUM_FACTORIES + 1, lineNum = NUMBER_OF_LINES + 1;
char colour = '\0';
ss >> factoryNum >> colour >> lineNum;
TileType tileType = charToTileType(colour);
--lineNum;
//Error checking with helpful messages
if (validFactoryNum(factoryNum))
{
if (selectableTile(colour))
{
if (validLineNum(lineNum))
{
//If drawing from center pile
if (factoryNum == 0)
{
if (!centerPile.empty() && centerPileContains(tileType))
{
//If moving tiles straight to broken line
if (lineNum == 5)
{
playerTurnID->getMosaic()->addToBrokenTiles(drawFromCenter(tileType), tileType);
inputDone = true;
}
//If moving tiles to pattern line
else if (!playerTurnID->getMosaic()->isFilled(lineNum, tileType) &&
playerTurnID->getMosaic()->getLine(lineNum)->canAddTiles(tileType))
{
if (containsFirstPlayer())
{
playerTurnID->getMosaic()->getBrokenTiles()->addFront(FIRSTPLAYER);
}
playerTurnID->getMosaic()->insertTilesIntoLine(lineNum, drawFromCenter(tileType), tileType);
inputDone = true;
}
else
errorMessage = "Specified line cannot add tile";
}
else
errorMessage = "Center pile does not contain specified tile";
}
//If drawing from factory
else
{
//-1 from factory num input as index starts at 0
--factoryNum;
if (!factories[factoryNum]->isEmpty())
{
if (factories[factoryNum]->contains(tileType))
{
//if Adding straight to broken tiles
if (lineNum == 5)
{
playerTurnID->getMosaic()->addToBrokenTiles(factories[factoryNum]->draw(tileType), tileType);
for (TileType tile : factories[factoryNum]->empty())
{
centerPile.push_back(tile);
}
inputDone = true;
}
//Adding to pattern line
else if (!playerTurnID->getMosaic()->isFilled(lineNum, tileType) &&
playerTurnID->getMosaic()->getLine(lineNum)->canAddTiles(tileType))
{
playerTurnID->getMosaic()->insertTilesIntoLine(lineNum, factories[factoryNum]->draw(tileType), tileType);
for (TileType tile : factories[factoryNum]->empty())
{
centerPile.push_back(tile);
}
inputDone = true;
}
else
errorMessage = "Specified line cannot add tile";
}
else
errorMessage = "Selected factory does not contain specified tile";
}
else
errorMessage = "Selected factory is empty";
}
// If the input has been executed
if (inputDone)
{
//Change player turn
changePlayerTurn();
menu->printMessage("Turn successful.");
}
}
else
errorMessage = "Invalid line number";
}
else
errorMessage = "Invalid colour";
}
else
errorMessage = "Invalid factory number";
}
// Command to save game to file
else if (command == "save")
{
errorMessage = "Invalid syntax for save command";
std::string fileName;
if (!ss.eof())
{
ss.get(); // removes initial whitespace between save and filename
std::getline(ss, fileName);
if (isNotWhiteSpace(fileName))
{
Saver saver;
saver.save(this, fileName);
inputDone = true;
}
}
if (inputDone)
menu->printMessage("Game successfully saved to '" + fileName + "'");
}
if (!inputDone)
menu->printMessage("Invalid input: " + errorMessage + ", try again");
}
else
{
exit = true;
}
} while (!exit && !inputDone);
}
if (!exit)
{
//Distribute tiles to walls
menu->printMessage("=== END OF ROUND ===");
for (auto player : players)
{
if (player->hasFirstPlayer())
{
playerTurnID = player;
}
for (TileType tile : player->calcScore())
{
lid->addBack(tile);
}
menu->printScore(player->getName(), player->getScore());
}
}
return exit;
}
bool GameEngine::validLineNum(int lineNum)
{
return lineNum >= 0 && lineNum <= NUMBER_OF_LINES;
}
bool GameEngine::validFactoryNum(int factoryNum)
{
return factoryNum >= 0 && factoryNum <= NUM_FACTORIES;
}
bool GameEngine::roundOver()
{
// centerPile.empty() && factoriesAreEmpty(); equivalent but less efficient
return !(!centerPile.empty() || !factoriesAreEmpty());
}
// returns true if factories are empty
bool GameEngine::factoriesAreEmpty()
{
bool factoriesEmpty = true;
int factoryIndex = 0;
while (factoriesEmpty && factoryIndex < NUM_FACTORIES)
{
factoriesEmpty &= factories[factoryIndex++]->isEmpty();
}
return factoriesEmpty;
}
void GameEngine::setPlayerTurn(int playerIndex)
{
playerTurnID = players[playerIndex];
}
void GameEngine::changePlayerTurn()
{
if (players[0] == playerTurnID)
{
playerTurnID = players[1];
}
else
{
playerTurnID = players[0];
}
}
Factory *GameEngine::getFactory(int position)
{
return factories[position];
}
void GameEngine::fillBag(int seed)
{
//Create both a bag and shuffle vector
TileType tileTypes[5] = {BLACK, LIGTHBLUE, DARKBLUE, YELLOW, RED};
TileType temp[100];
//Store all tiles in temp bag SORTED
for (int i = 0; i < 100; i++)
{
temp[i] = tileTypes[i / 20];
}
//Shuffle temp bag
std::shuffle(std::begin(temp), std::end(temp), std::default_random_engine(seed));
//Store shuffled tiles into the game bag
for (int i = 0; i < 100; i++)
{
bag->addBack(temp[i]);
}
}
int GameEngine::drawFromCenter(TileType colour)
{
int count = 0;
std::vector<int> remove;
int decrement = 0;
//Loop through vector saving indexes of tiles to be erased
for (int i = 0; i < (int)centerPile.size(); i++)
{
if (centerPile[i] == colour)
{
count++;
remove.push_back(i);
}
}
//Loop through indexes
for (int i = 0; i < (int)remove.size(); i++)
{
// erase element at index
centerPile.erase(centerPile.begin() + (remove[i] - decrement));
// Reduce all future indexes by 1
decrement++;
}
return count;
}
bool GameEngine::containsFirstPlayer()
{
bool contains = false;
if (centerPile[0] == FIRSTPLAYER)
{
centerPile.erase(centerPile.begin());
contains = true;
}
return contains;
}
void GameEngine::fillBag(TileList *bag)
{
if (this->bag != nullptr) delete this->bag;
this->bag = bag;
}
void GameEngine::fillLid(TileList *lid)
{
if (this->lid != nullptr) delete this->lid;
this->lid = lid;
}
void GameEngine::fillCenterPile(std::vector<TileType> centerPile)
{
this->centerPile = centerPile;
}
void GameEngine::fillFactories(Factory *factories[])
{
for (int i = 0; i < NUM_FACTORIES; ++i)
{
if (this->factories[i] != nullptr)
delete this->factories[i];
this->factories[i] = factories[i];
}
}
Player *GameEngine::addPlayer(string name)
{
//creates a new player
return addPlayer(name, 0, new Mosaic());
}
Player *GameEngine::addPlayer(std::string name, int score, Mosaic *mosaic)
{
players.push_back(new Player(name, score, mosaic));
return players.back();
}
void GameEngine::addPlayers()
{
//Checks for player names and adds them to player vector
bool hasValidName = true;
string name1;
do
{
menu->printMessage("Enter the name for player 1:");
name1 = menu->getInput();
hasValidName = isNotWhiteSpace(name1);
if (!hasValidName)
menu->printMessage("Error - Invalid Name");
} while (!hasValidName);
hasValidName = true;
string name2;
do
{
menu->printMessage("Enter the name for player 2:");
name2 = menu->getInput();
hasValidName = isNotWhiteSpace(name2);
if (!hasValidName)
menu->printMessage("Error - Invalid Name");
} while (!hasValidName);
addPlayer(name1);
addPlayer(name2);
menu->printMessage("Let's Play!");
}
std::vector<TileType> GameEngine::getCenterPile()
{
return centerPile;
}
TileList *GameEngine::getBag()
{
return bag;
}
TileList *GameEngine::getLid()
{
return lid;
}
Player *GameEngine::getPlayerTurnID()
{
return playerTurnID;
}
Player *GameEngine::getPlayer(int playerIndex)
{
return players.at(playerIndex);
}
bool GameEngine::hasPlayerWon()
{
bool playerWon = false;
unsigned int playerIndex = 0;
while (!playerWon && playerIndex < players.size())
playerWon = players.at(playerIndex++)->hasWon();
return playerWon;
}
bool GameEngine::centerPileContains(TileType tileType)
{
bool centerPileContainsTile = false;
unsigned int index = 0;
while (!centerPileContainsTile && index < centerPile.size())
{
centerPileContainsTile = centerPile[index] == tileType;
index++;
}
return centerPileContainsTile;
}