forked from AA789-ai/WarzoneGame-COMP345
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
345 lines (290 loc) · 9.96 KB
/
map.cpp
File metadata and controls
345 lines (290 loc) · 9.96 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
#include <iostream>
#include <string>
#include <vector>
#include "map.h"
#include <unordered_map>
//---------------------------Territory-------------------------------
// Constructor for Territory: Initializes the name, x-coordinate, and y-coordinate.
Territory::Territory(const std::string& n, int xCord, int yCord){
name = new std::string(n);
x = new int(xCord);
y = new int(yCord);
numberOfArmies = new int (0);
}
// Destructor for Territory: Deletes the dynamically allocated members. We should not delete adjacent territories.
Territory::~Territory(){
delete name;
delete x;
delete y;
delete numberOfArmies;
name = nullptr;
x = nullptr;
y = nullptr;
numberOfArmies = nullptr;
/*for(Territory* adjacentTerritory : adjacentTerritories){
delete adjacentTerritory;
}*/
}
//Copy constuctor. Just copying the pointers to adjacent territories, not the territories themselves as it won't make any sense.
Territory::Territory(const Territory& other) {
name = new std::string(*(other.name));
x = new int(*(other.x));
y = new int(*(other.y));
adjacentTerritories = other.adjacentTerritories;
}
// Adds an adjacent territory to the current territory's adjacentTerritories vector.
void Territory::addAdjacentTerritory(Territory* territory){
adjacentTerritories.push_back(territory);
}
//Basic getters
std::string Territory::getName() const{
return *name;
}
int Territory::getX() const{
return *x;
}
int Territory::getY() const{
return *y;
}
int Territory::getNumberOfArmies() const {
return *numberOfArmies;
}
// Getter for the adjacent territories.
const std::vector<Territory*>& Territory::getAdjacentTerritories() const {
return adjacentTerritories;
}
//Basic setters
void Territory::setName(const std::string& n){
delete name;
name = new std::string(n);
}
void Territory::setX (int xCord){
delete x;
x = new int(xCord);
}
void Territory::setY (int yCord){
delete y;
y = new int(yCord);
}
void Territory::setNumberOfArmies(int numOfArmies) {
delete numberOfArmies;
numberOfArmies = new int(numOfArmies);
}
//Assignment Operator
Territory& Territory::operator=(const Territory& other) {
if (this == &other) return *this;
delete name;
delete x;
delete y;
name = new std::string(*(other.name));
x = new int(*(other.x));
y = new int(*(other.y));
adjacentTerritories = other.adjacentTerritories;
return *this;
}
//Stream insertion Operator
std::ostream& operator<<(std::ostream& out, const Territory& territory) {
out << "Territory Name: " << territory.getName() << ", X: " << territory.getX() << ", Y: " << territory.getY() << ", Number of armies: " << territory.getNumberOfArmies();
return out;
}
//-------------------------------Continent-----------------------------
// Constructor for Continent: Initializes the name and the number of armies.
Continent::Continent(const std::string& n, int bonus) {
name = new std::string(n);
this->bonus = new int(bonus);
}
// Destructor for Continent: Deletes the dynamically allocated members. We should not delete territories of a continent when deleting it.
Continent::~Continent(){
delete name;
delete bonus;
name = nullptr;
bonus = nullptr;
/*for (Territory* territory : territories){
delete territory;
}*/
}
//Copy constructor. Just copying the pointers of territorries. Not the territories themselves as it doesn't make sense.
Continent::Continent(const Continent& other) {
name = new std::string(*(other.name));
bonus = new int(*(other.bonus));
territories = other.territories;
}
// Adds a territory to the current continent's territories vector.
void Continent::addTerritory(Territory* territory) {
territories.push_back(territory);
}
//Basic getters
const std::vector<Territory*>& Continent::getTerritories() const {
return territories;
}
int Continent::getBonus() const {
return *bonus;
}
std::string Continent::getName() const {
return *name;
}
//Assignment Operator
Continent& Continent::operator=(const Continent& other) {
if (this == &other) return *this;
delete name;
delete bonus;
name = new std::string(*(other.name));
bonus = new int(*(other.bonus));
territories = other.territories;
return *this;
}
//Stream insertion Operator
std::ostream& operator<<(std::ostream& out, const Continent& continent) {
out << "Continent Name: " << continent.getName() << ", Number of Armies: " << continent.getBonus();
return out;
}
//---------------------------------Map----------------------------------
// Constructor for Map: Initializes the image, scroll, author, name, wrap, and warn.
Map::Map(const std::string& i, const std::string& s, const std::string& a, const std::string& n, bool wp, bool wn){
image = new std::string(i);
scroll = new std::string(s);
author = new std::string(a);
name = new std::string(n);
wrap = new bool(wp);
warn = new bool(wn);
}
// Destructor for Map: Deletes the dynamically allocated members and territories and continents vectors. This is where we delete territories and continent as well.
Map::~Map(){
delete image;
delete scroll;
delete author;
delete name;
delete wrap;
delete warn;
image = nullptr;
scroll = nullptr;
author = nullptr;
name = nullptr;
wrap = nullptr;
warn = nullptr;
for (Territory* territory : territories){
delete territory;
}
territories.clear();
for (Continent* continent : continents){
delete continent;
}
territories.clear();
}
// Copy constructor Just copying the pointers, not the territories and continents themselves as it doesn't make sense.
Map::Map(const Map& other) {
image = new std::string(*(other.image));
scroll = new std::string(*(other.scroll));
author = new std::string(*(other.author));
name = new std::string(*(other.name));
wrap = new bool(*(other.wrap));
warn = new bool(*(other.warn));
territories = other.territories;
continents = other.continents;
}
// Adds a territory to the map's territories vector.
void Map::addTerritory(Territory* territory){
territories.push_back(territory);
}
// Adds a continent to the map's continents vector.
void Map::addContinent(Continent* continent){
continents.push_back(continent);
}
// Basic getters
std::string Map::getImage() const{
return *image;
}
std::string Map::getScroll() const{
return *scroll;
}
std::string Map::getAuthor() const{
return *author;
}
std::string Map::getName() const{
return *name;
}
bool Map::getWrap() const {
return *wrap;
}
bool Map::getWarn() const {
return *warn;
}
const std::vector<Continent*>& Map::getContinents() const {
return continents;
}
//Assignment Operator
Map& Map::operator=(const Map& other) {
if (this == &other) return *this;
delete image;
delete scroll;
delete author;
delete name;
delete wrap;
delete warn;
image = new std::string(*(other.image));
scroll = new std::string(*(other.scroll));
author = new std::string(*(other.author));
name = new std::string(*(other.name));
wrap = new bool(*(other.wrap));
warn = new bool(*(other.warn));
territories = other.territories;
continents = other.continents;
return *this;
}
//Stream insertion Operation
std::ostream& operator<<(std::ostream& out, const Map& map) {
out << "Map Name: " << map.getName() << ", Author: " << map.getAuthor();
return out;
}
//--------------------------------Map Validation Helper methods----------------------------------
// Depth-first search to visit territories and build the visited set.
void Map::dfs(Territory* start, std::unordered_set<Territory*>& visited) const {
if (visited.find(start) != visited.end()) return;
visited.insert(start);
for (Territory* adjacent : start->getAdjacentTerritories()) {
dfs(adjacent, visited);
}
}
// Checks if all territories in the map are interconnected.
//Satisfies: 1) the map is a connected graph
bool Map::isConnected() const {
if (territories.empty()) return true;
std::unordered_set<Territory*> visited;
dfs(territories[0], visited);
return visited.size() == territories.size();
}
// Checks if all continents in the map are interconnected through their territories.
//Satisfies: 2) continents are connected subgraphs
bool Map::areContinentsInterconnected() const {
if (continents.empty() || territories.empty()) return true;
std::unordered_set<Territory*> visited;
dfs(territories[0], visited);
for (Continent* continent : continents) {
bool continentVisited = false;
for (Territory* territory : continent->getTerritories()) {
if (visited.find(territory) != visited.end()) {
continentVisited = true;
break;
}
}
if (!continentVisited) return false;
}
return true;
}
// Checks if territories are unique to continents.
//Satisfies: 3) each country belongs to one and only one continent
bool Map::areTerritoriesUniqueToContinents() const {
std::unordered_set<Territory*> territoriesSet;
for (Continent* continent : continents) {
for (Territory* territory : continent->getTerritories()) {
if (territoriesSet.find(territory) != territoriesSet.end()) return false;
territoriesSet.insert(territory);
}
}
return true;
}
// Determines the validity of the map.
bool Map::isValid() const {
//std::cout << isConnected() << " " << areContinentsInterconnected() << " " << areTerritoriesUniqueToContinents() << " these were results\n";
return isConnected() && areContinentsInterconnected() && areTerritoriesUniqueToContinents();
}