-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameEngine.cpp
678 lines (612 loc) · 18.2 KB
/
GameEngine.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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#include "GameEngine.h"
#include "GameObservers.h"
#include <iostream>
#include <fstream>
#include <filesystem>
#include <string>
#include <random>
#include <algorithm>
#ifdef _DEBUG
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
using namespace std;
namespace fs = std::filesystem;
MapDirectoryInit::MapDirectoryInit()
{
cout << "--Welcome to Warzone Game--" << endl;
//display map files in folder
std::string path = "MapFiles";
MapLoader* mapLoader;
ConquestFileReader* conMapLoader;
for (const auto & entry : fs::recursive_directory_iterator(path))
{
std::cout << entry.path().filename() << std::endl;
}
while(1)
{
int fileType;
//ask user to pick
cout << "\nPlease enter the name of the map file you wish to load: " << endl;
cin >> selectedMapName;
cout << "" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//use user input as parameter to MapLoader() constructor
mapLoader = DBG_NEW MapLoader(selectedMapName);
//If it is a Warzone file
if(mapLoader->getStatus())
{
if(mapLoader->getMap() != NULL)
{
gameMapPtr = mapLoader->getMap();
delete mapLoader;
break;
}
delete mapLoader;
}
else if(mapLoader->getMap() == NULL)
{
//Try the input with Conquest adapter
delete mapLoader;
conMapLoader = DBG_NEW ConquestFileReader(selectedMapName);
ConquestFileReaderAdapter* conquestFileReaderAdapter = new ConquestFileReaderAdapter(conMapLoader);
conquestFileReaderAdapter->loadMap(selectedMapName);
//if it is a Conquest map file
if (conMapLoader->getStatus())
{
if(conMapLoader->getMap() != NULL)
{
gameMapPtr = conMapLoader->getMap();
delete conquestFileReaderAdapter;
break;
}
}
else
{
delete conquestFileReaderAdapter;
}
}
}
}
string MapDirectoryInit::getSelectedMapName()
{
return selectedMapName;
}
Map* MapDirectoryInit::getGameMap()
{
return gameMapPtr;
}
MapDirectoryInit::~MapDirectoryInit(){
delete gameMapPtr;
}
//copy constructor
MapDirectoryInit::MapDirectoryInit(const MapDirectoryInit& c) :
selectedMapName(c.selectedMapName),
gameMapPtr(new Map(*c.gameMapPtr)) {}
//assignmnet operator overload
MapDirectoryInit &MapDirectoryInit::operator = (const MapDirectoryInit& o)
{
selectedMapName = o.selectedMapName;
gameMapPtr = o.gameMapPtr;
return *this;
}
//stream i/o overloads
istream &operator >> (istream &stream, MapDirectoryInit &o)
{
stream >> o.selectedMapName;
return stream;
}
ostream &operator << (ostream &out, const MapDirectoryInit &o)
{
out << o.selectedMapName;
return out;
}
PlayerListInit::PlayerListInit()
{
bool validEntry = false;
string playerName;
playerListPtr = DBG_NEW vector<Player*>;
//set number of players
while(!validEntry)
{
cout << "Please enter the number of players in the game (2-5): " << endl;
if((cin >> numOfPlayers) && numOfPlayers >= 2 && numOfPlayers <= 5)
{
validEntry = true;
}
else
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Not a valid entry, please enter a value between 2 and 5.\n" << endl;
}
}
//create player objects and add them to the player list
for (int i= 0; i<numOfPlayers; i++)
{
string playerName;
cout << "Please enter player " << i+1 << "'s name"<< endl;
cin >> playerName;
bool b = true;
Player* player;
while (b) {
cout << "please input the number corresponding to the player type\n1: Human ai\n2: Aggresive ai\n3: benevolent ai\n4: neutral ai\n";
int i = -1;
cin >> i;
switch (i) {
case 1: {
player = DBG_NEW Player(playerName, new HumanPlayerStrategy());
b = false;
break;
}
case 2: {
player = DBG_NEW Player(playerName, new AggressivePlayerStrategy());
b = false;
break;
}
case 3: {
player = DBG_NEW Player(playerName, new BenevolentPlayerStrategy());
b = false;
break;
}
case 4: {
player = DBG_NEW Player(playerName, new NeutralPlayerStrategy());
b = false;
break;
}
default:
cout << "inccorrect number please try again";
}
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
playerListPtr->push_back(player);
}
//Generate deck as a function of the number of players
deckPtr = DBG_NEW Deck(numOfPlayers);
cout << "Displaying Deck: " << endl;
cout << "\n" << *deckPtr;
string yesNo;
cout << "Do you wish to have phase information displayed on screen?(Yes/No) "<< endl;
cin >> yesNo;
if(yesNo == "No")
{
setDisplayPhaseInfo(false);
}
else
{
setDisplayPhaseInfo(true);
}
cout << "\nDo you wish to have game statistics information displayed on screen?(Yes/No) "<< endl;
cin >> yesNo;
if(yesNo == "No")
{
setDisplayStatsInfo(false);
}
else
{
setDisplayStatsInfo(true);
}
}
PlayerListInit::~PlayerListInit() {
delete deckPtr;
for (int i = 0; i < playerListPtr->size(); i++) {
delete playerListPtr->at(i);
}
delete playerListPtr;
}
vector<Player*>* PlayerListInit::getPlayerList()
{
return playerListPtr;
}
int PlayerListInit::getNumOfPlayers()
{
return numOfPlayers;
}
Deck* PlayerListInit::getDeckPtr()
{
return deckPtr;
}
bool PlayerListInit::getDisplayPhaseInfo()
{
return phaseObservers;
}
void PlayerListInit::setDisplayPhaseInfo(bool b)
{
phaseObservers = b;
}
bool PlayerListInit::getDisplayStatsInfo()
{
return statsObservers;
}
void PlayerListInit::setDisplayStatsInfo(bool b)
{
statsObservers = b;
}
//copy constructor
PlayerListInit::PlayerListInit(const PlayerListInit& c) :
numOfPlayers(c.numOfPlayers),
playerListPtr(new vector<Player*>(*c.playerListPtr)),
deckPtr(new Deck(*c.deckPtr)),
phaseObservers(c.phaseObservers),
statsObservers(c.statsObservers) {}
//assignmnet operator overload
PlayerListInit &PlayerListInit::operator = (const PlayerListInit& o)
{
numOfPlayers = o.numOfPlayers;
playerListPtr = o.playerListPtr;
deckPtr = o.deckPtr;
phaseObservers = o.phaseObservers;
statsObservers = o.statsObservers;
return *this;
}
ostream &operator << (ostream &out, const PlayerListInit &o)
{
out << "This is a PlayerInit Object";
return out;
}
GameInit::GameInit(vector<Player*>* plPtr, Map* gmPtr, PlayerListInit* pli)
{
if(gmPtr == NULL)
{
cout << "Map is null" << endl;
}
pliPtr = pli;
playerListPtr = plPtr;
gameMapPtr = gmPtr;
gameDeckPtr = pli->getDeckPtr();
startupPhase(playerListPtr, gameMapPtr);
}
GameInit::~GameInit() {
}
void GameInit::startupPhase(vector<Player*>* playerListPtr, Map* gameMapPtr)
{
//randomly determine order of play
//rng used as seed for std::mt19337
std::random_device rd;
//mersenne-twister-engine to ensure high quality "shuffle"
std::mt19937 g(rd());
//shuffle playerList, play order will be the order in which players appear in the shuffled list
std::shuffle(playerListPtr->begin(), playerListPtr->end(), g);
//randomly assign all territories in Map object to one player (ie. dont assign 2 players to the same territory)
vector<int> territoryIndices;
vector<int> playerListIndices;
vector<Territory*>* territoriesPtr = gameMapPtr->getTerritories();
for(int i=0; i<gameMapPtr->getTerritories()->size(); i++)
{
territoryIndices.push_back(i);
//lists of indices to shuffle, of same size as territoryList
}
//list of indices for the playerList
for(int i=0; i<playerListPtr->size(); i++)
{
playerListIndices.push_back(i);
}
//shuffle territory indices to randomize territory assignment
std::shuffle(territoryIndices.begin(), territoryIndices.end(), g);
//assign territories to players without "double booking"
for(int j=0; j<territoryIndices.size(); j+=playerListIndices.size())
{
for(int k=0; k<playerListIndices.size(); k++)
{
//if there aren't enough remaining territories to assign one to each player, the remaining territories are neutral.
if((j+playerListIndices.size())<=territoryIndices.size())
{
//set territory owner
delete territoriesPtr->at(territoryIndices.at(j + k))->getterritory_owner();
territoriesPtr->at(territoryIndices.at(j + k))->setterritory_owner(playerListPtr->at(k));
//add territory to player's toDefend list
playerListPtr->at(k)->gettoDefend(*gameMapPtr)->push_back(territoriesPtr->at(territoryIndices.at(j+k)));
}
}
}
//Give players armies A to distribute (Dont actually let them distribute just give them an initial count)
const int n = playerListPtr->size();
switch(n)
{
case 2:
{
for(auto player : *playerListPtr)
{
player->addReinforcements(40);
}
break;
}
case 3:
{
for(auto player : *playerListPtr)
{
player->addReinforcements(35);
}
break;
}
case 4:
{
for(auto player : *playerListPtr)
{
player->addReinforcements(30);
}
break;
}
case 5:
{
for(auto player : *playerListPtr)
{
player->addReinforcements(25);
}
break;
}
}
}
vector<Player*>* GameInit::getPlayerListPtr()
{
return playerListPtr;
}
Map* GameInit::getGameMapPtr()
{
return gameMapPtr;
}
Deck* GameInit::getGameDeckPtr()
{
return gameDeckPtr;
}
PlayerListInit* GameInit::getpliPtr()
{
return pliPtr;
}
//copy constructor
GameInit::GameInit(const GameInit& c) :
pliPtr(new PlayerListInit(*c.pliPtr)),
playerListPtr(new vector<Player*>(*c.playerListPtr)),
gameMapPtr(new Map(*c.gameMapPtr)),
gameDeckPtr(c.gameDeckPtr) {}
//assignmnet operator overload
GameInit &GameInit::operator = (const GameInit& o)
{
pliPtr = o.pliPtr;
playerListPtr = o.playerListPtr;
gameMapPtr = o.gameMapPtr;
gameDeckPtr = o.gameDeckPtr;
return *this;
}
ostream &operator << (ostream &out, const GameInit &o)
{
out << "This is a GameInit Object";
return out;
}
WarzoneGame::WarzoneGame(GameInit* gi)
{
playerListPtr = gi->getPlayerListPtr();
gameMapPtr = gi->getGameMapPtr();
gameDeckPtr = gi->getGameDeckPtr();
hasWon = false;
gameInitPtr = gi;
}
bool WarzoneGame::ordersRemain()
{
for(Player* player : *playerListPtr)
{
if(!player->getOrderList()->empty())
{
return false;
}
}
return true;
}
WarzoneGame::~WarzoneGame() {
}
void WarzoneGame::reinforcementPhase(Player *player, int numTerrOwned)
{
setCurrentPhase(0);
const int MIN_REINFORCEMENT = 3;
int reinforcement = 0;
int continentBonus = player->getContinentBonus(gameMapPtr);
reinforcement += player->getNumTerrOwned() / 3;
reinforcement += continentBonus;
//to make sure that the player has the minimum reinforcement
if (reinforcement < MIN_REINFORCEMENT) {
reinforcement = MIN_REINFORCEMENT;
}
//Place the reinforcements in the players' pools.
player->addReinforcements(reinforcement);
//notify subscribers of change of phase to display the troops that were added on turn start.
Notify();
}
void WarzoneGame::issueOrdersPhase(Player* player)
{
setCurrentPhase(1);
player->issueOrder(gameMapPtr, playerListPtr, gameDeckPtr);
Notify();
}
void WarzoneGame::executeOrdersPhase()
{
setCurrentPhase(2);
setExecutionQueue();
for(Order* order : executionQueue)
{
cout << "Executing " << *order << endl;
if(dynamic_cast<Advanceorder*>(order))
{
order->execute();
}
else
{
order->execute();
}
}
Notify();
for (int i = 0; i < executionQueue.size(); i++) {
delete executionQueue.at(i);
}
executionQueue.clear();
}
void WarzoneGame::mainGameLoop()
{
//Game loop runs until only the winner remains
int counter = 0;
while(playerListPtr->size()>1)
{
//Remove players who own no territories
for(int i=0; i<playerListPtr->size(); i++)
{
if(playerListPtr->at(i)->getNumTerrOwned()<=0)
{
cout << "Player: " << *playerListPtr->at(i) << " has been eliminated" << endl;
delete playerListPtr->at(i);
playerListPtr->erase(playerListPtr->begin()+i);
//update the observers with new playerlist
Notify();
}
}
//Each player gets their turn according to the playerList order (from startup phase)
if (playerListPtr->size() > 1) {
for (Player* player : *playerListPtr)
{
setCurrentPlayer(player);
//Player draws a card only if they conquered a territory in the previous turn
if (player->getName().compare("Neutral") != 0 && player->getcaptureTerritory()) {
gameDeckPtr->draw(player->getHand());
player->setcaptureTerritory(false);
}
reinforcementPhase(player, player->getNumTerrOwned());
player->clear();
issueOrdersPhase(player);
}
//All players are done issuing orders, execution of orders can begin
executeOrdersPhase();
}
counter++;
if (counter > 300) break;
//End the game for domonstrative purposes
}
//Only one player remains in the playerList, declare a winner
cout << *playerListPtr->at(0) << " Wins!" << endl;
setHasWon(true);
Notify();
}
Player* WarzoneGame::getCurrentPlayer()
{
return currentPlayer;
}
void WarzoneGame::setCurrentPlayer(Player* p)
{
currentPlayer = p;
}
Map* WarzoneGame::getGameMap()
{
return gameMapPtr;
}
void WarzoneGame::setExecutionQueue()
{
vector<Order*> executionVector;
while(!this->ordersRemain())
{
for (int k = 0; k< playerListPtr->size(); k++){
vector<Order*>* ol = playerListPtr->at(k)->getOrderList();
int priority = 99;
int index = -1;
for (int i = 0; i < ol->size(); i++){
Deployorder* o = dynamic_cast<Deployorder*> (ol->at(i));
Advanceorder* o1 = dynamic_cast<Advanceorder*> (ol->at(i));
Airliftorder* o2 = dynamic_cast<Airliftorder*> (ol->at(i));
Blockadeorder* o3 = dynamic_cast<Blockadeorder*> (ol->at(i));
Bomborder* o4 = dynamic_cast<Bomborder*> (ol->at(i));
Negotiateorder* o5 = dynamic_cast<Negotiateorder*> (ol->at(i));
Reinforcementorder* o6 = dynamic_cast<Reinforcementorder*> (ol->at(i));
if (o!=NULL){
priority = 1;
index = i;
break;
}
if (o1!=NULL && priority >4){
priority = 4;
index = i;
}
if (o2!=NULL && priority >2){
priority = 2;
index = i;
}
if (o3!=NULL && priority >3){
priority = 3;
index = i;
}
if (o4!=NULL && priority >4){
priority = 4;
index = i;
}
if (o5!=NULL && priority >4){
priority = 4;
index = i;
}
if (o6!=NULL && priority >4){
priority = 4;
index = i;
}
}
if (ol->empty())continue;
executionVector.push_back(ol->at(index));
ol->erase(ol->cbegin()+index);
}
}
executionQueue = executionVector;
}
vector<Order*> WarzoneGame::getExecutionQueue()
{
return executionQueue;
}
int WarzoneGame::getCurrentPhase()
{
return currentPhase;
}
void WarzoneGame::setCurrentPhase(int n)
{
currentPhase = n;
}
bool WarzoneGame::getHasWon()
{
return hasWon;
}
void WarzoneGame::setHasWon(bool b)
{
hasWon = b;
}
vector<Player*> WarzoneGame::getPlayerList()
{
return *playerListPtr;
}
GameInit* WarzoneGame::getGameInitPtr()
{
return gameInitPtr;
}
//copy constructor
WarzoneGame::WarzoneGame(const WarzoneGame& c) :
playerListPtr(new vector<Player*>(*c.playerListPtr)),
gameMapPtr(new Map(*c.gameMapPtr)),
gameDeckPtr(new Deck(*c.gameDeckPtr)),
currentPlayer(new Player(*c.currentPlayer)),
executionQueue(c.executionQueue),
currentPhase(c.currentPhase),
hasWon(c.hasWon),
gameInitPtr(new GameInit(*c.gameInitPtr)) {}
//assignmnet operator overload
WarzoneGame &WarzoneGame::operator = (const WarzoneGame& o)
{
playerListPtr = o.playerListPtr;
gameMapPtr = o.gameMapPtr;
gameDeckPtr = o.gameDeckPtr;
currentPlayer = o.currentPlayer;
executionQueue = o.executionQueue;
currentPhase = o.currentPhase;
hasWon = o.hasWon;
gameInitPtr = o.gameInitPtr;
return *this;
}
ostream &operator << (ostream &out, const WarzoneGame &o)
{
out << "This is a WarzoneGame Object";
return out;
}