/
Map.cpp
195 lines (140 loc) · 8.51 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
//
// Map.cpp
// Ex1
//
// Created by Maxim Vainshtein on 14/11/2015.
// Copyright © 2015 Bar-Ilan. All rights reserved.
//
#include "Map.hpp"
#include "Tile.hpp"
#include <algorithm>
constexpr size_t Map::index(size_t x, size_t y) const { return x + m_uiWidth * y; }
Map::Map(const std::string& strFormattedMap) : m_cData(nullptr) {
//The input is in formatted view, so fill the data according to each 'tile' (letter) - first get the size
size_t uiCurrentIndex = strFormattedMap.find_first_of("\n");
m_uiHeight = m_uiWidth = stoi(strFormattedMap.substr(0, uiCurrentIndex));
//Allocate the dynamic memory block required
typedef std::aligned_storage<sizeof(Tile), std::alignment_of<Tile>::value>::type storage_type;
m_cData = reinterpret_cast<Tile*>(new storage_type[m_uiWidth * m_uiHeight]);
//Now parse each row by getting the relevant line and fill tiles by it
for (size_t uiRow = 0 ; uiRow < m_uiWidth ; uiRow++) {
size_t uiEndOfRow = strFormattedMap.find_first_of("\n", uiCurrentIndex+1);
std::string strRowInfo = strFormattedMap.substr(uiCurrentIndex+1, m_uiWidth);
uiCurrentIndex = uiEndOfRow;
for (size_t uiCol = 0 ; uiCol < m_uiHeight ; uiCol++) {
//Using placement new to create tiles, assignemt is as told by instructions
switch (strRowInfo[uiCol]) {
case 'S': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kStart, uiCol, uiRow); break;
case 'G': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kEnd, uiCol, uiRow); break;
case 'R': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kRoad, uiCol, uiRow); break;
case 'D': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kDirt, uiCol, uiRow); break;
case 'H': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kHill, uiCol, uiRow); break;
case 'W': new(m_cData + uiRow * m_uiWidth + uiCol) Tile(Tile::Types::kWater, uiCol, uiRow); break;
default: break;
}
}
}
}
Map::~Map() {
//Delete the allocated dynamic array
if (m_cData) delete [] m_cData;
}
Tile Map::getStartTile() const { return Tile(m_cData[0]); }
Tile Map::getEndTile() const { return Tile(m_cData[index(m_uiWidth, m_uiHeight)]); }
Map::Directions Map::getDirection(const Tile &cOrigin, const Tile& cDestination) const {
//Important to remember that highest is 0 and lowest is N-1, sideways is normal
//Get the horizontal direction
Directions eHorizontal;
if (cDestination.m_uiPositionX == cOrigin.m_uiPositionX) eHorizontal = Directions::kCenter;
else if (cDestination.m_uiPositionX > cOrigin.m_uiPositionX) eHorizontal = Directions::kRight;
else eHorizontal = Directions::kLeft;
//Get the vertical direction
Directions eVertical;
if (cDestination.m_uiPositionY == cOrigin.m_uiPositionY) eVertical = Directions::kCenter;
else if (cDestination.m_uiPositionY > cOrigin.m_uiPositionY) eVertical = Directions::kDown;
else eVertical = Directions::kUp;
//Compare both horizontal and vertical to get the final heading
switch (eHorizontal) {
case Directions::kCenter:
switch (eVertical) {
case Directions::kCenter: return Directions::kCenter;
case Directions::kUp: return Directions::kUp;
case Directions::kDown: return Directions::kDown;
default: break; //Shouldnt reach
}
break;
case Directions::kRight:
switch (eVertical) {
case Directions::kCenter: return Directions::kRight;
case Directions::kUp: return Directions::kRightUp;
case Directions::kDown: return Directions::kRightDown;
default: break; //Shouldnt reach
}
case Directions::kLeft:
switch (eVertical) {
case Directions::kCenter: return Directions::kLeft;
case Directions::kUp: return Directions::kLeftUp;
case Directions::kDown: return Directions::kLeftDown;
default: break; //Shouldnt reach
}
//Shouldnt reach
default: break;
}
throw std::runtime_error("incorrect implementation of direction between tiles");
}
const Tile& Map::getTile(const Tile &cOrigin, Directions direction) const {
}
const std::vector<const Tile*> Map::getNeighbors(const Tile &cTile) const {
//Can have up to 8 adjacent tiles
std::vector<const Tile*> vcNeighbors(8);
size_t uiRowHighest = std::min(m_uiWidth, cTile.m_uiPositionX + 1);
size_t uiColHighest = std::min(m_uiHeight, cTile.m_uiPositionY + 1);
size_t uiRowLowest = std::max(size_t(0), (cTile.m_uiPositionX != 0) ? cTile.m_uiPositionX - 1 : 0);
size_t uiColLowest = std::max(size_t(0), (cTile.m_uiPositionY != 0) ? cTile.m_uiPositionY - 1 : 0);
//Iterate through all of the neighbors and store those who are valid
for (size_t uiRow = uiRowLowest ; uiRow <= uiRowHighest ; uiRow++)
for (size_t uiCol = uiColLowest ; uiCol <= uiColHighest ; uiCol++) {
//Skip the requesting tile
if (uiRow == cTile.m_uiPositionX && uiCol == cTile.m_uiPositionY) continue;
const Tile& cNeighbor = m_cData[index(uiRow, uiCol)];
//Check for water tiles
if (cNeighbor.eType == Tile::Types::kWater) continue;
else {
/*
* Check if the neighbor is sideways or not, otherwise we add (default).
* For sideways neighbors the related neighbors must also be checked.
* When the relevent tiles for the neighbors (adjacent in direction) have
* been checked and found not to be Water, it's valid.
*/
switch (getDirection(cTile, cNeighbor)) {
case Directions::kLeftDown:
//Must get the 'Down' and 'Left' neighbors of current tile - they must exist due to sideways existing
if (getTile(cTile, Directions::kLeft).eType == Tile::Types::kWater) continue;
if (getTile(cTile, Directions::kDown).eType == Tile::Types::kWater) continue;
vcNeighbors.push_back(&cNeighbor);
break;
case Directions::kLeftUp:
//Must get the 'Up' and 'Left' neighbors of current tile - they must exist due to sideways existing
if (getTile(cTile, Directions::kLeft).eType == Tile::Types::kWater) continue;
if (getTile(cTile, Directions::kUp).eType == Tile::Types::kWater) continue;
vcNeighbors.push_back(&cNeighbor);
break;
case Directions::kRightDown:
//Must get the 'Down' and 'Right' neighbors of current tile - they must exist due to sideways existing
if (getTile(cTile, Directions::kRight).eType == Tile::Types::kWater) continue;
if (getTile(cTile, Directions::kDown).eType == Tile::Types::kWater) continue;
vcNeighbors.push_back(&cNeighbor);
break;
case Directions::kRightUp:
//Must get the 'Up' and 'Right' neighbors of current tile - they must exist due to sideways existing
if (getTile(cTile, Directions::kRight).eType == Tile::Types::kWater) continue;
if (getTile(cTile, Directions::kUp).eType == Tile::Types::kWater) continue;
vcNeighbors.push_back(&cNeighbor);
break;
//Since the direction is 'Up', 'Left', 'Right', 'Down' its valid
default: vcNeighbors.push_back(&cNeighbor); break;
}
}
}
return vcNeighbors;
}