Repository navigation
Expand file tree
/
Copy pathGameEngine.cpp
More file actions
623 lines (547 loc) · 16.7 KB
/
Copy pathGameEngine.cpp
File metadata and controls
623 lines (547 loc) · 16.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#include "GameEngine.h"
#include "Map.h"
#include "Player.h"
#include "CommandProcessing.h"
#include "Card.h"
#include <string>
#include <cmath>
#include <vector>
#include <iostream>
#include <filesystem>
using namespace std;
namespace GameEngine
{
//default constructor
GameEngine::GameEngine()
{
deck = new Deck(50);
map = new Map();
usercommand = NULL;
currentgamestate = new GameState();
*currentgamestate = GameState::Start; //initialize game to Start state
//Sending command to log file
Subject::Notify("Current Game State: Start");
}
//copy constructor
GameEngine::GameEngine(const GameEngine& other)
{
//initialize, then copy
usercommand = NULL;
*usercommand = *other.usercommand;
//initialize, then copy
currentgamestate = new GameState();
*currentgamestate = *other.currentgamestate;
//initialize, then copy
map = new Map();
*map = *other.map;
//initialize, then copy
deck = new Deck();
*deck = *other.deck;
}
GameEngine& GameEngine::operator=(const GameEngine& other)
{
if (this == &other)
{
return *this;
}
//initialize, then copy
usercommand = NULL;
*usercommand = *other.usercommand;
//initialize, then copy
currentgamestate = new GameState();
*currentgamestate = *other.currentgamestate;
//initialize, then copy
map = new Map();
*map = *other.map;
//initialize, then copy
deck = new Deck();
*deck = *other.deck;
return *this;
}
ostream& operator <<(ostream& o, const GameEngine::GameState& g)
{
switch (g)
{
case GameEngine::GameState::Start:
o << "Start";
break;
case GameEngine::GameState::MapLoaded:
o << "MapLoaded";
break;
case GameEngine::GameState::MapValidated:
o << "MapValidated";
break;
case GameEngine::GameState::PlayersAdded:
o << "PlayersAdded";
break;
case GameEngine::GameState::AssignReinforcement:
o << "AssignReinforcement";
break;
case GameEngine::GameState::IssueOrders:
o << "IssueOrders";
break;
case GameEngine::GameState::ExecuteOrders:
o << "ExecuteOrders";
break;
case GameEngine::GameState::Win:
o << "Win";
break;
}
return o;
}
//destructor
GameEngine::~GameEngine()
{
delete usercommand;
delete currentgamestate;
delete map;
delete deck;
}
void GameEngine::setUserCommand(string a)
{
usercommand = new string(a);
}
GameEngine::GameState GameEngine::getGameState()
{
return *currentgamestate;
}
void GameEngine::checkCommand()
{
switch (*currentgamestate)
{
case GameState::Start:
if (*usercommand == "loadmap")
{
*currentgamestate = GameState::MapLoaded;
//Sending command to log file
Subject::Notify("Current Game State: MapLoaded");
}
else
{
cout << "Invaild command." << endl;
}
break;
case GameState::MapLoaded:
if (*usercommand == "loadmap")
{
*currentgamestate = GameState::MapLoaded;
//Sending command to log file
Subject::Notify("Current Game State: MapLoaded");
}
else if (*usercommand == "validatemap")
{
*currentgamestate = GameState::MapValidated;
//Sending command to log file
Subject::Notify("Current Game State: MapValidated");
}
else
{
cout << "Invaild command." << endl;
}
break;
case GameState::MapValidated:
if (*usercommand == "addplayer")
{
*currentgamestate = GameState::PlayersAdded;
//Sending command to log file
Subject::Notify("Current Game State: PlayersAdded");
}
else
{
cout << "Invaild command." << endl;
}
break;
case GameState::PlayersAdded:
if (*usercommand == "addplayer")
{
*currentgamestate = GameState::PlayersAdded;
//Sending command to log file
Subject::Notify("Current Game State: PlayersAdded");
}
else if (*usercommand == "assigncountries")
{
*currentgamestate = GameState::AssignReinforcement;
//Sending command to log file
Subject::Notify("Current Game State: ReinforcementAssigned");
}
else
{
cout << "Invaild command." << endl;
}
break;
case GameState::AssignReinforcement:
if (*usercommand == "issueorder")
{
*currentgamestate = GameState::IssueOrders;
//Sending command to log file
Subject::Notify("Current Game State: OrderIssued");
}
else
{
cout << "Invaild command." << endl;
}
break;
case GameState::IssueOrders:
if (*usercommand == "issueorder")
{
*currentgamestate = GameState::IssueOrders;
//Sending command to log file
Subject::Notify("Current Game State: OrderIssued");
}
else if (*usercommand == "endissueorders")
{
*currentgamestate = GameState::ExecuteOrders;
//Sending command to log file
Subject::Notify("Current Game State: OrderExecuted");
}
else
{
cout << "Invaild command." << endl;
}
break;
case GameState::ExecuteOrders:
if (*usercommand == "execorder")
{
*currentgamestate = GameState::ExecuteOrders;
//Sending command to log file
Subject::Notify("Current Game State: OrderExecuted");
}
else if (*usercommand == "endexecorders")
{
*currentgamestate = GameState::AssignReinforcement;
//Sending command to log file
Subject::Notify("Current Game State: ReinforcementAssigned");
}
else if (*usercommand == "win")
{
*currentgamestate = GameState::Win;
//Sending command to log file
Subject::Notify("Current Game State: Win!");
}
else
{
cout << "Invaild command." << endl;
}
break;
case GameState::Win:
if (*usercommand == "play")
{
*currentgamestate = GameState::Start;
//Sending command to log file
Subject::Notify("Current Game State: Start");
}
else if (*usercommand == "end")
{
exit(0);
}
else
{
cout << "Invaild command." << endl;
}
break;
}
}
void GameEngine::cleanUserCommand()
{
delete usercommand;
usercommand = NULL;
}
void GameEngine::startupPhase()
{
// ---------------------------------------- LoadMap ---------------------------------------- //
if(*usercommand == "loadmap")
{
//Retrieving all the available maps to load
string path = "./";
string* files = new string[5];
int count = 0;
for (const auto & entry : __fs::filesystem::directory_iterator(path))
{
string temp = entry.path();
if (temp.substr(temp.size() - 3) == "map")
{
temp = entry.path();
files[count] = temp.substr(2);
count++;
}
}
//Displaying all the available maps
int count2 = 0;
cout << "Please choose which map to load: (Type the name)" << endl;
for(int i = 0; i < count; i++)
{
cout << count2+1 << ": " << files[count2] << endl;
count2++;
}
//Storing name of file to pull data from:
string input_file_name;
cin >> input_file_name;
//Loading Map
MapLoader loader(input_file_name);
Map temp = loader.loadMap();
map->copy(temp);
}
// ---------------------------------------- validateMap ---------------------------------------- //
if(*usercommand == "validatemap")
{
//Validating Map
map->validate();
}
// ---------------------------------------- AddPlayer ---------------------------------------- //
if(*usercommand == "addplayer")
{
//Adding players
cout << "Please select how many players you would like to add (2-6): ";
int numPlayers = -1;
do
{
cin >> numPlayers;
cout << numPlayers;
if((numPlayers > 6) || (numPlayers < 2))
{
cout << "\nPlease try again, number of players must be between 2 and 6!" << endl;
numPlayers = -1;
}
}
while(numPlayers == -1);
for (int i = 0; i < numPlayers; i ++)
{
cout << "\nPlease enter name of Player # " << (i+1) << ": ";
string name;
cin >> name;
Player *temp2 = new Player(name);
players.push_back(temp2);
}
}
// ---------------------------------------- GameStart ---------------------------------------- //
//Distributing territories to players
if(*usercommand == "gamestart")
{
cout << map->getNumOfTerritories() << endl;
//Iterate through all territories
for (int i = 0; i < map->getNumOfTerritories(); i++)
{
//Iterate through all players
for(int y = 0; y < players.size(); y++)
{
Territory *temp = new Territory();
temp->copy(map->getTerritories()[i]);
players[y]->assignTerritory(temp);
i++;
}
i--;
}
// //To display territories of each player (testing)
// for(int y = 0; y < players.size(); y++)
// {
// cout << "Player: " << players[y]->getName() << endl;
// players[y]->printTerritories();
// }
//Giving 50 armies to the players
for(int y = 0; y < players.size(); y++)
{
players[y]->updateReinforcementPool(50);
}
//Making players draw two cards from the deck
for(int y = 0; y < players.size(); y++)
{
string cardType = deck->draw().getAssignedType();
Card *temp5 = new Card(cardType);
players[y]->addCardToHand(temp5);
cardType = deck->draw().getAssignedType();
temp5 = new Card(cardType);
players[y]->addCardToHand(temp5);
}
//Switching the game to the play phase
// //To test if everything is good!
// for(int y = 0; y < players.size(); y++)
// {
// cout << "\nplayer: " << players[y]->getName() << endl;
// players[y]->printHand();
// }
// deck->display();
}
}
void GameEngine::reinforcementPhase()
{
// ---------------------------------------- AssignReinforcement ---------------------------------------- //
//if(*usercommand == "assigncountries")
if(true)
{
vector<Continent> c = map->getContinents(); //retrieve continents list for user selected map
for (int i = 0; i < c.size() ; i++)
{
int cid = c[i].getId();
int count; //amount of territories for a continent
vector<Territory> t = map->getTerritories(); //retrieve territories list for user selected map
for (int j = 0; j < t.size(); j++)
{
if (t[j].getContinent_id() == cid) // territory's continent matches given continent in iteration then increment its amount of territories
{
count++;
}
}
for (int y = 0; y < players.size(); y++) //iterate through players
{
vector<Territory *> t = players[y]->getTerritories(); //list of player's owned territories
int pcount; //amount of territories owned by player for a continent
for (int i = 0; i < t.size(); i++) //iterate through players owned territories
{
if (t[i]->getContinent_id() == cid) //territory is in current continent then increment its count
pcount++;
}
if (pcount >= count) //player controls all of continent's territories then add bonus value to formula computing how many armies to add to their pool
{
int a = floor((players[y]->getNoTerritories()) / 3);
players[y]->updateReinforcementPool((players[y]->getArmies()) + a + 6);
}
else
{ //otherwise, proceed with regular formula: no of territories owned divided by 3, rounded down
int a = floor((players[y]->getNoTerritories()) / 3);
players[y]->updateReinforcementPool((players[y]->getArmies()) + a);
}
}
}
}
}
void GameEngine::issueOrdersPhase()
{
// ---------------------------------------- IssueOrders ---------------------------------------- //
//if(*usercommand == "issueorder")
if(true)
{
//iterate through players and have them issue orders to add to their orderlist
for (int y = 0; y < players.size(); y++) {
players[y]->issueOrder();
}
}
}
void GameEngine::executeOrdersPhase()
{
// ---------------------------------------- ExecuteOrders ---------------------------------------- //
//if(*usercommand == "execorder")
if(true)
{
//iterate through players and retrieve that player's orderlist and execute its orders
for (int y = 0; y < players.size(); y++) {
vector<Order *> ol = players[y]->getOrderList();
for (int i = 0; i < ol.size(); ++i) {
Order o = *ol[i];
o.execute();
}
}
}
}
//game play loop once the game has started
void GameEngine::mainGameloop()
{
cout << "In main game loop" << endl;
int total = map->getNumOfTerritories();
//iterate through players
for(int y = 0; y < players.size(); y++)
{
int t = players[y]->getNoTerritories();
if (t <= 0) //if player has no territories owned then they are eliminated from the game
{
players[y]->~Player();
}
if (t >= total) //if player owns all territories then the game is won and over
{
cout << "Game Over. " << players[y]->getName() << " has won." << endl;
return;
}
}
//otherwise there is no reason to break from game and loop will follow the three main phases
reinforcementPhase();
issueOrdersPhase();
executeOrdersPhase();
}
void GameEngine::stringToLog(string s)
{
stringsToLog = s;
}
void GameEngine::tournament(vector<string> listofmapfiles, vector<string> listofplayerstrategies, int numberofgames, int maxnumberofturns)
{
cout << "Creating Tournament..." << endl;
cout << listofmapfiles.size() << endl;
//Vector to store winners
vector<string> winners;
//For each map file
for(int i = 0; i < listofmapfiles.size()-1; i++)
{
cout << "\n*** For the map" << listofmapfiles[i] << ": ***" << endl;
//For each game
for(int y = 0; y < numberofgames; y++)
{
cout << "\nFor game #" << (y + 1) << ":" << endl;
//Resetting Deck
deck = new Deck(50);
//Loading Map
MapLoader loader(listofmapfiles[i]);
Map temp = loader.loadMap();
map->copy(temp);
//Validating Map
map->validate();
//Adding the players
for(int z = 0; z < listofplayerstrategies.size(); z++)
{
string name = "Player " + std::to_string(z+1) + " (" + listofplayerstrategies[z] + ")";
Player *temp2 = new Player(name);
players.push_back(temp2);
}
//Setup game (assign territories and hands to players)
usercommand = new string("gamestart");
startupPhase();
//Main game loop
string winner = "N/A";
for(int i = 0; i < maxnumberofturns; i++)
{
int total = map->getNumOfTerritories();
//iterate through players
for(int y = 0; y < players.size(); y++)
{
int t = players[y]->getNoTerritories();
if (t <= 0) //if player has no territories owned then they are eliminated from the game
{
players[y]->~Player();
}
if (t >= total) //if player owns all territories then the game is won and over
{
winner = players[y]->getName();
break;
}
}
//otherwise there is no reason to break from game and loop will follow the three main phases
reinforcementPhase();
issueOrdersPhase();
executeOrdersPhase();
}
//If no winner assigned declare a draw
if (winner == "N/A")
{
winner = "draw";
cout << "The game ended in a draw!";
winners.push_back("Draw!");
}
else
{
cout << "Game Over. " << players[y]->getName() << " has won." << endl;
winners.push_back(players[y]->getName());
}
}
}
//Display results & Log it
ofstream myFile;
myFile.open("TournamentResult.txt", ofstream::app);
cout << "\n\n*** Results ***\n" << endl;
for(int i = 0; i < listofmapfiles.size()-1; i++)
{
for(int y = 0; y < numberofgames; y++)
{
string temp = "- Map: " + listofmapfiles[i] + " -> Game #" + std::to_string(y+1) + ": " + winners[i+y];
cout << temp << endl;
myFile << temp << endl;
}
}
}
}