-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMap.cpp
576 lines (449 loc) · 16.6 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
#include "Map.h"
#include <iostream>
using namespace std;
//Authors: Daniel & Nico
/*---------------- Map class ----------------*/
Map::Map(int nbTeritories, int nbContinents){
this->nbTeritories = new int(nbTeritories);
this->nbContinents = new int(nbContinents);
this->countries = new Territory[nbTeritories];
//Making the adjacencyMtrix 2d array
this->adjacencyMatrix = new Territory**[nbTeritories];
for(int i = 0; i < nbTeritories; i++){
adjacencyMatrix[i] = new Territory*[nbTeritories];
for(int s = 0; s<nbTeritories; s++){
adjacencyMatrix[i][s] = nullptr;
}
}
}
//Default constructor
Map::Map(){
this->nbTeritories = NULL;
this->nbContinents = NULL;
this->adjacencyMatrix = NULL;
}
//Copy constructor
Map::Map(const Map& og){
this->nbTeritories = new int(*og.nbTeritories);
this->nbContinents = new int(*og.nbContinents);
this->adjacencyMatrix = new Territory**[*nbTeritories];
this->countries = new Territory[*nbTeritories];
//Creating new adjacency matrix
for(int i = 0; i < *nbTeritories; i++){
adjacencyMatrix[i] = new Territory*[*nbTeritories];
//Copying elemnts from old map adjacency matrix to this one
for(int s = 0; s < *nbTeritories; s++){
adjacencyMatrix[i][s] = og.adjacencyMatrix[i][s];
}
}
//Copying the territories array
for(int s = 0; s < *nbTeritories; s++){
countries[s] = og.countries[s];
}
}
//Destructor
Map::~Map(){
for(int i = 0; i < *nbTeritories; i++){
for(int s = 0; s < *nbTeritories; s++){
//We are not deleting the items at adjacencyMatrix[i][s] as those
//are shared territory objects that can cause issues as we can attemp to delet them again later on
//So we simply remove the pointer reference.
adjacencyMatrix[i][s] = NULL;
}
delete [] adjacencyMatrix[i];
adjacencyMatrix[i] = NULL;
}
delete this->nbContinents;
this->nbContinents = NULL;
delete this->nbTeritories;
this->nbTeritories = NULL;
delete [] this->countries;
this->countries = NULL;
delete [] this->adjacencyMatrix;
this->adjacencyMatrix = NULL;
}
//Adds edge between two nodes
void Map::addEdge(int x, int y, Territory* tx, Territory* ty){
if(this->adjacencyMatrix[x][y] != nullptr){
this->adjacencyMatrix[x][y] = nullptr;
}
if(this->adjacencyMatrix[y][x] != nullptr){
this->adjacencyMatrix[y][x] = nullptr;
}
this->adjacencyMatrix[x][y] = ty;
this->adjacencyMatrix[y][x] = tx;
}
//Helper method to traverse the graph (DFS)
void Map::dfs(int node, bool* visited, bool* visitedCon, int& ct, int& cc){
visited[node] = true;
ct++;
//Counting number of continents - for continent connectivity
if(!visitedCon[*this->countries[node].getContinentId()]){
visitedCon[*this->countries[node].getContinentId()] = true;
cc++;
}
for(int i = 0; i < *this->nbTeritories; i++){
if(this->adjacencyMatrix[node][i] != nullptr && !visited[i]){
dfs(i, visited, visitedCon, ct, cc);
}
}
}
//Validate validates if map object is a valid map
//Note: the method checks for graph and continent connectivity in the same DFS sweep.
bool Map::validate(){
bool* visits = new bool[*nbTeritories];
bool* continentVisits = new bool[*nbContinents];
bool isUnique = true;
unordered_map<string, int> hashmap;
//Assuring that visited array is created properly - that is the array has only false values at the start
for (int i = 0; i < *this->nbTeritories; i++){
visits[i] = false;
}
//Assuring that continentVisits array is created properly - that is the array has only false values at the start
for (int i = 0; i < *this->nbContinents; i++){
continentVisits[i] = false;
}
//Teritory counter
int counterT = 0;
//Continent counter
int counterC = 0;
dfs(0, visits, continentVisits, counterT, counterC);
//Deleting visits array; we only need the array for dfs/ validation
delete visits;
delete continentVisits;
//Only way we can have a country that has 2 continents, is to have a duplicate country, in which case it is an invalid map
for(int i = 0; i < *this->nbTeritories; i++){
if((++hashmap[*this->countries[i].getTerritoryName()]) > 1){
isUnique = false;
cout << "\nRejecting Map - country in 2 continents" << endl;
break;
}
}
//Testing Graph conectivity (if all territories are connected)
if(counterT == *this->nbTeritories && counterC == *this->nbContinents && isUnique){
return true;
}else{
if(counterT == *this->nbTeritories){
cout << "\nCountries not conected - rejecting map" << endl;
}
if(counterC == *this->nbContinents){
cout << "\nContinents not connected - rejecting map" << endl;
}
return false;
}
}
//Setter
void Map::setCountries(Territory arr[]){
for(int i = 0; i < *nbTeritories; i++){
this->countries[i] = arr[i];
}
this->countries = arr;
}
int* Map::getNbTerritories(){
return this->nbTeritories;
}
Territory* Map::getCountries(){
return this->countries;
}
Territory*** Map::getAdjacencyMatrix(){
return this->adjacencyMatrix;
}
//Operator overloading for a map
Map& Map::operator=(const Map& og){
this->nbTeritories = new int(*og.nbTeritories);
this->nbContinents = new int(*og.nbContinents);
this->adjacencyMatrix = new Territory**[*og.nbTeritories];
//Creating new adjacency matrix
for(int i = 0; i < *nbTeritories; i++){
this->adjacencyMatrix[i] = new Territory*[*nbTeritories];
//Copying elemnts from old map adjacency matrix to this one
for(int s = 0; s < *nbTeritories; s++){
adjacencyMatrix[i][s] = og.adjacencyMatrix[i][s];
}
}
return *this;
}
//Printing a map
std::ostream& operator<<(std::ostream &strm, const Map &m){
string res = "";
for (int i = 0; i < *m.nbTeritories; i++)
{
res += ("The country is " + *m.countries[i].getTerritoryName() + " we need " + to_string(*m.countries[i].getAmntToInvade()) + " soldiers to invade - The neighbhors are : ");
for (int s = 0; s < *m.nbTeritories; s++)
{
if (s == i)
{
continue;
}
if (m.adjacencyMatrix[i][s] == nullptr)
{
continue;
}
res += ("\n" + *m.countries[s].getTerritoryName());
}
res += "\n\n";
}
return strm << res << endl;
}
/*---------------- Map Loader class ----------------*/
//Map Loader contructor that loads the map
MapLoader::MapLoader(string fileName){
this->nbTeritories = new int(countEntities(fileName, "[countries]"));
this->nbContinents = new int(countEntities(fileName, "[continents]"));
if(*this->nbTeritories <= 0 || *this->nbContinents <=0){
cout << "Map is in an invalid format, rejecting map" << endl;
}else{
//Creating empty continent array
string continentsArr[*this->nbContinents];
this->continents = continentsArr;
this->contInvade = new int[*nbContinents];
//Creating empty teritorry array and empty map
this->countries = new Territory[*this->nbTeritories];
this->map = new Map(*nbTeritories, *nbContinents);
readContinents(fileName);
readCountries(fileName);
this->map->setCountries(this->countries);
readBorders(fileName);
if(!this->map->validate()){
cout << "Map is invalid; failed validation() method - rejecting map" << endl;
}else{
cout << "Map is valid!" << endl;
}
}
}
//Map Loader destructor
MapLoader::~MapLoader(){
delete this->nbContinents;
this->nbContinents = NULL;
delete this->nbTeritories;
this->nbTeritories = NULL;
delete this->map;
this->map = NULL;
}
//Helper method to count a certain type of entities in the .map file
int MapLoader::countEntities(string fileName, string enityType){
ifstream mapFile(fileName);
int counter = 0;
string text;
//Counting nb of entities
while(getline(mapFile, text)){
if(text.compare(enityType) == 0){
while(text.compare("") != 0){
counter++;
getline(mapFile, text);
}
}
}
mapFile.close();
return (counter - 1);
}
//Method to read the countries in the .map file
void MapLoader::readCountries(string fileName){
ifstream file(fileName);
string text;
int counter = 0;
//Reading countries only
while (getline(file, text)){
if(text.compare("[countries]") == 0){
while(text.compare("") != 0 || text.compare("[borders]") != 0){
getline(file, text);
//Array to store the territory properties to be parsed from the file
//We may need to modify it later on if we will use the other .map info
string propArr[3];
//Spliting the string by tokens to create teritory object
for(int i = 0, f = 0; i < text.length() && f < 3; i++, f++){
propArr[f] = splitString(text, i);
}
//We do -1 since the values read start from 1, and we want
//the ID to corespond to the position in the countries array.
if(counter < *this->nbTeritories){
this->countries[counter].setTerritoryId(stoi(propArr[0]) - 1);
this->countries[counter].setName(propArr[1]);
this->countries[counter].setContinentId(stoi(propArr[2]) - 1);
this->countries[counter].setAmntToInvade(this->contInvade[stoi(propArr[2]) - 1]);
counter++;
}else{
break;
}
}
file.close();
break;
}
}
}
//Method to read the borders between countries and hence formulate the map (adding edges)
void MapLoader::readBorders(string fileName){
ifstream file(fileName);
string text;
int currCountry; //Current country to which we are attributing the borders.
bool firstToken = true;
//Reading countries only
while (getline(file, text)){
if(text.compare("[borders]") == 0){
while(text.compare("") != 0){
getline(file, text);
for(int i = 0; i < text.length(); i++){
string token = splitString(text, i);
if(firstToken){
firstToken = false;
currCountry = (stoi(token) - 1);
}else{
//Creating edge (border) between the current country we are possesing and its neighbhor.
int index = (stoi(token) - 1);
this->map->addEdge(currCountry, index, &this->countries[currCountry], &this->countries[index]);
}
}
firstToken = true;
}
file.close();
break;
}
}
}
//Reading the continents in the .map file
void MapLoader::readContinents(string fileName){
ifstream file(fileName);
string text;
int counter = 0;
while (getline(file, text)){
if(text.compare("[continents]") == 0){
while(text.compare("") != 0 || text.compare("[countries]") != 0){
getline(file, text);
//Array to store the continent properties to be parsed from the file
//We may need to modify it later on if we will use the other .map info (color in this case)
string propArr[2];
//Spliting the string by tokens to create teritory object
for(int i = 0, f = 0; i < text.length() && f < 2; i++, f++){
propArr[f] = splitString(text, i);
}
if(counter < *this->nbContinents){
this->continents[counter] = propArr[0];
this->contInvade[counter] = stoi(propArr[1]);
counter++;
}else{
break;
}
}
file.close();
break;
}
}
}
//Helper method to split the string by tokens
string MapLoader::splitString(string text, int &i){
string token = "";
while (text[i] != ' ' && i < text.length()){
token += text[i];
i++;
}
return token;
}
Map* MapLoader::getMap(){
return this->map;
}
/*---------------- Territory Class ----------------*/
//Constructor
Territory::Territory(int posessor, string TerritoryName, int TerritoryId, bool isFree, int continentId, int amntToInvade, int numberOfSoldiers){
this->posessor = new int(posessor);
this->TerritoryName = new string(TerritoryName);
this->TerritoryId = new int(TerritoryId);
this->isFree = new bool(isFree);
this->continentId = new int(continentId);
this->amntToInvade = new int(amntToInvade);
this->numberOfSoldiers = new int(numberOfSoldiers);
}
//Default Consructor
Territory::Territory(){
this->posessor = new int(-1);
this->TerritoryName = new string("");
this->TerritoryId = new int(-1);
this->isFree = new bool(true);
this->continentId = new int(-1);
this->amntToInvade = new int(-1);
this->numberOfSoldiers = new int(-1);
}
//Copy Cinstructor
Territory::Territory(const Territory& og){
this->posessor = new int(*og.posessor);
this->TerritoryName = new string(*og.TerritoryName);
this->TerritoryId = new int(*og.TerritoryId);
this->isFree = new bool(*og.isFree);
this->continentId = new int(*og.continentId);
this->amntToInvade = new int(*og.amntToInvade);
this->numberOfSoldiers = new int(*og.numberOfSoldiers);
}
//Destructor
Territory::~Territory(){
delete this->posessor;
this->posessor = NULL;
delete this->TerritoryName;
this->TerritoryName = NULL;
delete this->TerritoryId;
this->TerritoryId = NULL;
delete this->isFree;
this->isFree = NULL;
delete this->continentId;
this->continentId = NULL;
delete this->amntToInvade;
this->amntToInvade = NULL;
delete this->numberOfSoldiers;
this->numberOfSoldiers = NULL;
}
Territory& Territory::operator=(const Territory& t) {
this->setContinentId(*t.continentId);
this->setName(*t.TerritoryName);
this->setPosessor(*t.posessor);
this->setStatus(*t.isFree);
this->setTerritoryId(*t.TerritoryId);
this->setNumberOfSoldiers(*t.numberOfSoldiers);
this->setAmntToInvade(*t.amntToInvade);
return *this;
}
std::ostream& operator<<(std::ostream &strm, const Territory &t){
return strm << "The territorie name: " << *t.TerritoryName << " , is occupied: " << *t.isFree << ", amnt of soldiers: " << *t.numberOfSoldiers
<< ", amount to invade: " << *t.amntToInvade << ", id of player possesing it: " << *t.posessor << endl;
}
//Getters
int* Territory:: getPosessor(){
return this->posessor;
}
int* Territory::getTerritoryId(){
return this->TerritoryId;
}
int* Territory::getContinentId(){
return this->continentId;
}
bool* Territory::getIsFree(){
return this->isFree;
}
string* Territory::getTerritoryName(){
return this->TerritoryName;
}
int* Territory::getAmntToInvade(){
return this->amntToInvade;
}
int* Territory::getNumberOfSoldiers(){
return this->numberOfSoldiers;
}
//Setters
void Territory::setPosessor(int id){
*this->posessor = id;
}
void Territory::setTerritoryId(int id){
*this->TerritoryId = id;
}
void Territory::setStatus(bool stat){
*this->isFree = stat;
}
void Territory::setName(string name){
*this->TerritoryName = name;
}
void Territory::setContinentId(int id){
*this->continentId = id;
}
void Territory::setAmntToInvade(int amnt){
*this->amntToInvade = amnt;
}
void Territory::setNumberOfSoldiers(int amnt){
*this->numberOfSoldiers = amnt;
}