-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameController.cpp
More file actions
398 lines (356 loc) · 10.5 KB
/
Copy pathGameController.cpp
File metadata and controls
398 lines (356 loc) · 10.5 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
#include "GameController.h"
#include <iostream>
#include <string>
#include <sstream>
#include <regex>
#include <limits>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
GameController::GameController(int playerCount, bool colouredTiles)
{
this->colouredTiles = colouredTiles;
this->game = new Game(playerCount);
this->pCount = playerCount;
for (int i = 0; i < pCount; ++i)
{
addPlayer();
}
this->keepGoing = true;
}
GameController::GameController(bool colouredTiles, bool againstAI)
{
this->colouredTiles = colouredTiles;
this->game = new Game(colouredTiles);
this->pCount = 2;
addPlayer();
addAI();
this->keepGoing = true;
}
GameController::GameController(Player* players[],unsigned int playerCount, Board &board,
LinkedList &tileBag, int currentPlayerNo,
bool firstTurn, bool colouredTiles)
{
// For milestone 2, this constructor only creates 2-player games.
this->colouredTiles = colouredTiles;
this->pCount = playerCount;
this->game = new Game(pCount);
for(unsigned int i = 0; i < playerCount; i++){
game->addPlayer(players[i]);
}
game->setCurrentPlayer(currentPlayerNo);
game->setBoard(board);
game->setTileBag(tileBag);
this->keepGoing = true;
if (!firstTurn)
{
game->skipFirstTurn();
}
// If this is not included, the game status will not be displayed on the
// first turn of a loaded game.
printScoreBoardHand();
}
GameController::~GameController()
{
if (game != nullptr)
{
delete game;
}
}
void GameController::addPlayer()
{
bool correct = false;
string input = "";
while (correct == false)
{
cout << "Enter a name for player " << game->getPlayerCount() + 1
<< " (uppercase characters only)" << endl;
cout << "> ";
std::getline(std::cin, input);
if (validate_PlayerName(input))
{
correct = true;
}
else
{
cout << "Invalid Input: name is not exclusively UPPERCASE" << endl;
}
}
Player *newP = new Player(input);
game->addPlayer(newP);
delete newP;
}
void GameController::addAI(){
Player *newP = new Player(true);
game->addPlayer(newP);
delete newP;
}
void GameController::gameStart()
{
game->dealPlayerTiles();
game->setCurrentPlayer(0);
printScoreBoardHand();
}
void GameController::gameLoop()
{
string input = "";
while (keepGoing)
{
bool moveSuccess = false;
if(game->getCurrentPlayer()->getAIStatus()){
input = aiMoveSelect();
} else {
input = askForPlayerMove();
}
if (keepGoing)
{
// Validate and execute move
moveSuccess = validateAndExecute(input);
// If the last move emptied the player hand, end the game, this can
// only happen after the TileBag was emptied, so no need to check
// for that.
if (game->getCurrentPlayer()->getHand()->getSize() == 0)
{
cout << game->getBoard()->toString() << endl;
game->getCurrentPlayer()->setScore(
(game->getCurrentPlayer()->getScore() + 6));
keepGoing = false;
cout << "Game over" << endl;
for (int i = 0; i < pCount; ++i)
{
cout << "Score for " << game->getPlayer(i)->getName() << ": "
<< game->getPlayer(i)->getScore() << endl;
}
cout << "Player " << game->getWinner()->getName() << " won!"
<< endl;
}
else
{
// switch current player if move was a success, and reprint
// board, but not if the game has finished
if (moveSuccess == true)
{
game->nextPlayer();
printScoreBoardHand();
}
}
}
}
}
void GameController::skipFirstTurn()
{
game->skipFirstTurn();
}
string GameController::askForPlayerMove()
{
string input = "";
cout << endl;
cout << "> ";
std::getline(std::cin, input);
cout << endl;
if (std::cin.eof())
{
keepGoing = false;
}
return input;
}
bool GameController::validateAndExecute(string input)
{
bool moveSuccess = false;
// This block borrowed and modified from:
// https://www.fluentcpp.com/2017/04/21/how-to-split-a-string-in-c/
std::istringstream iss(input);
std::vector<std::string> results(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>());
// End derived block
if (validate_Place(input))
{
// Valid place command given
moveSuccess = makeAMove(results.at(1), results.at(3));
}
else if (validate_Replace(input))
{
// Valid replace command given
moveSuccess = replaceATile(results.at(1));
}
else if (validate_save(results))
{
// Because validate_save() is true, we know there is a second token
std::string filename = results.at(1);
if (game->saveGame(filename))
{
std::cout << "Game successfully saved" << std::endl;
}
else
{
std::cout << "Sorry, there was an error while saving the game"
<< std::endl;
}
}
else if (results.size() > 0 && results.at(0) == "quit")
{
// Valid quit command given
keepGoing = false;
}
else
{
// Invalid command given
std::cout << "Invalid Input" << std::endl;
}
return moveSuccess;
}
bool GameController::makeAMove(string tileSTR, string moveSTR)
{
bool success = false;
Tile *mTile = new Tile(tileSTR);
string colSTR = moveSTR.substr(1, 2);
int col = std::stoi(colSTR);
// Check if the player hand has the tile or not
if (game->getCurrentPlayer()->hasTile(mTile))
{
// Try to place the tile if the player has it
success = game->placeTile(*mTile, moveSTR.at(0), col);
// Remove tile from player hand if the move was valid
if (success)
{
game->removeTileCurrPlayer(mTile);
// Draw new tile from tileBag if there are any left
if (game->getTileBag()->getSize() > 0)
{
game->drawATile();
}
}
else
{
std::cout << "Move is invalid." << std::endl;
}
}
else
{
std::cout << "You don't have that tile" << std::endl;
}
delete mTile;
return success;
}
bool GameController::replaceATile(string tileSTR)
{
Tile *rTile = new Tile(tileSTR);
bool success = false;
// Check if the player has that tile or not, if yes...
if (game->getCurrentPlayer()->hasTile(rTile))
{
success = game->swapTile(rTile);
}
// If the action didn't succeed, we should tell the player
if (success == false)
{
std::cout << "Replacement not successful." << std::endl;
if (game->getTileBag()->getSize() < 1)
{
std::cout << "Tile bag is empty!" << std::endl;
}
}
delete rTile;
return success;
}
bool GameController::validate_Place(string input)
{
// https://www.softwaretestinghelp.com/regex-in-cpp/
// Modified from here
// regex expression for pattern to be searched
std::regex regex("^place [ROYGBP][1-6] at [A-Z][0-9]{1,2}$");
// flag type for determining the matching behavior (in this case on string
// objects)
std::smatch m;
// regex_search that searches pattern in the string
return std::regex_search(input, m, regex);
}
bool GameController::validate_Replace(string input)
{
// https://www.softwaretestinghelp.com/regex-in-cpp/
// Modified from here
std::regex regex("^replace [ROYGBP][1-6]$");
std::smatch m;
return std::regex_search(input, m, regex);
}
bool GameController::validate_PlayerName(string input)
{
// https://www.softwaretestinghelp.com/regex-in-cpp/
// Modified from here
std::regex regex("^[A-Z]+$");
std::smatch m;
return std::regex_search(input, m, regex);
}
bool GameController::validate_save(std::vector<std::string> &input)
{
bool isValid = false;
if (input.size() > 0)
{
isValid = input.size() == 2 && input.at(0) == "save";
}
return isValid;
}
void GameController::printScoreBoardHand()
{
// Print current state of the game/board
cout << endl
<< game->getCurrentPlayer()->getName() << ", it's your turn"
<< endl;
for (int i = 0; i < pCount; ++i)
{
cout << "Score for " << game->getPlayer(i)->getName() << ": "
<< game->getPlayer(i)->getScore() << endl;
}
if (colouredTiles)
{
cout << game->getBoard()->toConsoleString() << endl
<< endl
<< "Your hand is " << endl
<< game->getCurrentPlayer()->getHand()->toConsoleString() << endl;
}
else
{
cout << game->getBoard()->toString() << endl
<< endl
<< "Your hand is " << endl
<< game->getCurrentPlayer()->getHand()->toString() << endl;
}
}
string GameController::aiMoveSelect(){
int tempScore = 0;
int highScore = 0;
int tileIndex = -1;
int col = -1;
int row = -1;
string tileSTR = "";
string moveSTR = "";
string selectedMove = "";
for (int i = 0; i<game->getCurrentPlayer()->getHand()->getSize(); i++){
Tile *mTile = game->getCurrentPlayer()->getHand()->get(i);
for (int j = 0; j<game->getBoard()->getHeight(); j++){
for (int k = 0; k<game->getBoard()->getWidth(); k++){
if(game->validateTile(*mTile,j,k)){
tempScore = game->scoreTile(*mTile, j, k, false);
if(tempScore > highScore){
highScore = tempScore;
row = j;
col = k;
tileIndex = i;
}
}
}
}
}
if(highScore>0){
tileSTR = game->getCurrentPlayer()->getHand()->get(tileIndex)->toString();
moveSTR = game->getBoard()->positionString(row,col);
selectedMove = "place " + tileSTR + " at " + moveSTR;
}else{
selectedMove = "replace " + game->getCurrentPlayer()->getHand()->get(0)->toString();
}
cout<<endl;
cout<<selectedMove<<endl;
return selectedMove;
}