-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHouseMap.h
190 lines (117 loc) · 5.61 KB
/
HouseMap.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
#ifndef _HOUSE_MAP__H_
#define _HOUSE_MAP__H_
#include <map>
#include "Position.h"
#include "Constants.h"
enum class HouseView { Map, Matrix };
class Path
{
Position endPos;
int distance;
Direction nextStep;
public:
Path(Position endPos, int distance, Direction nextStep) : endPos(endPos), distance(distance), nextStep(nextStep) {}
Position getPosition() const { return endPos; }
int getDistance() const { return distance; }
Direction getNextStep() const { return nextStep; }
};
class Square {
int row;
int col;
unsigned char value;
public:
Square(int row, int col, unsigned char value) : row(row), col(col), value(value) {}
Square(Position pos, unsigned char value) : row(pos.getRow()), col(pos.getCol()), value(value) {}
int getRow() const { return row; }
int getCol() const { return col; }
char getValue() const { return value == '0' ? SPACE : value; }
Position getPosition() const { return Position(row, col); }
};
class HouseMap
{
map<Position, unsigned char> houseMap;
unsigned char** houseMatrix;
int eastBorder, westBorder, southBorder, northBorder;
Position mapDockPos, matrixDockPos;
int dirtLeft;
bool isHouseFullyMapped;
int distanceToDock;
int distanceLastUpdate;
HouseView view;
public:
HouseMap()
{
mapDockPos = matrixDockPos = Position(0, 0);
isHouseFullyMapped = false;
eastBorder = westBorder = southBorder = northBorder = 0;
dirtLeft = 0;
view = HouseView::Map;
setDistanceToDock(0);
}
~HouseMap() { DeleteMatrix(houseMatrix); }
unsigned char operator[](const Position pos) const { return view == HouseView::Matrix ? GetFromMatrix(pos) : GetFromMap(pos); }
void operator[](const Square square) {
if (view == HouseView::Matrix) InsertToMatrix(square);
else { InsertToMap(square); UpdateBorders(square.getPosition()); }
}
int getDistanceLastUpdate() const { return distanceLastUpdate; }
int getDistanceToDock() const { return distanceToDock; }
void setDistanceToDock(int distance) { distanceToDock = distance; distanceLastUpdate = 0; }
void IncrementDistanceToDock() { if (distanceLastUpdate > 0) distanceToDock++; distanceLastUpdate++; }
Position getDockingPos() const { return view == HouseView::Matrix ? matrixDockPos : mapDockPos; }
HouseView getHouseView() const { return view; }
int getEastBorder() const { return eastBorder; }
int getWestBorder() const { return westBorder; }
int getSouthBorder() const { return southBorder; }
int getNorthBorder() const { return northBorder; }
int getHeight() const { return southBorder - northBorder + 1; }
int getWidth() const { return eastBorder - westBorder + 1; }
void DecreaseDirt(Position pos);
void AddToDirt(int dirt) { dirtLeft += dirt; }
void BordersRevealed() {
CreateMatrix();
view = HouseView::Matrix;
}
void Print(unsigned char** matrix) const;
Path FindPathToTarget(unsigned char** matrix, int seed);
Direction FindClosestUndiscoveredReachableSquareOnBorderNextStep(Position startPos, int seed);
Direction FindClosestUndiscoveredReachableSquareNextStep(Position startPos, int seed);
Direction FindClosestDirtySquareNextStep(Position startPos, int seed);
Direction ShortestDistanceNextStep(Position startPos, Position endPos, int seed);
int FindShortestDistance(Position startPos, Position endPos);
bool IsBordersMapped() const { return view == HouseView::Matrix; }
bool IsDirtySquare(Position pos) const { return IsDirtySquare(operator[](pos)); }
bool IsWall(Position pos) const { return operator[](pos) == WALL; }
bool IsSquareAlreadyDiscovered(Position pos) const { return operator[](pos) != UNDISCOVERD_UNREACHABLE && operator[](pos) != UNDISCOVERD_REACHABLE; }
bool IsUndiscoveredButReachableSquare(Position pos) const { return operator[](pos) == UNDISCOVERD_REACHABLE; }
bool IsSquareAlreadyVisited(Position pos) const { return IsSquareAlreadyDiscovered(pos) && !IsWall(pos); }
bool IsSquareReachable(Position pos) const { return IsSquareAlreadyVisited(pos) || IsUndiscoveredButReachableSquare(pos); }
bool IsAllHouseRevelead();
bool IsHouseClean() const { return dirtLeft == 0; }
bool IsBordersDiscovered() const;
private:
unsigned char GetFromMap(int row, int col) const { return GetFromMap(Position(row, col)); }
unsigned char GetFromMap(const Position pos) const {
if (houseMap.find(pos) == houseMap.end()) return UNDISCOVERD_UNREACHABLE;
return houseMap.at(pos);
}
unsigned char GetFromMatrix(const Position pos) const { return houseMatrix[pos.getRow()][pos.getCol()]; }
void InsertToMap(Square square) { houseMap[square.getPosition()] = square.getValue(); }
void InsertToMatrix(Square square) { houseMatrix[square.getRow()][square.getCol()] = square.getValue(); }
void UpdateBorders(Position pos);
void CreateMatrix();
void RemoveAllUndiscoverableSuqares();
void DeleteMatrix(unsigned char** matrix) const;
Direction GetFirstStepFromPath(unsigned char** matrix, int row, int col, unsigned char lastRoundChar) const;
unsigned char** ClosestUndiscoveredReachableSquareOnBorderMatrix(Position startPos) const;
unsigned char** ClosestUndiscoveredReachableSquareMatrix(Position startPos) const;
unsigned char** DirtySquaresMatrix(Position startPos) const;
unsigned char** FindShortestDistanceMatrix(Position startPos, Position endPos) const;
unsigned char** BordersMatrix() const;
unsigned char** CreateDistanceMatrix() const;
bool IsHouseCompleted();
bool IsSquareOnBorder(int row, int col) const { return row == 0 || row == getHeight() - 1 || col == 0 || col == getWidth() - 1; }
bool IsBorderReachable(unsigned char** matrix, int i, int j) const;
bool IsDirtySquare(unsigned char squareValue) const { return squareValue >= '1' && squareValue <= '9'; }
};
#endif //_HOUSE_MAP__H_