Repository navigation
Expand file tree
/
Copy pathMap.cpp
More file actions
604 lines (481 loc) · 12.4 KB
/
Copy pathMap.cpp
File metadata and controls
604 lines (481 loc) · 12.4 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
//============================================================================
// Name : Map.cpp
// Author : Arsany Fahmy
// Version :
// Copyright :
// Description : Comp 345 Ass 1
//============================================================================
#include "Map.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
// ----------------------------------------- Continent Class -----------------------------------------//
Continent::Continent()
{
name = "NoName";
id = -1;
color = "NoColor";
}
Continent::Continent(string ContName, int cont_id, string col)
{
name = ContName;
id = cont_id;
color = col;
}
//copy
void Continent::copy(Continent &c)
{
name = c.name;
id = c.id;
color = c.color;
}
//Getters
string Continent::getName()
{
return name;
}
int Continent::getId()
{
return id;
}
string Continent::getColor()
{
return color;
}
//Setters
void Continent::setName(string n)
{
name = n;
}
void Continent::setId(int i )
{
id = i;
}
void Continent::setColor(string c)
{
color = c;
}
// ----------------------------------------- Territory Class -----------------------------------------//
Territory::Territory()
{
territory_id = -1;
territory_name = "null";
continent_id = -1;
numOfArmies = -1;
}
Territory::Territory(int id, string name, int cont_id, int arm)
{
territory_id = id;
territory_name = name;
continent_id = cont_id;
numOfArmies = arm;
}
//copy
void Territory::copy(Territory &t)
{
territory_id = t.territory_id;
territory_name = t.territory_name;
continent_id = t.continent_id;
numOfArmies = t.numOfArmies;
}
//Getters
int Territory::getTerritory_id()
{
return territory_id;
}
string Territory::getTerritory_name()
{
return territory_name;
}
int Territory::getContinent_id()
{
return continent_id;
}
int Territory::getNumOfArmies()
{
return numOfArmies;
}
//Setters
void Territory::setTerritory_id(int id)
{
territory_id = id;
}
void Territory::setTerritory_name(string name)
{
territory_name = name;
}
void Territory::setContinent_id(int id)
{
continent_id = id;
}
void Territory::setNumOfArmies(int arm)
{
numOfArmies = arm;
}
// ----------------------------------------- Map Class -----------------------------------------//
Map::Map(): territories(), continents()
{
mapFileName = "null";
numOfTerritories = -1;
numOfContinents = -1;
}
Map::Map(string fileName, int numTerritories, int numContinents): territories(), continents()
{
mapFileName = fileName;
numOfTerritories = numTerritories;
numOfContinents = numContinents;
}
//copy
void Map::copy(Map &m)
{
mapFileName = m.mapFileName;
numOfTerritories = m.numOfTerritories;
numOfContinents = m.numOfContinents;
territories = m.territories;
continents = m.continents;
adjacentTerritories = m.adjacentTerritories;
territoriesInContinents = m.territoriesInContinents;
}
void Map::addContinent(Continent cont)
{
numOfContinents++;
continents.push_back(cont);
}
void Map::addTerritory(Territory terr)
{
numOfTerritories++;
territories.push_back(terr);
}
void Map::printContinents()
{
std::vector<Continent>::iterator it;
cout << "\nStored Continents: [" << numOfContinents << "]" << endl;
int count = 1;
for (it = continents.begin(); it != continents.end(); ++it)
{
cout << "Continent #" << (count) << ":" << endl;
cout << "Name: " << it->getName() << endl;
cout << "ID: " << it->getId() << endl;
cout << "Color: " << it->getColor() << endl;
cout << "\n";
count++;
}
}
void Map::printTerritories()
{
std::vector<Territory>::iterator it;
cout << "\nStored Territories: [" << numOfTerritories << "]" << endl;
int count = 1;
for (it = territories.begin(); it != territories.end(); ++it)
{
cout << "Territory #" << (count) << ":" << endl;
cout << "ID: " << it->getTerritory_id() << endl;
cout << "Name: " << it->getTerritory_name() << endl;
cout << "Continent_id: " << it->getContinent_id() << endl;
cout << "\n";
count++;
}
}
void Map::initializeAdjacentTerritoriesGraph()
{
adjacentTerritories = new vector<Territory> [numOfTerritories+1];
}
void Map::initializeTerritoriesInContinentsGraph()
{
territoriesInContinents = new vector<Territory> [numOfContinents+1];
}
void Map::addAdjacentTerritoryBorder(int source_territory_id, Territory destination_territory, int destination_position)
{
adjacentTerritories[source_territory_id].push_back(destination_territory);
}
void Map::addTerritoryToContinent(int source_continent_id, Territory terr)
{
territoriesInContinents[source_continent_id].push_back(terr);
}
void Map::printAdjacentTerritoryGraph()
{
cout << "\nGraph of adjacent territories: " << endl;
//Iterate over all vertices
for(int i = 1; i < numOfTerritories+1; i++)
{
cout << "Territory_id " << i << "->";
for(Territory neighbor: adjacentTerritories[i])
{
cout << neighbor.getTerritory_name() << ",";
}
cout << endl;
}
}
vector<Territory> Map::getAdjacentTerritories(Territory t)
{
return adjacentTerritories[t.getTerritory_id()];
}
void Map::printContinentGraph()
{
cout << "\nGraph of continents: " << endl;
//Iterate over all vertices
for(int i = 1; i < numOfContinents+1; i++)
{
cout << "Continent_id " << i << "->";
for(Territory terr: territoriesInContinents[i])
{
cout << terr.getTerritory_name() << ",";
}
cout << endl;
}
}
bool Map::validate()
{
// Validating if 1) the map is a connected graph
//cout << "\nGraph is not connected!" << endl;
// Validating if 2) continents are connected subgraphs
//cout << "\Continents are not connected!" << endl;
// Validating if 3) each country belongs to one and only one continent.
//cout << "\Each country doensn't belong to one and only one continent!" << endl;
cout << "Map is successfully validated!" << endl;
return 1;
}
//Getters
string Map::getFileName()
{
return mapFileName;
}
int Map::getNumOfTerritories()
{
return numOfTerritories;
}
int Map::getNumOfContinents()
{
return numOfContinents;
}
vector<Territory> Map::getTerritories()
{
return territories;
}
vector<Continent> Map::getContinents()
{
return continents;
}
//Setters
void Map::setFileName(string name)
{
mapFileName = name;
}
void Map::setNumOfTerritories(int num)
{
numOfTerritories = num;
}
void Map::setNumOfContinents(int num)
{
numOfContinents = num;
}
void Map::setTerritories(vector<Territory> terr)
{
territories = terr;
}
void Map::setContinents(vector<Continent> cont)
{
continents = cont;
}
// ----------------------------------------- MapLoader Class -----------------------------------------//
MapLoader::MapLoader(): map()
{
mapFileName = "null";
}
MapLoader::MapLoader(string fileName): map()
{
mapFileName = fileName;
}
//copy
void MapLoader::copy(MapLoader &m)
{
map = m.map;
mapFileName = m.mapFileName;
}
Map MapLoader::loadMap()
{
// --------------------- Checking if file exists ---------------------//
ifstream myFile;
myFile.open(mapFileName);
if (myFile.good() == 0)
{
//Returning Failure
cout << "\nMap failed to load! Please make sure that file exists, terminating program..." << endl;
exit(0);
return map;
}
//temp variable to store the context of each line in file:
string temp;
//temp variable to count how many countries and continents;
int count = 0;
//If input file is open and valid:
if (myFile.is_open())
{
//endloop variable to end the while loop when all the countries have been stored
bool endLoop = false;
//While text file still lines and the countries data have not yet been reached
while(getline(myFile, temp) && endLoop == false)
{
// --------------------- Storing territories ---------------------//
//When [continents] headline is met --> *** store continents ***
if (temp.find("[continents]") != std::string::npos)
{
//resetting count
count = 0;
//Storing data of continents
while(getline(myFile, temp) && endLoop == false)
{
//skip one more line if there is a note (line starting with ';')
if(temp.substr(0, 1) == ";")
getline(myFile, temp);
//Stop the loop when no more continents (empty line)
if(temp.size() == 1)
{
endLoop = true;
}
else
{
//counting how many continents
count++;
//Storing the 3 words separated by white spaces from temp into a tokens vector
//Continents format:
//EX: Scandinavia 5 #fff780
// [name] [continent_id] [color]
string buf;
stringstream ss(temp);
vector<string> tokens;
//while there is more tokens
while (ss >> buf)
{
tokens.push_back(buf);
}
//Storing the different tokens (continents info) inside the Map class
Continent continent;
continent.setName(tokens[0]);
continent.setId(stoi(tokens[1]));
continent.setColor(tokens[2]);
map.addContinent(continent);
}
}
//Storing number of continents
map.setNumOfContinents(count);
//To keep scanning the file for next line to fill countries info
endLoop = false;
}
// --------------------- Storing Continents ---------------------//
//When [countries] headline is met --> *** store countries ***
if (temp.find("[countries]") != std::string::npos)
{
//resetting count
count = 0;
//Initializing the continents graph
map.initializeTerritoriesInContinentsGraph();
//Storing data of countries
while(getline(myFile, temp) && endLoop == false)
{
//skip one more line if there is a note (line starting with ';')
if(temp.substr(0, 1) == ";")
getline(myFile, temp);
//Stop the loop when no more countries (empty line)
if(temp.size() == 1)
{
endLoop = true;
}
else
{
//counting how many continents
count++;
//Storing the 5 words separated by white spaces from temp into a tokens vector
//Countries format:
//EX: 1 Reykjavik 1 27 30
// [id] [name] [continent_id] [x_coor] [y_coor]
string buf;
stringstream ss(temp);
vector<string> tokens;
//while there is more tokens
while (ss >> buf)
{
tokens.push_back(buf);
}
//Storing the different tokens (countries info) inside the Map class
Territory territory;
territory.setTerritory_id(stoi(tokens[0]));
territory.setTerritory_name(tokens[1]);
territory.setContinent_id(stoi(tokens[2]));
map.addTerritory(territory);
//storing the continent of territory
int cont_id = stoi(tokens[2]);
map.addTerritoryToContinent(cont_id, territory);
}
}
//Storing number of territories
map.setNumOfTerritories(count);
//To keep scanning the file for next line to fill countries info
endLoop = false;
}
// --------------------- Storing Borders ---------------------//
//When [borders] headline is met --> *** store borders info ***
if (temp.find("[borders]") != std::string::npos)
{
//resetting count
count = 0;
//Initializing the adjacent territories graph
map.initializeAdjacentTerritoriesGraph();
//Storing data of borders
while(getline(myFile, temp) && endLoop == false)
{
//resetting count
count = 0;
//skip one more line if there is a note (line starting with ';')
if(temp.substr(0, 1) == ";")
getline(myFile, temp);
//Stop the loop when no more borders (empty line)
if(temp.size() == 1)
{
endLoop = true;
}
else
{
//Storing the shared borders from each line
//Borders format:
//EX: 10 8 9 11
// [country_id] [...other countries_id that have border]
string buf;
stringstream ss(temp);
vector<string> tokens;
//while there is more tokens
while (ss >> buf)
{
tokens.push_back(buf);
}
//Finding the right matching territory that we are adding a border to
int territory_id = stoi(tokens[0]);
//Storing the different tokens (Borders info) inside the Map class
for(int y = 1; y < tokens.size(); y++)
{
int destination_territory = stoi(tokens[y]) - 1;
map.addAdjacentTerritoryBorder(territory_id, map.getTerritories()[destination_territory], y);
}
}
}
//To keep scanning the file for next line to fill countries info
endLoop = false;
}
}
}
//Returning success
cout << "\nMap successfully loaded!" << endl;
return map;
}
//Getters
string MapLoader::getFileName()
{
return mapFileName;
}
//Setters
void MapLoader::setFileName(string name)
{
mapFileName = name;
}