-
Notifications
You must be signed in to change notification settings - Fork 0
/
islands.h
376 lines (313 loc) · 7.08 KB
/
islands.h
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
/***********************************************************************\
*
* Contents: islands detector
* Systems: all
*
\***********************************************************************/
#ifndef ISLANDS_H
#define ISLANDS_H
/****************************** Includes *******************************/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <exception>
#include <cassert>
#include "color.h"
/****************** Conditional compilation switches *******************/
/***************************** Constants *******************************/
/***************************** Datatypes *******************************/
/***************************** Variables *******************************/
/****************************** Macros *********************************/
/***************************** Forwards ********************************/
/***************************** Functions *******************************/
/** 2D coordinates
*/
class Coordinates
{
public:
int x,y;
Coordinates(int x, int y)
: x(x)
, y(y)
{
}
friend std::ostream& operator<<(std::ostream &outputStream, const Coordinates &coordinates)
{
outputStream << "Coordinates { " << coordinates.x << ", " << coordinates.y << " }";
return outputStream;
}
};
class Island;
/** tile in map
*/
class Tile
{
public:
enum class Types
{
WATER,
LAND,
TREE,
MOUNTAIN,
BUILDING
};
Tile()
: coordinates(Coordinates(0,0))
, type(Types::WATER)
, island(nullptr)
{
}
Tile(Types type)
: coordinates(Coordinates(0,0))
, type(type)
, island(nullptr)
{
}
Tile(const Coordinates &coordinates, Types type)
: coordinates(coordinates)
, type(type)
, island(nullptr)
{
}
Tile(uint x, uint y, Types type)
: coordinates(Coordinates(x,y))
, type(type)
, island(nullptr)
{
}
const Coordinates &getCoordinates() const
{
return coordinates;
}
Types getType() const
{
return type;
}
Color getColor() const
{
return color;
}
void set(const Coordinates &coordinates, Types type, const Color &color)
{
this->coordinates = coordinates;
this->type = type;
this->color = color;
}
void set(const Coordinates &coordinates, Types type)
{
this->coordinates = coordinates;
this->type = type;
}
Island *getIsland() const
{
return island;
}
void setIsland(Island *island)
{
assert(island != nullptr);
this->island = island;
}
bool isIsland(const Island *island) const
{
return this->island == island;
}
friend std::ostream& operator<<(std::ostream &outputStream, const Tile &tile)
{
outputStream << "Tile { " << tile.coordinates << ", type=";
switch (tile.getType())
{
case Types::WATER:
outputStream << "water";
break;
case Types::LAND:
outputStream << "land";
break;
case Types::TREE:
outputStream << "tree";
break;
case Types::MOUNTAIN:
outputStream << "mountain";
break;
case Types::BUILDING:
outputStream << "building";
break;
}
outputStream << " }";
return outputStream;
}
private:
Coordinates coordinates;
Types type;
Color color;
Island *island;
};
/** island
*/
class Island
{
public:
Island()
{
}
Island(const Coordinates &coordinates)
: frontiers({coordinates})
{
}
char getId() const
{
return id;
}
void setId(char id)
{
this->id = id;
}
/** check if there are any unexplorted frontier tiles available
* @return true iff frontier tiles available
*/
bool hasFrontiers() const
{
return frontiers.size() > 0;
}
/** fetch and remove next frontier coordinate
* @return coordinates
*/
Coordinates fetchFrontier()
{
Coordinates coordinates = frontiers.back();
frontiers.pop_back();
return coordinates;
}
/** merge tile into island
* @param tile tile
*/
void merge(Tile &tile)
{
tile.setIsland(this);
frontiers.push_back(tile.getCoordinates());
}
friend std::ostream& operator<<(std::ostream &outputStream, const Island *island)
{
outputStream << "Island { frontiers=" << island->frontiers.size() << " }";
return outputStream;
}
private:
char id;
std::vector<Coordinates> frontiers;
};
/** map
*/
class Map
{
public:
/** create new map
* @oaram width, height map width+height
*/
Map(uint width, uint height)
: width(width)
, height(height)
{
for (uint y = 0; y < height; y++)
{
std::vector<Tile> row;
for (uint x = 0; x < height; x++)
{
row.push_back(Tile());
}
tiles.push_back(row);
}
}
/** create new map
*/
Map()
: width(0)
, height(0)
{
}
/** destroy map
*/
virtual ~Map()
{
for (const Island *island : islands)
{
delete(island);
}
}
/** reset map content
*/
void reset();
/** get map width
* @return width
*/
uint getWidth() const
{
return width;
}
/** get map height
* @return height
*/
uint getHeight() const
{
return height;
}
/** get tile at x/y position
* @param x,y position
* @return tile
*/
Tile &getTile(int x, int y);
/** set tile at x/y posiiton
* @param x,y position
* @param type tile type
* @param color color
*/
void setTile(uint x, uint y, Tile::Types type, const Color &color)
{
assert(x < width);
assert(y < height);
tiles[y][x].set(Coordinates(x, y), type, color);
}
/** set tile at x/y posiiton
* @param x,y position
* @param type tile type
*/
void setTile(uint x, uint y, Tile::Types type)
{
assert(x < width);
assert(y < height);
tiles[y][x].set(Coordinates(x, y), type);
}
/** load map
* @oaram filePath file path
*/
void load(const std::string &filePath);
/** save map
* @oaram filePath file path
*/
void save(const std::string &filePath);
/** find islands
*/
uint findIslands();
/** print map with detected islands
*/
void printIslands() const;
/** print
*/
void print() const;
friend std::ostream& operator<<(std::ostream &outputStream, const Map &map)
{
outputStream << "Map { ";
outputStream << " }";
return outputStream;
}
private:
uint width, height;
std::vector<std::vector<Tile>> tiles;
std::unordered_set<Island*> islands;
};
#endif // ISLANDS_H
/* end of file */