-
Notifications
You must be signed in to change notification settings - Fork 2
/
map.cpp
309 lines (278 loc) · 8.27 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
#include "map.h"
#include <iostream>
//#include <crtdbg.h>
#ifdef _DEBUG
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
// Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
// allocations to be of _CLIENT_BLOCK type
#else
#define DBG_NEW new
#endif
//Class Territory
//constructors
Territory::Territory() {
territory_name = "default territory";
territory_continent = 0;
territory_owner = DBG_NEW Player();
territory_armycount = 0;
}
//Parameterized constructor
Territory::Territory(int continent,string name, Player* player, int army) {
territory_continent = continent;
territory_name = name;
territory_owner = player;
territory_armycount= army;
}
//Copy constructor
Territory::Territory(const Territory* territory) {
territory_continent = territory->territory_continent;
territory_name = territory->territory_name;
territory_owner = territory->territory_owner;
territory_armycount = territory->territory_armycount;
}
//assignment operator
Territory& Territory::operator=(const Territory& territory) {
this->territory_continent = territory.territory_continent;
this->territory_name = territory.territory_name;
this->territory_owner = territory.territory_owner;
this->territory_armycount = territory.territory_armycount;
return *this;
}
//Stream insertion operator overload to output a territory
ostream& operator << (ostream& out, const Territory& t) {
out << "Territory of " << t.territory_name << " Owned by: " << *t.territory_owner <<
+", Occupied by: " << t.territory_armycount << " troops" << endl;
return out;
}
//getters
string Territory::getterritory_name() {
return territory_name;
}
int Territory::getterritory_continent() {
return territory_continent;
}
Player* Territory::getterritory_owner() {
return territory_owner;
}
int Territory::getterritory_armycount() {
return territory_armycount;
}
void Territory::setterritory_armycount(int a) {
territory_armycount = a;
}
//setters
void Territory::setterritory_name(string s) {
territory_name = s;
}
void Territory::setterritory_owner(Player* p) {
territory_owner = p;
}
//destructor
Territory::~Territory() {
territory_owner = NULL;
}
//Class Continent
Continent::Continent() {
name = "";
id = 0;
bonus = 0;
}
Continent::Continent(const Continent* c) {
name = c->name;
bonus = c->bonus;
id = c->id;
for (int i = 0; i < c->territoriesInContinent.size(); i++) {
this->territoriesInContinent.push_back(c->territoriesInContinent[i]);
}
}
Continent::Continent(string n, int i, int b, vector<Territory*> terr) {
name = n;
id = i;
bonus = b;
for (int i=0; i<terr.size(); i++) {
territoriesInContinent.push_back( terr[i]);
}
}
//getters and setters
void Continent::setName(string n) {
name = n;
}
void Continent::setId(int i) {
id = i;
}
void Continent::setBonus(int b) {
id = b;
}
string Continent::getName() {
return name;
}
int Continent::getId() {
return id;
}
int Continent::getBonus() {
return bonus;
}
bool Continent::ownedByOnePlayer(Player* aPlayer, Map *m){
for (int i=0; i<territoriesInContinent.size(); i++){
if (territoriesInContinent.at(i)->getterritory_owner()->getName().compare(aPlayer->getName()) != 0)
{
return false;
}
}
return true;
}
//insertion operator (toString)
ostream& operator << (ostream& out, const Continent& c) {
out << c.id <<" Continent " << c.name << ", has bonus armies of " << c.bonus
<< "\nList of territories ("<< c.territoriesInContinent.size() <<")\n[\n";
for(auto T : c.territoriesInContinent){
out << T->getterritory_name() << "\n";
}
out << "]\n" << endl;
return out;
}
Continent::~Continent() {
}
//Class Map
//constructors
Map::Map() {
this->adjacent_matrix = 0;
this->vertices = 0;
}
Map::Map(const Map *map) {
this->vertices = map->vertices;
this->adjacent_matrix = DBG_NEW bool* [this->vertices];
for (int i = 0; i < vertices; i++) {
this->adjacent_matrix[i] = DBG_NEW bool [this->vertices];
for (int j = 0; j < vertices; j++)
adjacent_matrix[i][j] = map->adjacent_matrix[i][j];
}
this->territoryListPtr = DBG_NEW vector<Territory*>;
for (int i = 0; i < map->territoryListPtr->size(); i++) {
territoryListPtr->push_back(DBG_NEW Territory(map->territoryListPtr->at(i)));
}
this->continentListPtr = DBG_NEW vector<Continent*>;
for (int i = 0; i < map->continentListPtr->size(); i++) {
continentListPtr->push_back(DBG_NEW Continent(map->continentListPtr->at(i)));
}
}
Map::Map(int vertices, vector<Territory*>* territoryList, vector<Continent*>* continentList) {
this->vertices = vertices;
adjacent_matrix = DBG_NEW bool* [vertices];
//initialize the 2d array with false
for (int i = 0; i < vertices; i++) {
adjacent_matrix[i] = DBG_NEW bool[vertices];
for (int j = 0; j < vertices; j++)
adjacent_matrix[i][j] = false;
}
//Set the territories list from loaded map file
territoryListPtr = DBG_NEW vector<Territory*>;
for (int i = 0; i < territoryList->size(); i++) {
territoryListPtr->push_back(territoryList->at(i));
}
continentListPtr = DBG_NEW vector<Continent*>;
for (int i = 0; i < continentList->size(); i++) {
continentListPtr->push_back(continentList->at(i));
}
}
//destructor
Map::~Map() {
for (int i = 0; i < vertices; i++) {
if(adjacent_matrix[i]!= NULL)delete[] adjacent_matrix[i];
adjacent_matrix[i] = NULL;
}
delete[] adjacent_matrix;
adjacent_matrix = NULL;
for (int i = 0; i < territoryListPtr->size(); i++) {
delete territoryListPtr->at(i);
}
territoryListPtr->clear();
delete territoryListPtr;
for (int i = 0; i < continentListPtr->size(); i++) {
delete continentListPtr->at(i);
}
continentListPtr->clear();
delete continentListPtr;
}
//assignment operator
Map& Map::operator=(const Map& map)
{
this->adjacent_matrix = map.adjacent_matrix;
this->vertices = map.vertices;
return *this;
}
vector<Territory*>* Map::getTerritories()
{
return territoryListPtr;
}
//add an border between two v's
void Map::addBorder(int i, int j) {
adjacent_matrix[i][j] = true;
adjacent_matrix[j][i] = true;
}
//to display the matrix
void Map::toString() {
std::cout << "Output Matrix: " << std::endl;
std::cout << " ";
for (int x = 0; x < vertices; x++) {
std::cout << x << " ";
}
std::cout << std::endl;
for (int i = 0; i < vertices; i++) {
if (i>=10){
std::cout << " " << i << " ";
for (int j = 0; j < vertices; j++)
std::cout << adjacent_matrix[i][j] << " ";
std::cout << "\n";
}
else{
std::cout << " "<< i << " ";
for (int j = 0; j < vertices; j++)
std::cout << adjacent_matrix[i][j] << " ";
std::cout << "\n";
}
}
}
//transvering matrix
void Map::transverse(int u, bool visited[]) {
visited[u] = true;
for (int v = 0; v < vertices; v++) {
if (adjacent_matrix[u][v]) {
if (!visited[v])
transverse(v, visited);
}
}
}
//This method verifies whether the map is connected or not
//checks whether the nodes (transversal) are visible -> true
//not visible -> false
bool Map::Validate() {
//Check that territories form a connected graph
bool* visited = DBG_NEW bool[vertices];
for (int u = 0; u < vertices; u++) {
for (int i = 0; i < vertices; i++)
visited[i] = false;
transverse(u, visited);
for (int i = 0; i < vertices; i++) {
if (!visited[i]) {
delete[] visited;
return false;
}
}
}
delete[] visited;
return true;
}
bool Map::isAdjacent(Territory a, Territory b) {
int indexa;
int indexb;
for (int i=0; i<territoryListPtr->size(); i++){
if (territoryListPtr->at(i)->getterritory_name().compare(a.getterritory_name())==0)indexa = i;
if (territoryListPtr->at(i)->getterritory_name().compare(b.getterritory_name())==0)indexb = i;
}
return(adjacent_matrix[indexa][indexb]);
}
vector<Continent*>* Map::getContinents()
{
return continentListPtr;
}