-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.cpp
833 lines (756 loc) · 20.4 KB
/
map.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
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
#include "stdafx.h"
#include <iostream>
#include <unordered_map>
#include<string>
#include "map.h"
#include <cstdlib>
#include "GameObservers.h"
using namespace std;
//Continent implementation
Continent::Continent(string name) {
continentName = name;
unordered_map<string, Country*> subCountry;
}
Continent::~Continent() {
subCountry.clear();
}
// add country to the continent object only if the country is not already added
void Continent::addSubCountry(string name,Country* country) {
if (!isSubCountryExist(name)) {
subCountry[name] = country;
cout << "" << endl;
cout << "Perfect Captain! , We added (" + name + ") in the continent ("+ country->getContinentName() + ")" << endl;
}
else {
cout << "" << endl;
cout << "Captain! , This Country (" + name + ") is already in (" +country->getContinentName() + ")" << endl;
}
}
bool Continent::isSubCountryExist(string name) {
return !(subCountry.find(name) == subCountry.end());
}
void Continent::displayInfo() {
cout << "" << endl;
cout << "" << endl;
cout << "@@[" + continentName + "]" << endl;
for (std::pair<std::string, Country*> element : subCountry) {
element.second->displayInfo();
}
}
string Continent::getName() {
return continentName;
}
unordered_map<string, Country*> Continent::getSubCountry() {
return subCountry;
}
//check if it exist a path that connect all country in the continent
bool Continent::recursiveCheckTree(unordered_map<string, int> arrOfEdgeCountry, queue<Country *> queueOfCountry) {
arrOfEdgeCountry[queueOfCountry.front()->getCountryName()] = 1;
for (std::pair<std::string, Country*> element : queueOfCountry.front()->getEdgeCountry()) {
if (isSubCountryExist(element.first) && arrOfEdgeCountry[element.first] == 0) {
queueOfCountry.push(element.second);
}
}
queueOfCountry.pop();
if (queueOfCountry.size() == 0) {
bool allchecked = true;
for (std::pair<std::string, int> element2 : arrOfEdgeCountry) {
if (element2.second == 0) {
allchecked = false;
break;
}
}
return allchecked;
}
else {
return recursiveCheckTree(arrOfEdgeCountry, queueOfCountry);
}
}
bool Continent::isConnectedContinent() {
if (subCountry.size() < 2) {
cout << "" << endl;
cout << "Captain!, the continent (" + continentName + ") is a connected Continent graph!" << endl;
return 1;
}
else {
unordered_map<string, int> subCountryToCompare;
for (std::pair<std::string, Country*> element : subCountry) {
subCountryToCompare[element.first] = 0;
}
queue<Country *> queueOfCountry;
for (std::pair<std::string, Country*> element2 : subCountry) {
queueOfCountry.push(element2.second);
break;
}
if (recursiveCheckTree(subCountryToCompare, queueOfCountry)) {
cout << "" << endl;
cout << "Captain!, the continent (" + continentName + ") is a connected Continent graph!" << endl;
return 1;
}
cout << "" << endl;
cout << "Captain!, the continent (" + continentName + ") is a NOT A connected Continent graph!" << endl;
return 0;
}
}
string Continent::setOwnedColor() {
int red = 0;
int blue = 0;
int yellow = 0;
int green = 0;
int white = 0;
int colorArr[] = { red,blue,yellow,green ,white };
int domColorn = 0;
string domColor = "NONE";
unordered_map<string, Country*> ownedCountries;
// initialize the array of countries being owned (having armies on it with no repetions)
for (std::pair<std::string, Country*> element : subCountry)
{
bool check = false;
for (std::pair<std::string, Country*> element2 : ownedCountries)
{
if (element.first == element2.first)
{
check = true;
}
}
if (check)
{
}
else
{
if (element.second->hasOwner()) {
ownedCountries[element.first] = element.second;
}
}
}
// Sums up the number of countries owned for each Player color
for (std::pair<std::string, Country*> element : ownedCountries)
{
string owner = element.second->setOwnedColor();
if (element.second->getOwnerColor() == "red")
{
colorArr[0]++;
}
else if (element.second->getOwnerColor() == "blue")
{
colorArr[1]++;
}
else if (element.second->getOwnerColor() == "yellow")
{
colorArr[2]++;
}
else if (element.second->getOwnerColor() == "green")
{
colorArr[3]++;
}
else if (element.second->getOwnerColor() == "white")
{
colorArr[4]++;
}
}
// Sets the dominant Player color based on the above calculation
for (int i = 0; i < sizeof(colorArr); i++)
{
if (colorArr[i] > domColorn)
{
domColorn = colorArr[i];
switch (i)
{
case 0:
domColor = "red";
break;
case 1:
domColor = "blue";
break;
case 2:
domColor = "yellow";
break;
case 3:
domColor = "green";
break;
case 4:
domColor = "white";
break;
}
}
}
return domColor;
}
int Continent::computeScoreC(string colorP)
{
string ownedcolor = setOwnedColor();
if (colorP == ownedcolor) {
return 1;
}
else {
return 0;
}
}
//Country implementation
Country::Country(string name,View* observer) {
countryName = name;
continentName = "";
unordered_map<string, Country*> edgeCountry;
armies["red"] = new int(0);
armies["blue"] = new int(0);
armies["green"] = new int(0);
armies["yellow"] = new int(0);
armies["white"] = new int(0);
cities["red"] = new int(0);
cities["blue"] = new int(0);
cities["green"] = new int(0);
cities["yellow"] = new int(0);
cities["white"] = new int(0);
this->observer = observer;
}
Country::~Country() {
armies.clear();
cities.clear();
}
// add country to the edge of the Country object if this country is not already an edge country
void Country:: addEdgeCountry(string name,Country* country) {
if (!isEdgeCountryExist(name)) {
edgeCountry[name] = country;
cout << "" << endl;
cout << "Perfect Captain! , We connected ("+name+") to ("+ getCountryName()+")" << endl;
}
else {
cout << "" << endl;
cout << "Captain! , This Country ("+name+") is already connected to ("+ getCountryName()+")" << endl;
}
}
unordered_map<string, Country*> Country::getEdgeCountry() {
return edgeCountry;
}
void Country::setContinentName(string name) {
continentName = name;
}
bool Country::hasOwner() {
for (std::pair<std::string, int*> element : armies) {
if (*element.second > 0) {
return 1;
}
}
return 0;
}
string Country::setOwnedColor() {
string colorDom = "";
int armyDom = 0;
for (std::pair<std::string, int*> element : armies) {
if (*element.second > armyDom)
{
armyDom = *element.second;
colorDom = element.first;
}
}
ownerColor = colorDom;
return colorDom;
}
void Country::addArmies(string color, int nbOfArmies) {
*armies[color] += nbOfArmies;
observer->notify(getCountryName(), setOwnedColor());
}
void Country::addCities(string color, int nbOfCities) {
*cities[color] += nbOfCities;
}
void Country::DestroyArmies(string color) {
*armies[color] -= 1;
observer->notify(getCountryName(), setOwnedColor());
}
void Country::displayInfo() {
cout << "" << endl;
cout << "@["+countryName+"]" << endl;
cout << "[Continent]" << endl;
cout << "-" + getContinentName() << endl;
if (edgeCountry.size() > 0) {
cout << "[Edge Countries]" << endl;
for (std::pair<std::string, Country*> element : edgeCountry) {
cout << "-" + element.first << endl;;
}
}
cout << "[Armies]" << endl;
for (std::pair<std::string, int*> element : armies) {
cout << "-" + element.first << " :: "<<*element.second<< endl;;
}
cout << "[Cities]" << endl;
for (std::pair<std::string, int*> element : cities) {
cout << "-" + element.first << " :: " << *element.second << endl;;
}
}
void Country::setOwner(string player) {
ownerColor = player;
}
bool Country::isEdgeCountryExist(string name) {
return !(edgeCountry.find(name) == edgeCountry.end());
}
string Country::getCountryName() {
return countryName;
}
string Country::getContinentName() {
return continentName;
}
int Country::getEdgeCountrySize() {
return edgeCountry.size();
}
//sets the owner of the country (most armies) and test wheter the Player owns it or not
int Country::computeScoreR(string colorP)
{
string owner =setOwnedColor();
return (ownerColor == colorP);
}
string Country::getOwnerColor()
{
return ownerColor;
}
Map* Map::m_instance = NULL;
Map* Map::getInstance()
{
if (m_instance == NULL) {
m_instance = new Map();
}
else {
cout << " There is already an map in this game ! " << endl;
return m_instance;
}
return(m_instance);
}
void Map::releaseInstance()
{
if (m_instance != NULL) {
delete Map::m_instance;
m_instance = NULL;
}
}
//Map implementation
Map::Map() {
unordered_map<string, Country*> allCountry;
unordered_map<string, Continent*> allContinent;
}
Map::~Map() {
for (auto it : allCountry) {
it.second->~Country();
}
for (auto it : allContinent) {
it.second->~Continent();
}
allContinent.clear();
allCountry.clear();
}
void Map::displayAllCountry() {
cout << endl;
cout << "This is all Country" << endl;
for (std::pair<std::string, Country*> element : allCountry) {
cout << "- " << element.first << endl;
}
}
//create continent if does not already exist
bool Map::createContinent(string continentName) {
if (!isContinentExist(continentName)) {
Continent* continent = new Continent(continentName);
allContinent[continentName] = continent;
cout << "" << endl;
cout << "Captain ! , This continent ("+continentName+") is now created!" << endl;
return 1;
}
else {
cout << "" << endl;
cout << "Captain ! , This continent ("+continentName+") already exist!" << endl;
return 0;
}
}
string Map::getRandomCountry() {
int randomNumber = rand() % allCountry.size();
int counter = 0;
for (std::pair<std::string, Country*> element : allCountry) {
if (counter == randomNumber) {
return element.first;
}
counter++;
}
}
bool Map::displayContinentInfo(string ContinentName) {
if (isContinentExist(ContinentName)) {
allContinent[ContinentName]->displayInfo();
return 1;
}
else {
cout << "" << endl;
cout << "Captain!, this continent does not exist" << endl;
return 0;
}
}
Country* Map::getCountryByName(string countryName) {
return allCountry[countryName];
}
bool Map::displayCountryInfo(string countryName) {
if (isCountryExist(countryName)) {
allCountry[countryName]->displayInfo();
return 1;
}
else {
cout << "" << endl;
cout << "Captain!, this country does not exist" << endl;
return 0;
}
}
void Map::addArmyToCountry(string countryName, string color, int nbOfArmy) {
if (isCountryExist(countryName)) {
allCountry[countryName]->addArmies(color, nbOfArmy);
cout << endl;
cout << nbOfArmy << " " << color << " (army/armies) have been added to " << countryName << endl;
}
}
void Map::addCityToCountry(string countryName, string color, int nbOfCity) {
if (isCountryExist(countryName)) {
allCountry[countryName]->addCities(color, nbOfCity);
cout << endl;
cout << nbOfCity << " " << color << " (city/cities) have been added to " << countryName << endl;
}
}
// connect a country to a continent (Every Country needs a Continent)
bool Map::connectToContinent(string continentName,string countryName){
if (isContinentExist(continentName)) {
if (allCountry[countryName]->getContinentName() == "") {
allCountry[countryName]->setContinentName(continentName);
allContinent[continentName]->addSubCountry(countryName, allCountry[countryName]);
return 1;
}
else {
return 0;
}
}
else {
return 0;
}
}
void Map::deleteMap() {
for (std::pair<std::string, Continent*> element : allContinent) {
delete element.second;
element.second = NULL;
}
for (std::pair<std::string, Country*> element : allCountry) {
delete element.second;
element.second = NULL;
}
}
bool Map::createCountry(string countryName) {
if (!isCountryExist(countryName)) {
Country* country = new Country(countryName,observer);
allCountry[countryName] = country;
cout << "" << endl;
cout << "Captain ! , This Country (" + countryName + ") is now created!" << endl;
return 1;
}
else {
cout << "" << endl;
cout << "Captain ! , This Country (" + countryName + ") ALREADY EXIST!" << endl;
return 0;
}
}
void Map::showMap() {
for (std::pair<std::string, Continent*> element : allContinent) {
element.second->displayInfo();
}
}
//Connect a country to another country, this method allow the AddEdgecountry to be use
bool Map::connectToCountry(string countryFrom,string countryTo) {
if (!allCountry[countryFrom]->isEdgeCountryExist(countryTo)) {
allCountry[countryFrom]->addEdgeCountry(countryTo,allCountry[countryTo]);
return 1;
}
else {
cout << "" << endl;
cout << "Captain! , ("+countryFrom+") and ("+countryTo+") are already connected!";
return 0;
}
}
Continent* Map::getContinent(string name) {
return allContinent[name];
}
Country* Map::getCountry(string name) {
return allCountry[name];
}
bool Map::isCountryExist(string name) {
return !(allCountry.find(name) == allCountry.end());
}
bool Map::isContinentExist(string name) {
return !(allContinent.find(name) == allContinent.end());
}
void Map::startGame() {
cout << "" << endl;
cout << "The GAME CAN START" << endl;
}
bool Map::isConnectedMap() {
bool checked = true;
for (std::pair<std::string, Continent*> element : allContinent) {
if (element.second->isConnectedContinent() == 0) {
checked = false;
break;
}
}
if (!checked) {
cout << "" << endl;
cout << "Captain! This is not a Connected Graph MAP" << endl;
return 0;
}
else {
unordered_map<string, int> subCountryToCompare;
for (std::pair<std::string, Country*> element : allCountry) {
subCountryToCompare[element.first] = 0;
}
queue<Country *> queueOfCountry;
for (std::pair<std::string, Country*> element2 : allCountry) {
queueOfCountry.push(element2.second);
break;
}
if (recursiveCheckTree(subCountryToCompare, queueOfCountry)) {
cout << "" << endl;
cout << "Captain!, we have a full Connected Graph Map!" << endl;
return 1;
}
else {
cout << "" << endl;
cout << "Captain!, This is NOT A Connected Graph Map!" << endl;
return 0;
}
}
}
bool Map::recursiveCheckTree(unordered_map<string, int> arrOfCountry, queue<Country *> queueOfCountry) {
arrOfCountry[queueOfCountry.front()->getCountryName()] = 1;
for (std::pair<std::string, Country*> element : queueOfCountry.front()->getEdgeCountry()) {
if (arrOfCountry[element.first] == 0) {
queueOfCountry.push(element.second);
}
}
queueOfCountry.pop();
if (queueOfCountry.size() == 0) {
bool allchecked = true;
for (std::pair<std::string, int> element2 : arrOfCountry) {
if (element2.second == 0) {
allchecked = false;
break;
}
}
return allchecked;
}
else {
return recursiveCheckTree(arrOfCountry, queueOfCountry);
}
}
unordered_map<string, Continent*> Map::getAllContinent() {
return allContinent;
}
//Main question to Manually create a Map
void Map::askBuildMapQuestion() {
while (true) {
cout << "" << endl;
cout << "" << endl;
cout << "What do you want to do ?" << endl;
cout << "1) Create Continent" << endl;
cout << "2) Create Country" << endl;
cout << "3) Connect Country" << endl;
cout << "4) Get Continent info" << endl;
cout << "5) Get Country info" << endl;
cout << "6) Show Map" << endl;
cout << "7) Is the map connected?" << endl;
cout << "8) End" << endl;
int value;
cin >> value;
switch (value) {
case 1: {
cout << "" << endl;
cout << "What is the name of the continent?" << endl;
string name;
cin >> name;
createContinent(name);
break;
}
case 2:
cout << "" << endl;
if (allContinent.size() != 0) {
cout << "What is the name of the country?" << endl;
string countryName;
cin >> countryName;
if (createCountry(countryName)) {
while (true) {
cout << "" << endl;
cout << countryName + " is in which continent?" << endl;
for (std::pair<std::string, Continent*> element : allContinent) {
cout << "-" + element.first << endl;
}
string continentName;
cin >> continentName;
if (connectToContinent(continentName, countryName)) {
if (allContinent[continentName]->getSubCountry().size() > 1) {
while (true) {
cout << "" << endl;
cout << countryName + " is connected to which country?" << endl;
for (std::pair<std::string, Country*> element : allContinent[continentName]->getSubCountry()) {
cout << "-" + element.first << endl;
}
string countrySelected;
cin >> countrySelected;
if (countryName == countrySelected) {
cout << "" << endl;
cout << "AHAHHA Captain, I think you drunk to much Tequila, you cannot connect a country to itself!";
continue;
}
else {
if (allContinent[continentName]->isSubCountryExist(countrySelected)) {
if (connectToCountry(countryName, countrySelected) && connectToCountry(countrySelected,countryName)) {
break;
}
}
else {
cout << "" << endl;
cout << "Captain! this country does not exist in this continent!";
}
}
}
break;
}
else {
break;
}
}
else {
continue;
}
}
}
}
else {
cout << "" << endl;
cout << "Captain! , you need to create a continent first!" << endl;
}
break;
case 3:
if (allCountry.size() > 1) {
while (true) {
cout << "" << endl;
cout << "Captain!, which country you want to create connection?" << endl;
for (std::pair<std::string, Country*> element : allCountry) {
cout << "-" + element.first << endl;
}
string countrySelected;
cin >> countrySelected;
if (isCountryExist(countrySelected)) {
cout << "" << endl;
cout << "Captain!, (" + countrySelected + ") is connected with which country?" << endl;
for (std::pair<std::string, Country*> element : allCountry) {
cout << "-" + element.first << endl;
}
string countrySelected2;
cin >> countrySelected2;
if (countrySelected == countrySelected2) {
cout << "" << endl;
cout << "AHAHHA Captain, I think you drunk to much Tequila, you cannot connect a country to itself!" << endl;
continue;
}
else {
if (isCountryExist(countrySelected2)) {
if (connectToCountry(countrySelected, countrySelected2) && connectToCountry(countrySelected2,countrySelected)) {
break;
}
}
}
}
}
}
else {
cout << "" << endl;
cout << "Captain!, you need at least 2 countries in the map to make connection!" << endl;
}
break;
case 4:
if (allContinent.size() > 0) {
while (true) {
cout << "" << endl;
cout << "Captain!, give me a continent and I'll bring you the info" << endl;
for (std::pair<std::string, Continent*> element : allContinent) {
cout << "-" + element.first << endl;
}
string continentSelected;
cin >> continentSelected;
if (displayContinentInfo(continentSelected)) {
break;
}
}
}
else {
cout << "" << endl;
cout << "Captain!, you need to create a continent first!" << endl;
}
break;
case 5:
if (allCountry.size() > 0) {
while (true) {
cout << "" << endl;
cout << "Captain!, give me a country and I'll bring you the info" << endl;
for (std::pair<std::string, Country*> element : allCountry) {
cout << "-" + element.first << endl;
}
string countrySelected;
cin >> countrySelected;
if (displayCountryInfo(countrySelected)) {
break;
}
}
}
else {
cout << "" << endl;
cout << "Captain!, you need to create a country first!" << endl;
}
break;
case 6:
if (allContinent.size() > 0) {
for (std::pair<std::string, Continent*> element : allContinent) {
element.second->displayInfo();
}
}
else {
cout << "" << endl;
cout << "Captain!, you need to create a continent first!" << endl;
}
break;
case 7:
if (allContinent.size() != 0) {
isConnectedMap();
}
else {
cout << "" << endl;
cout << "Captain!, you need to create a continent first!" << endl;
}
break;
case 8:
if (allContinent.size() == 0) {
cout << "" << endl;
cout << "You did not create a continent" << endl;
continue;
}
if (allCountry.size() == 0) {
cout << "" << endl;
cout << "You need to build at least one country" << endl;
continue;
}
if (isConnectedMap()) {
startGame();
}
exit(0);
break;
default:
cout << "" << endl;
cout << "Captain!, enter a valid selection!" << endl;
break;
}
}
}
void Map::setObserverView() {
this->observer = new View();
}
View* Map::getObserverView() {
return this->observer;
}
unordered_map<string,Country*> Map::getAllCountry() {
return allCountry;
}