-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgameDriver.cpp
More file actions
736 lines (607 loc) · 19.3 KB
/
Copy pathgameDriver.cpp
File metadata and controls
736 lines (607 loc) · 19.3 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
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
Game Driver by Tyler Lauzon
Thought process:
First thing is first, a general idea of the process that the game driver needs has been given so
all thats needs to be done is for it to be converted to code.
The functions were written down for each phase, then from there it was seeing how they were
going to be linked together and what kind of information will be passed back and forth.
The design of the driver was inspired simply from the phases and their ordering. I was aiming
for a simple yet sturdy base to the upcoming project.
Game rules:
The rules for the driver are simple. Each player gets his/her turn to go through all the phases of the game.
Each player has their chance to place new units at the beginning of his/her turn followed by
an attack on adjacent countries. Then finally they can move their units around to fortify certain positions.
Players however will not be given any individual benefits unless they are earned (posession of
a continent or Risk card) in order to keep the games equal for all.
*/
#include "gameDriver.h"
#include "Player.h"
#include <iostream>
#include "aggressive.h"
#include <string>
#include <fstream>
using namespace std;
gameDriver::gameDriver()
{
myMap.loadMap("myMap.map"); //load contents from sample file downloaded from given website
whosTurn = 0;
phaseNum = 0;
}
gameDriver::gameDriver(map* myMap, Player** players, AI** computers, int numberOfPlayers, int whosTurn, int phaseNum)
{
this->myMap = *myMap;
this->players = players;
this->computers = computers;
this->numberOfPlayers = numberOfPlayers;
this->whosTurn = whosTurn;
this->phaseNum = phaseNum;
}
gameDriver::~gameDriver()
{
}
void gameDriver::startMenu(){
int response;
clearScreen();
cout << "0: Start a new game\n"
<< "1: Load a game\n\n"
<< "What would you like to do? ";
cin >> response;
//getline(cin, response);
if (response == 1)
{
string in;
clearScreen();
cout << "Saved Games\n"
<< "--------------------\n";
system("dir /b saves");
cout << "\n\nWhich game would you like to load? ";
cin.ignore();
getline(cin, in);
loadGame(in);
}
}
void gameDriver::start()
{
startMenu();
if (phaseNum == 0)
startPhase();
mainPhase();
}
//1) startup phase
//(where the number of players and map are chosen, and the countries are randomly assigned to the players);
void gameDriver::startPhase()
{
clearScreen();
cout << "This is the beginning of the start phase\n"
<< "--------------------------------------------\n\n";
do{
cout << "How many players will be participating in this game? (max: 6)";
cin >> numberOfPlayers;
//getline(cin, numberOfPlayers);
} while (numberOfPlayers > 6 || numberOfPlayers < 2);
vector<int> countryRefs;
for (int i = 0; i < sizeof(myMap.countries) / sizeof(myMap.countries[0]); i++){
countryRefs.push_back(i);
}
computers = new AI*[6 - numberOfPlayers];
for (int i = 0; i < 6 - numberOfPlayers; i++)
{
int infantry = 13;
computers[i] = new AI(i + 1);
for (int j = 0; j < 7; j++){
int rng = rand() % countryRefs.size();
myMap.countries[countryRefs[rng]].occupiedBy = computers[i];
if (j != 6){
myMap.countries[countryRefs[rng]].numberOfPieces += rand() % infantry;
infantry -= myMap.countries[countryRefs[rng]].numberOfPieces;
}
else
myMap.countries[countryRefs[rng]].numberOfPieces += infantry;
computers[i]->AddCountry(&myMap.countries[countryRefs[rng]]);
countryRefs.erase(countryRefs.begin() + rng);
}
}
for (int i = 0; i < 6 - numberOfPlayers; i++)
{
for (int j = 0; j < computers[i]->GetCountries().size(); j++)
{
computers[i]->GetCountries()[j]->numberOfPieces++;
}
}
players = new Player*[numberOfPlayers];
for (int i = 0; i < numberOfPlayers; i++)
{
int infantry = 13;
players[i] = new Player((i + 1), "player " + to_string(i + 1));
for (int j = 0; j < 7; j++){
int rng = rand() % countryRefs.size();
myMap.countries[countryRefs[rng]].occupiedBy = players[i];
if (j != 6){
myMap.countries[countryRefs[rng]].numberOfPieces += rand() % infantry;
infantry -= myMap.countries[countryRefs[rng]].numberOfPieces;
}
else
myMap.countries[countryRefs[rng]].numberOfPieces += infantry;
players[i]->AddCountry(&myMap.countries[countryRefs[rng]]);
countryRefs.erase(countryRefs.begin() + rng);
}
}
for (int i = 0; i < numberOfPlayers; i++)
{
for (int j = 0; j < players[i]->GetCountries().size(); j++)
{
players[i]->GetCountries()[j]->numberOfPieces++;
}
}
phaseNum++;
//Create the deck.
_deck = new Deck();
}
/*
the main play phase (a round-robin turn-based phase where the players are allowed to reinforce, attack and
move). The sub-phases of the main phase are:
*/
void gameDriver::mainPhase()
{
clearScreen();
cout << "This is the beginning of the main phase\n"
<< "-----------------------------------------\n\n";
while (true){
if (whosTurn < numberOfPlayers){
for (int id = whosTurn; id < numberOfPlayers; id++, whosTurn++){
cout << "\nPlayer " << players[id]->getID() << " turn\n";
cout << "------------------------------------------\n";
switch (phaseNum){
case 1:
reinforcementPhase(players[id]);
phaseNum++;
case 2:
attackPhase(players[id]);
if (myMap.isWinner())
gameOver();
phaseNum++;
case 3:
fortification(players[id]);
phaseNum = 1;
}
}
}
for (int id = (whosTurn-numberOfPlayers); id < 6 - numberOfPlayers; id++, whosTurn++){
cout << "\nComputer " << computers[id]->getID() << " turn\n";
cout << "------------------------------------------\n";
switch (phaseNum){
case 1:
reinforcementPhase(computers[id]);
phaseNum++;
case 2:
attackPhase(computers[id]);
if (myMap.isWinner())
gameOver();
phaseNum++;
case 3:
fortification(computers[id]);
phaseNum = 1;
}
}
whosTurn = 0;
clearScreen();
}
}
void gameDriver::gameOver(){
cout << "Congratulations " << myMap.countries[0].occupiedBy->name << " you are the winner!\n\n";
cout << "Game is over!\n\n";
system("pause");
exit(0);
}
//a) reinforcement phase (where a player is given some armies and places them on some if the countries he owns)
void gameDriver::reinforcementPhase(Player* user){
int total = 0;
clearScreen();
updatePlayerContinents();
if (int(user->GetCountries().size() / 3) > 3)
user->SetRenforcements(user->GetCountries().size() / 3);
else
user->SetRenforcements(3);
for (size_t i = 0; i < user->GetContinents().size(); i++)
total += user->GetContinents().at(i)->getArmyNum();
if (user->GetHand().Size() > 3){
string input;
do{
cout << "Do you want to trade in some cards for units? (" << cardReturns << " available) Y/N: ";
cin.ignore();
getline(cin, input);
cout << endl;
for (size_t i = 0; i < input.length(); i++)
input[i] = toupper(input[i]);
} while (input != "Y" || input != "N");
clearScreen();
if (input == "Y")
if (user->GetHand().TradeIn(user->isComputer))
total += getCardUnits();
}
user->SetRenforcements(user->GetRenforcements() + total);
cout << "This is the reinforcement phase for player " << user->getID() << endl;
cout << "---------------------------------------------------------\n\n";
int remaining = user->GetRenforcements();
int countryNum;
int armyNum;
while (remaining > 0){
clearScreen();
cout << "Remaining armies: " << remaining << endl;
output.PlayerStats(*user);
do{
cout << "\n\nWhere would you like to place your armies? (Must be between 1 and " << user->GetCountries().size() << ") ";
//(cin, countryNum);
cin >> countryNum;
} while (countryNum > int(user->GetCountries().size()));
do{
cout << "\n\nHow many armies would you like to place here? (Must be less than " << remaining << ") ";
//getline(cin, armyNum);
cin >> armyNum;
} while (armyNum > remaining);
user->GetCountries()[countryNum-1]->numberOfPieces += armyNum;
remaining -= armyNum;
}
mainMenu();
}
void gameDriver::reinforcementPhase(AI* comp){
int total = 0;
clearScreen();
updatePlayerContinents();
if (int(comp->GetCountries().size() / 3) > 3)
comp->SetRenforcements(comp->GetCountries().size() / 3);
else
comp->SetRenforcements(3);
for (size_t i = 0; i < comp->GetContinents().size(); i++)
total += comp->GetContinents().at(i)->getArmyNum();
if (comp->GetHand().Size() > 2){
if (comp->GetHand().TradeIn(comp->isComputer))
total += getCardUnits();
}
if (comp->GetHand().Size() > 2)
if (comp->GetHand().TradeIn(comp->isComputer))
total += getCardUnits();
comp->SetRenforcements(comp->GetRenforcements() + total);
cout << "This is the reinforcement phase for computer " << comp->getID() << endl;
cout << "---------------------------------------------------------\n\n";
int remaining = comp->GetRenforcements();
//clearScreen();
comp->GetCountries()[comp->strat->reinforce(comp)]->numberOfPieces += (int)floor(remaining*0.75);
remaining -= (int)floor(remaining*0.75);
while (remaining > 0)
{
comp->GetCountries()[rand() % comp->GetCountries().size()]->numberOfPieces++;
remaining--;
}
mainMenu();
}
//b) attack phase(where a player may declare a series of attacks from one of his countries to one of its adjacent countries owned by another player)
void gameDriver::attackPhase(Player* user){
string response = "Y";
while (response == "Y" && user->canPlay()){
clearScreen();
cout << "This is the attack phase for player " << user->getID() << endl;
cout << "---------------------------------------------------------\n\n";
cin.ignore();
output.PlayerStats(*user);
do{
cout << "Do you want to attack? (Y or N) ";
getline(cin, response);
for (size_t i = 0; i < response.length(); i++)
response[i] = toupper(response[i]);
} while (response != "Y" && response != "N");
if (response == "Y")
{
string from;
string to;
output.attackPlayerStats(*user);
do{
cout << "Where would you like to attack from? (1 - " << user->GetCountries().size() << ") ";
getline(cin, from);
} while (!isNumber(from) || (stoi(from) > user->GetCountries().size()));
Country* att;
do{
cout << "Which country would you like to attack? (Type the name of the country) ";
getline(cin, to);
to = toupperCase(to);
att = user->GetCountries()[stoi(from) - 1]->findAdjacent(to);
} while (att == nullptr);
Battle battle1(user->GetCountries().at(stoi(from) - 1), att);
}
}
mainMenu();
}
void gameDriver::attackPhase(AI* comp){
clearScreen();
cout << "This is the attack phase for computer " << comp->getID() << " using the " << comp->stratType << " strategy." << endl;
cout << "--------------------------------------------------------------------------\n\n";
comp->strat->execute(comp);
mainMenu();
}
//c) fortification phase (where a player may move some armies from one of his countries to another of his countries)
void gameDriver::fortification(Player* user){
struct validation{
Country* country;
vector<string> adjacent;
validation(Country* c){
country = c;
}
bool findAdj(string a){
for (size_t i = 0; i < adjacent.size(); i++)
if (adjacent[i] == a)
return true;
return false;
}
};
clearScreen();
if (user->canPlay()){
cout << "This is the fortification phase for player " << user->getID() << endl;
cout << "---------------------------------------------------------\n\n";
string answer;
bool quit = false;
output.fortificationPlayerStats(*user);
//cin.ignore();
do{
cout << "Would you like to move any armies? (Y or N) ";
getline(cin, answer);
for (size_t i = 0; i < answer.length(); i++)
answer[i] = toupper(answer[i]);
} while (answer != "Y" && answer != "N");
while (toupper(answer[0]) == 'Y')
{
string country1;
string country2;
int armies = 0;
vector<validation> validC;
for (size_t i = 0; i < user->GetCountries().size(); i++){
for (int j = 0; j < user->GetCountries()[i]->getAdjacentCount(); j++){
if (user->GetCountries()[i]->getAdjacent()[j]->occupiedBy->name == user->GetCountries()[i]->occupiedBy->name){
validC.push_back(validation(user->GetCountries()[i]));
break;
}
}
}
for (size_t i = 0; i < validC.size(); i++){
for (int j = 0; j < validC[i].country->getAdjacentCount(); j++){
if (validC[i].country->getAdjacent()[j]->occupiedBy->name == validC[i].country->occupiedBy->name){
validC[i].adjacent.push_back(validC[i].country->getAdjacent()[j]->name);
}
}
}
bool good;
int pos;
do{
cout << "Which country would you like to take armies from? (1 - " << to_string(user->GetCountries().size()) << ")";
getline(cin, country1);
good = false;
if (isNumber(country1)){
for (size_t i = 0; i < validC.size(); i++){
if (validC[i].country->name == user->GetCountries()[stoi(country1) - 1]->name){
good = true;
pos = i;
break;
}
}
}
} while (!good || user->GetCountries()[stoi(country1)-1]->numberOfPieces < 2 || stoi(country1) > user->GetCountries().size());
Country* reciever;
do{
cout << "Which country would you like to move the armies to? (Type the name of the country) ";
getline(cin, country2);
country2 = toupperCase(country2);
reciever = user->GetCountries()[stoi(country1) - 1]->findAdjacent(country2);
} while (reciever == nullptr || !validC[pos].findAdj(reciever->name));
Country* start = user->GetCountries()[stoi(country1) - 1];
Country* end = reciever;
do
{
armies = output.OutIn("How many armies? (Max " + to_string(user->GetCountries()[stoi(country1)-1]->numberOfPieces - 1)+")", armies);
} while (!((armies > 0 || (armies < (*user).GetCountries()[stoi(country1) - 1]->getNumberOfPieces())) && (((*user).GetCountries()[stoi(country1) - 1]->getNumberOfPieces()) - armies) != 0));
start->numberOfPieces -= armies;
end->numberOfPieces += armies;
clearScreen();
output.fortificationPlayerStats(*user);
answer = output.OutIn("Would you like to move more armies?", answer);
}
}
mainMenu();
}
void gameDriver::fortification(AI* comp){
clearScreen();
cout << "This is the fortification phase for computer " << comp->getID() << endl;
cout << "---------------------------------------------------------\n\n";
output.PlayerStats(*comp);
(*comp).strat->fortification(comp);
output.PlayerStats(*comp);
mainMenu();
}
void gameDriver::mainMenu()
{
int response;
cout << "Main Menu\n"
<< "-----------------------------------------------------\n"
<< "0: Exit game\t"
<< "1: Save gamestate\t"
<< "2: Continue\n"
<< "-----------------------------------------------------\n";
cin >> response;
//getline(cin, response);
if (response == 0)
exit(0);
else if (response == 1){
string save;
clearScreen();
cout << "What would you like to save it as?\n";
cin.ignore();
getline(cin, save);
saveGame(save);
exit(0);
}
}
void gameDriver::clearScreen(){
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void gameDriver::saveGame(string save){
ofstream outStream;
outStream.open("saves/"+save, ios_base::trunc);
outStream << "map=" << save << ".map"<< endl
<< "numberOfPlayers=" << numberOfPlayers << endl
<< "whosTurn=" << whosTurn << endl
<< "phaseNum=" << phaseNum << endl
<< "computers=";
for (int i = 0; i < 6 - numberOfPlayers; i++)
outStream << computers[i]->stratType << ",";
outStream.close();
myMap.saveMap("saves/"+save + ".map");
}
void gameDriver::loadGame(string load){
ifstream inStream;
inStream.open("saves/" + load);
if (inStream.is_open())
{
string line;
string thisMap;
int numberOfPlayers;
int whosTurn;
int phaseNum;
AI** comps;
getline(inStream, line);
thisMap = line.substr(line.find('=') + 1);
getline(inStream, line);
numberOfPlayers = stoi(line.substr(line.find('=') + 1));
comps = new AI*[6 - numberOfPlayers];
getline(inStream, line);
whosTurn = stoi(line.substr(line.find('=') + 1));
getline(inStream, line);
phaseNum = stoi(line.substr(line.find('=') + 1));
getline(inStream, line);
line = line.substr(line.find('=') + 1);
for (int i = 0; i < 6-numberOfPlayers; i++){
comps[i] = new AI(i+1, line.substr(0, line.find(',')));
line = line.substr(line.find(',') + 1);
}
inStream.close();
Player** players = new Player*[numberOfPlayers];
for (int i = 0; i < numberOfPlayers; i++)
{
players[i] = new Player(i + 1, "player " + to_string(i + 1));
}
map* newMap = new map();
newMap->loadMap("saves/" + thisMap);
for (int i = 0; i < newMap->getCountrySize(); i++)
{
if (!newMap->countries[i].occupiedBy->isComputer){
if (players[newMap->countries[i].occupiedBy->playerID - 1]->GetCountries().size() == 0)
players[newMap->countries[i].occupiedBy->playerID - 1] = newMap->countries[i].occupiedBy;
}
else
{
if (comps[newMap->countries[i].occupiedBy->playerID - 11]->GetCountries().size() == 0){
for (int j = 0; j < newMap->countries[i].occupiedBy->GetCountries().size(); j++)
{
comps[newMap->countries[i].occupiedBy->playerID - 11]->AddCountry(&newMap->countries[i]);
}
}
}
}
*this = gameDriver::Builder().setMap(newMap).setNumberOfPlayers(numberOfPlayers).setWhosTurn(whosTurn).setPhaseNum(phaseNum).setComputers(comps).setPlayers(players).build();
}
else
{
clearScreen();
cout << "Cannot open gamesave!\n";
inStream.close();
system("pause");
startMenu();
}
}
void gameDriver::updatePlayerContinents()
{
for (int i = 0; i < numberOfPlayers; i++){
for (int j = 0; j < myMap.getNumOfContinents(); j++){
int k;
for (k = 0; k < myMap.continents[j].getNumCountries(); k++){
if (!players[i]->hasCountry(myMap.continents[j].countryArray[k]->getName()))
break;
}
if (k == myMap.continents[j].getNumCountries() - 1){
players[i]->AddContinent(&myMap.continents[j]);
}
}
}
}
gameDriver::Builder::Builder(): numberOfPlayers(defaultNumberOfPlayers), whosTurn(defaultWhosTurn), phaseNum(defaultPhaseNum)
{
if (computers == nullptr){
for (int i = 0; i < 6 - numberOfPlayers; i++)
{
computers[i] = new AI(i + 1); //
}
}
if (players == nullptr)
{
players = new Player*[numberOfPlayers];
for (int i = 0; i < numberOfPlayers; i++)
{
players[i] = new Player(i + 1, "player " + (i + 1)); //TODO: Required player parameters
}
}
}
gameDriver::Builder& gameDriver::Builder::setComputers(AI** computers){
this->computers = computers;
return *this;
}
gameDriver::Builder& gameDriver::Builder::setPlayers(Player** players){
this->players = players;
return *this;
}
gameDriver::Builder& gameDriver::Builder::setMap(map* file){
this->builderMap = file;
return *this;
}
gameDriver::Builder& gameDriver::Builder::setNumberOfPlayers(int numOfPlayers){
this->numberOfPlayers = numOfPlayers;
return *this;
}
gameDriver::Builder& gameDriver::Builder::setPhaseNum(int phaseNum){
this->phaseNum = phaseNum;
return *this;
}
gameDriver::Builder& gameDriver::Builder::setWhosTurn(int whosTurn){
this->whosTurn = whosTurn;
return *this;
}
gameDriver gameDriver::Builder::build(){
return gameDriver(builderMap, players, computers, numberOfPlayers, whosTurn, phaseNum);
}
int gameDriver::getCardUnits(){
int tmp = cardReturns;
if (cardSetNum < 5){
cardReturns += 2;
cardSetNum++;
}
else if (cardSetNum == 5){
cardReturns += 3;
cardSetNum++;
}
else{
cardReturns += 5;
cardSetNum++;
}
return tmp;
}
bool gameDriver::isNumber(string s){
string::const_iterator it = s.begin();
while (it != s.end() && isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
string gameDriver::toupperCase(string input)
{
for (size_t i = 0; i < input.length(); i++)
input[i] = toupper(input[i]);
return input;
}