-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
294 lines (262 loc) · 9.63 KB
/
Game.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
#include "Game.h"
#include "input_util.h"
#include "FileUtil.h"
#include "QwirkleGameEngine.h"
#include <utility>
#include <iostream>
#include <sstream>
Game::Game(const std::vector<SharedPlayer>& players,
std::shared_ptr<std::map<string, bool>> enhancementsToStatusMap) {
this->players = std::make_shared<std::vector<SharedPlayer>>(players);
this->board = std::make_shared<GameBoard>(enhancementsToStatusMap);
this->tileBag = std::make_shared<LinkedList>();
this->currentPlayer = players.at(0);
this->enhancementsToStatusMap = enhancementsToStatusMap;
}
Game::~Game() = default;
Game::Game(const SharedVector<SharedPlayer> &players,
SharedPlayer currentPlayer, std::shared_ptr<GameBoard> board,
std::shared_ptr<LinkedList> tileBag,
std::shared_ptr<std::map<string, bool>> enhancementsToStatusMap) {
this->players = players;
this->currentPlayer = std::move(currentPlayer);
this->board = std::move(board);
this->tileBag = std::move(tileBag);
this->enhancementsToStatusMap = enhancementsToStatusMap;
}
void Game::initiation(unsigned int seed) {
std::vector<SharedTile> tileVector = createTileBag();
shuffleTileBag(tileVector, seed);
setUpPlayerHands();
}
void Game::start() {
while (!isFinished()) {
auto lastPlayer = currentPlayer;
std::cout << std::endl << currentPlayer->getName()
<< ", it's your turn" << std::endl;
printPlayerScores();
board->displayBoard();
std::cout << "Your hand is" << std::endl;
auto hand = currentPlayer->getHand();
if (hand->isEmpty()) {
std::cout << "<no items>" << std::endl;
} else {
std::map<string, bool>::iterator it =
enhancementsToStatusMap->find(COLOUR_MODE);
std::cout << hand->toString(it != enhancementsToStatusMap->end()
? it->second : false, false) << std::endl;
}
// While player hasn't finished their turn
while (lastPlayer == currentPlayer) {
string input = input_util::getStringInput(
std::regex(COMMAND_REGEX));
std::stringstream args = std::stringstream(input);
string command;
args >> command;
if (command == "save") {
saveCommand(args);
} else if (command == "place") {
placeCommand(args);
} else if (command == "replace") {
replaceCommand(args);
}
}
}
std::cout << "Game over" << std::endl;
printPlayerScores();
printWinningPlayer();
std::cout << std::endl;
QwirkleGameEngine::quit();
}
void Game::printWinningPlayer() {
SharedPlayer maxPlayer = nullptr;
for (SharedPlayer& player : *players) {
if (maxPlayer == nullptr ||
player->getScore() > maxPlayer->getScore()) {
maxPlayer = player;
}
}
std::vector<SharedPlayer> tiedPlayers;
for (SharedPlayer& player : *players) {
if (player->getScore() == maxPlayer->getScore() &&
player != maxPlayer) {
tiedPlayers.push_back(player);
}
}
if (tiedPlayers.empty()) {
std::cout << "Player " << maxPlayer->getName() << " won!" << std::endl;
} else {
tiedPlayers.push_back(maxPlayer);
std::cout << "Players ";
for (unsigned int i = 0; i < tiedPlayers.size(); i++) {
SharedPlayer tiedPlayer = tiedPlayers.at(i);
std::cout << tiedPlayer->getName();
if (i != tiedPlayers.size() - 1) {
std::cout << ", ";
}
}
std::cout << " tied with a score of " << maxPlayer->getScore() << "!"
<< std::endl;
}
}
void Game::printPlayerScores() {
for (SharedPlayer& player : *players) {
std::cout << "Score for " + player->getName() << ": "
<< player->getScore() << std::endl;
}
}
string Game::toString(bool isColour, bool isSaving) {
string results = "";
for (std::map<string, bool>::iterator it =
enhancementsToStatusMap->begin();
it != enhancementsToStatusMap->end(); it++) {
if (it->second) {
results += it->first + "\n";
}
}
std::map<string, bool>::iterator it =
enhancementsToStatusMap->find(MULTI_PLAYERS_MODE);
if (it != enhancementsToStatusMap->end() && it->second) {
std::stringstream ss;
string size;
ss << (int)players->size();
ss >> size;
results += size + "\n";
}
// Getting a string format of all the players in the game
for (SharedPlayer& player : *players) {
results += player->toString(isColour, isSaving);
}
// Getting a string format of the board, tileBag and the current player's name
results += this->board->toString();
results += this->tileBag->toString(false, true);
results += this->currentPlayer->getName();
return results;
}
std::vector<SharedTile> Game::createTileBag() {
std::vector<SharedTile> tileVector;
//First we would have to create the tile bag
for (const Colour& colour : constants::COLOURS) {
for (const Shape& shape : constants::SHAPES) {
//Create two tiles that are the same since there are 72 tiles (2 of each) in the tile bag.
SharedTile currentTile = std::make_shared<Tile>(colour, shape);
SharedTile currentTile2 = std::make_shared<Tile>(colour, shape);
tileVector.push_back(currentTile);
tileVector.push_back(currentTile2);
}
}
return tileVector;
}
void
Game::shuffleTileBag(std::vector<SharedTile> tileVector, unsigned int seed) {
//Proceed to shuffle these this tile vector with the seed generated from the system clock
std::shuffle(std::begin(tileVector), std::end(tileVector),
std::default_random_engine(seed));
//convert vector into tileBag Linked List
for (auto& tile : tileVector) {
tileBag->addTile(tile);
}
}
void Game::setUpPlayerHands() {
//Go through every player
for (SharedPlayer& player : *players) {
//pick out 6 tiles for the player
for (int tiles = 0; tiles < HAND_SIZE; tiles++) {
//select the tile
SharedTile tilePicked = tileBag->deleteTile(tileBag->getTile(
0)); //will perhaps change into a shared pointer
//add the tile to the persons hand.
player->addTile(tilePicked);
}
}
}
bool Game::isFinished() {
// if tilebag is empty and at least one player has no tiles left.
bool isAnyPlayerHandEmpty = false;
for (SharedPlayer& player : *players) {
if (player->getHand()->isEmpty()) {
isAnyPlayerHandEmpty = true;
}
}
bool ret = false;
if (tileBag->isEmpty() && isAnyPlayerHandEmpty) {
ret = true;
}
return ret;
}
SharedPlayer Game::nextPlayerTurn() {
SharedPlayer newCurrentPlayer = nullptr;
for (unsigned int i = 0; i < players->size() &&
newCurrentPlayer == nullptr; i++) {
auto player = players->at(i);
if (player == currentPlayer) {
currentPlayer = players->at((i + 1) % players->size());
newCurrentPlayer = currentPlayer;
}
}
return newCurrentPlayer;
}
void Game::replaceCommand(std::stringstream & args) {
string tileStr;
args >> tileStr;
SharedTile playerTile = currentPlayer->hasTile(tileStr[0],
std::stoi(
tileStr.substr(1)));
if (playerTile != nullptr) {
currentPlayer->removeTile(playerTile);
tileBag->addTile(playerTile);
SharedTile newTile = tileBag->deleteTile(tileBag->getTile(0));
currentPlayer->addTile(newTile);
std::map<string, bool>::iterator it =
enhancementsToStatusMap->find(COLOUR_MODE);
std::cout << std::endl << "Added "
<< newTile->toString(it != enhancementsToStatusMap->end()
? it->second : false, false)
<< " to your hand" << std::endl;
nextPlayerTurn();
} else {
std::cout << "Tile given isn't in your hand" << std::endl;
}
}
void Game::placeCommand(std::stringstream & args) {
string tileStr;
string pos;
args >> tileStr;
// do it twice to filter out the 'at'
args >> pos;
args >> pos;
SharedTile playerTile = currentPlayer->hasTile(tileStr[0],
std::stoi(
tileStr.substr(1)));
if (playerTile != nullptr) {
int points = board->placeTile(playerTile, pos[0],
std::stoi(pos.substr(1)));
if (points == -1) {
std::cout << "Cannot place a tile here" << std::endl;
} else {
if (points >= 12) {
std::cout << "QWIRKLE!" << std::endl;
}
currentPlayer->removeTile(playerTile);
currentPlayer->addScore(points);
SharedTile next = tileBag->deleteTile(tileBag->getTile(0));
if (next != nullptr) {
currentPlayer->addTile(next);
}
nextPlayerTurn();
}
} else {
std::cout << "Tile given isn't in your hand" << std::endl;
}
}
void Game::saveCommand(std::stringstream & args) {
string filename;
args >> filename;
try {
FileUtil::saveGame(filename, this);
std::cout << "Game saved successfully" << std::endl;
}
catch (const std::exception& ex) {
std::cout << "Failed to save: " << ex.what() << std::endl;
}
}