-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMap.cpp
More file actions
472 lines (393 loc) · 13 KB
/
Map.cpp
File metadata and controls
472 lines (393 loc) · 13 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
//
// Created by Claudia on 2020-10-07.
//
#include "Map.h"
#include "Player.h"
/////////////////////////////////////////////////////////////////////////////
/// MAP ///
/////////////////////////////////////////////////////////////////////////////
Map::Map() = default;
// constructor
Map::Map(string _name, vector<string> mapData){
name = _name;
int contI = 0;
int terrI = 0;
int borderI = 0;
// get indexes that limit the data necessary
for (size_t i = 0; i < mapData.size(); ++i){
if(mapData[i].find("continents") < mapData.size() && mapData[i].find("continents") > 0) {
contI = i;
} else if(mapData[i].find("countries") < mapData.size() && mapData[i].find("countries") > 0) {
terrI = i;
} else if(mapData[i].find("borders") < mapData.size() && mapData[i].find("borders") > 0) {
borderI = i;
}
}
// get all continent strings from map data
vector<string> tempContinents;
for (size_t i = (contI + 1); i < (terrI - 1); ++i){
tempContinents.push_back(mapData[i]);
}
// get all country strings from map data
vector<string> tempTerritories;
for (size_t i = (terrI + 1); i < (borderI - 1); ++i){
tempTerritories.push_back(mapData[i]);
}
// get nodes per country from map data
vector<string> borders;
for (size_t i = (borderI + 1); i < mapData.size(); ++i){
borders.push_back(mapData[i]);
}
setTerritories(tempTerritories, borders);
setContinents(tempContinents);
}
// copy constructor
Map::Map(const Map& _m){
name = _m.name;
territories = _m.territories;
continents = _m.continents;
}
// destructor
Map::~Map() {
for (auto & territorie : territories) {
delete territorie;
}
for (auto & continent : continents) {
delete continent;
}
}
// validate map
bool Map::validate() {
vector<string> sequence;
int idInt = 0;
int nodeInt = 0;
sequence.push_back(territories[0]->id);
for (auto & initialAdjacent : territories[0]->getAdjacentNodes()) {
sequence.push_back(initialAdjacent);
}
// attention! t starts at 1 -> territory[0] is node with id 1
for (int i = 0; i < territories.size(); i++) {
// turn string to int
stringstream geek(territories[i]->id);
geek >> idInt;
// check if enough
if(sequence.size() == territories.size()){
break;
}
// check each adjacent of current node in the sequence
for (auto & node : territories[idInt - 1]->getAdjacentNodes()) {
stringstream geek(node);
geek >> nodeInt;
vector<string> tempAdjacent = territories[nodeInt - 1]->getAdjacentNodes();
// look territory id inside it's adjacent node's adjacentList (must have each other)
if (!(std::count(tempAdjacent.begin(), tempAdjacent.end(), territories[idInt - 1]->id))) {
cout << "Invalid Map: " << name << endl;
return false;
}
// check ft enough
if (sequence.size() == territories.size()) {
break;
}
// if not in the sequence yet add it
if (!(std::find(sequence.begin(), sequence.end(), node) != sequence.end())) {
sequence.push_back(node);
}
}
if(sequence.size() == territories.size()){
cout << "->VALID Map: " << name << endl;
return true;
}
}
cout << "Invalid Map: " << name << endl;
return false;
}
// get territories
vector<Territory*> Map::getTerritories() {
return territories;
}
// get continents
vector<Continent*> Map::getContinents() {
return continents;
}
// set territories
void Map::setTerritories(vector<string> _territoriesData, vector<string> _bordersData) {
for (int i = 0; i < _territoriesData.size(); i++) {
if(_territoriesData[i].size() != 1) {
Territory* tempTer = new Territory(_territoriesData[i], _bordersData[i]);
territories.push_back(tempTer);
}
}
}
// set continents
void Map::setContinents(vector<string> _continentsData) {
// create continent objects from map data
for (int i = 0; i < _continentsData.size(); i++) {
if(_continentsData[i].size() != 1) {
Continent* tempCont = new Continent(to_string(i + 1), _continentsData[i]);
continents.push_back(tempCont);
}
}
for (auto & continent : continents) {
vector<string> adjacentCont;
// find each continent's territories
vector<Territory*> terr = getTerritoriesByContinentId(continent->id);
for (auto & j : terr) {
// find each territory adjacent nodes
vector<Territory*> adjacentTerr = getAdjacentTerritories(j->id);
for (auto & k : adjacentTerr) {
// push continent of adjacent territories of the continent's territories
if(!(std::find(adjacentCont.begin(), adjacentCont.end(), k->getContinentID()) != adjacentCont.end()) &&
k->getContinentID() != continent->id) {
adjacentCont.push_back(k->getContinentID());
}
}
}
// set continent of adjacent territories of the continent's territories, as the continents adjacent territorries
continent->adjacent = adjacentCont;
}
}
// get territory object by its id
Territory* Map::getTerritoryById(string territoryID) {
for (auto & territorie : territories) {
if(territorie->id == territoryID) {
return territorie;
}
}
return NULL;
}
// get continent object by its id
Continent* Map::getContinentById(string id){
for (auto & continent : continents) {
if(continent->id == id) {
return continent;
}
}
return NULL;
}
// get all territory objects of a continent
vector<Territory*> Map::getTerritoriesByContinentId(string id){
vector<Territory*> terr;
for (auto & territorie : territories) {
if(territorie->getContinentID() == id) {
terr.push_back(territorie);
}
}
return terr;
}
// get all Territories adjacent to a specific territory
vector<Territory*> Map::getAdjacentTerritories(string territoryID) {
vector<Territory*> tempList;
for (auto & territorie : territories) {
if(territorie->id == territoryID) {
vector<string> nodesStrings = territorie->getAdjacentNodes();
for (auto & nodesString : nodesStrings) {
tempList.push_back(getTerritoryById(reinterpret_cast<basic_string<char> &&>(nodesString)));
}
}
}
return tempList;
}
// get all enemy Territories adjacent to a specific territory
vector<Territory*> Map::getAdjacentEnemyTerritories(string territoryID, Player* p) {
vector<Territory*> enemyList;
for (auto& territorie : territories) {
if (territorie->id == territoryID) {
vector<string> nodesStrings = territorie->getAdjacentNodes();
for (auto& nodesString : nodesStrings) {
Territory* t = getTerritoryById(reinterpret_cast<basic_string<char>&&>(nodesString));
if (t->getOwnerID()!=p->name) enemyList.push_back(t);
}
}
}
return enemyList;
}
// stream insertion operator
ostream &operator<<(ostream &os, const Map &n) {
os << "Map " << n.name ;
os << " has " << n.continents.size() << " continents";
os << " and " << n.territories.size()<< " territories" << endl;
os << endl;
os << " The continent's are:"<< endl;
for(auto continent : n.continents) {
os << *continent;
}
os << endl;
os << " The territories' are:"<< endl;
for(auto territorie : n.territories) {
os << *territorie;
}
return os;
}
// assignment operator
Map& Map::operator = (const Map& _file) {
name = _file.name;
continents = _file.continents;
territories = _file.territories;
return *this;
}
bool Map::continentHasUniqueOwner(string continentID, int playerName) {
vector<Territory*> territories = getTerritoriesByContinentId(continentID);
for(auto terr : territories) {
// if one has different owner -> not true
if(terr->getOwnerID() != playerName) {
return false;
}
}
// if finish loop -> all the same
return true;
}
vector<Territory *> Map::getTerritoriesByOwnerID(int ownerID) {
vector<Territory *> tempList = {};
for (auto &t : territories) {
if(t->getOwnerID() == ownerID) {
tempList.push_back(t);
}
}
return tempList;
}
/////////////////////////////////////////////////////////////////////////////
/// CONTINENT ///
/////////////////////////////////////////////////////////////////////////////
Continent::Continent() = default;
// constructor
Continent::Continent(string pos, string continentsString) {
id = pos;
//seperate string by spaces
regex ws_re("\\s{1}");
vector<string> result{
sregex_token_iterator(continentsString.begin(), continentsString.end(), ws_re, -1), {}
};
name = result[0];
try{
bonus = stoi(result[1]);
} catch(exception) {
bonus = 0;
}
}
// copy constructor
Continent::Continent(const Continent& _c){
id = _c.id;
name = _c.name;
adjacent = _c.adjacent;
bonus = _c.bonus;
}
// destructor
Continent::~Continent() = default;
// stream insertion operator
ostream &operator<<(ostream &os, const Continent& n) {
os << n.name << " is continent #" << n.id << ", it gives " << n.bonus << " bonus";
os << " and has " << n.adjacent.size() << " adjacent continents -> ";
for(int i = 0; i < n.adjacent.size(); i++) {
if(i == n.adjacent.size()-1) {
os << n.adjacent.at(i) << ". "<< endl;
} else {
os << n.adjacent.at(i) << ", ";
}
}
return os;
}
// assignment operator
Continent& Continent::operator = (const Continent& _file) {
name = _file.name;
id = _file.id;
adjacent = _file.adjacent;
return *this;
}
/////////////////////////////////////////////////////////////////////////////
/// TERRITORY ///
/////////////////////////////////////////////////////////////////////////////
Territory::Territory() = default;
// contructor
Territory::Territory(string territoryString, string borderString) {
armiesNumber = 0;
ownerID = 99;
// seperate strings by spaces
regex ws_re("\\s+");
vector<string> result{
sregex_token_iterator(territoryString.begin(), territoryString.end(), ws_re, -1), {}
};
vector<string> bordersResult{
sregex_token_iterator(borderString.begin(), borderString.end(), ws_re, -1), {}
};
id = result[0];
name = result[1];
continentID = result[2];
for (int i = 0; i < bordersResult.size(); i++) {
if (i > 0) {
adjacent.push_back(bordersResult[i]);
}
}
}
// copy contructor
Territory::Territory(const Territory& otherTerritory) {
armiesNumber = otherTerritory.armiesNumber;
ownerID = otherTerritory.ownerID;
id = otherTerritory.id;
name = otherTerritory.name;
continentID = otherTerritory.continentID;
adjacent = otherTerritory.adjacent;
}
// destructor
Territory::~Territory() = default;
// get continent id
string Territory::getContinentID() {
return continentID;
}
// get adjacent nodes
vector<string> Territory::getAdjacentNodes() {
return adjacent;
}
// get armies
int Territory::getArmies() {
return armiesNumber;
}
// check if nodes are adjacent
bool Territory::isAdjacentNode(const string& _node) {
for (auto & i : adjacent) {
if(i == _node) {
return true;
}
}
return false;
}
// set armies
void Territory::setArmiesNumber(int amount) {
armiesNumber = amount;
}
// get owner
int Territory::getOwnerID() {
return ownerID;
}
// set owner
void Territory::setOwner(int _ownerID) {
ownerID = _ownerID;
}
// stream insertion operator
ostream &operator<<(ostream &os, const Territory &n) {
os << n.name << " is node #" << n.id << " from continent #" << n.continentID;
os << ", has " << n.adjacent.size() << " adjacent territories -> ";
for(int i = 0; i < n.adjacent.size(); i++) {
if(i == n.adjacent.size()-1) {
os << n.adjacent.at(i) << " ";
} else {
os << n.adjacent.at(i) << ", ";
}
}
os << " is owned by: " << n.ownerID;
os << " and has " << n.armiesNumber << " armies." << endl;
return os;
}
// assignment operator
Territory& Territory::operator = (const Territory& _file) {
name = _file.name;
id = _file.id;
continentID = _file.continentID;
adjacent = _file.adjacent;
armiesNumber = _file.armiesNumber;
ownerID = _file.ownerID;
return *this;
}
//comparison operator
bool Territory::operator< (const Territory& otherTerritory) const {
return armiesNumber < otherTerritory.armiesNumber;
}