-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
379 lines (337 loc) · 10.8 KB
/
Map.cpp
File metadata and controls
379 lines (337 loc) · 10.8 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
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
377
378
379
#include "Map.h"
#include <windows.h>
#include <ctime>
#include <cmath>
void eatenByCat();
void getCheese();
int Map::getHeight() const {
return HEIGHT;
}
int Map::getWidth() const {
return WIDTH;
}
Map::Map(int width, int height) {
srand(time(0));
WIDTH = width;
HEIGHT = height;
maze = new Square*[HEIGHT];
Square* cells = new Square[HEIGHT * WIDTH];
for (int i = 0; i < HEIGHT; ++i) {
maze[i] = cells + WIDTH * i;
}
generateMap();
breakWall();
//reveal edge
for (int i = 0; i < HEIGHT; ++i) {
maze[i][0].setVisible();
maze[i][WIDTH - 1].setVisible();
}
for (int i = 0; i < WIDTH; ++i) {
maze[0][i].setVisible();
maze[HEIGHT - 1][i].setVisible();
}
}
Map::~Map() {
delete[] maze[0];
delete[] maze;
}
Square& Map::getSquare(Coordinate coordinate) {
return maze[coordinate.getY()][coordinate.getX()];
}
Square& Map::getSquare(int xCoordinate, int yCoordinate) {
return maze[yCoordinate][xCoordinate];
}
void Map::revealAroundMouse(Coordinate coordinate) {
int xCoordinate = coordinate.getX();
int yCoordinate = coordinate.getY();
for (int i = yCoordinate - 1; i <= yCoordinate + 1; ++i) {
for (int j = xCoordinate - 1; j <= xCoordinate + 1; ++j) {
maze[i][j].setVisible();
}
}
}
void Map::setAllVisible() {
for (int i = 0; i < HEIGHT * WIDTH; ++i) {
maze[0][i].setVisible();
}
}
void Map::breakWall() {
int numberOfWall = (WIDTH - 2) * (HEIGHT - 2) / 10;
int xCoordinate;
int yCoordinate;
for (int i = 0; i < numberOfWall; i++) {
bool isWall;
bool bigCell;
short wallAdjacent;
do {
xCoordinate = rand() % (WIDTH - 2) + 1;
yCoordinate = rand() % (HEIGHT - 2) + 1;
isWall = maze[yCoordinate][xCoordinate].isWall();
bigCell = hasBigOpenCell(xCoordinate, yCoordinate);
wallAdjacent = numberOfAdjacentWalls(xCoordinate, yCoordinate);
} while (!isWall || wallAdjacent == 4 || bigCell);
maze[yCoordinate][xCoordinate].setWall(false);
}
}
bool Map::hasBigOpenCell(int xCoordinate, int yCoordinate) {
if (!maze[yCoordinate - 1][xCoordinate].isWall()) {
if (!maze[yCoordinate][xCoordinate - 1].isWall() && !maze[yCoordinate - 1][xCoordinate - 1].isWall()) {
return true;
}
if (!maze[yCoordinate][xCoordinate + 1].isWall() && !maze[yCoordinate - 1][xCoordinate + 1].isWall()) {
return true;
}
}
if (!maze[yCoordinate + 1][xCoordinate].isWall()) {
if (!maze[yCoordinate][xCoordinate - 1].isWall() && !maze[yCoordinate + 1][xCoordinate - 1].isWall()) {
return true;
}
if (!maze[yCoordinate][xCoordinate + 1].isWall() && !maze[yCoordinate + 1][xCoordinate + 1].isWall()) {
return true;
}
}
return false;
}
short Map::numberOfAdjacentWalls(int xCoordinate, int yCoordinate) {
short count = 0;
if (xCoordinate > 0) {
if (maze[yCoordinate][xCoordinate - 1].isWall()) {
count++;
}
}
if (xCoordinate < WIDTH - 1) {
if (maze[yCoordinate][xCoordinate + 1].isWall()) {
count++;
}
}
if (yCoordinate > 0) {
if (maze[yCoordinate - 1][xCoordinate].isWall()) {
count++;
}
}
if (yCoordinate < HEIGHT - 1) {
if (maze[yCoordinate + 1][xCoordinate].isWall()) {
count++;
}
}
return count;
}
ostream &operator<<(ostream &os, const Map &printingMap) {
for (int i = 0; i < printingMap.getHeight(); ++i) {
for (int j = 0; j < printingMap.getWidth(); ++j) {
os << printingMap.maze[i][j];
}
os << "\n";
}
return os;
}
void Map::generateMap() {
Square** generationMaze = new Square*[HEIGHT + 2];
Square* cells = new Square[(HEIGHT + 2) * (WIDTH + 2)];
for (int i = 0; i < (HEIGHT + 2); ++i) {
generationMaze[i] = cells + (WIDTH + 2) * i;
}
bool isBacktracking = false;
for (int i = 0; i < (HEIGHT + 2); ++i) {
generationMaze[i][0].setVisited();
generationMaze[i][WIDTH + 1].setVisited();
}
for (int i = 0; i < (WIDTH + 2); ++i) {
generationMaze[0][i].setVisited();
generationMaze[HEIGHT + 1][i].setVisited();
}
Coordinate lastVisited(2, 2);
generationMaze[2][2].setVisited();
int numberOfSquaresShouldVisit = ceil((WIDTH - 2) / 2.0) * ceil((HEIGHT - 2) / 2.0);
if (WIDTH % 2 == 0) {
for (int i = 0; i < (HEIGHT + 2); ++i) {
generationMaze[i][WIDTH].setWall(true);
if (i % 2 != 0) {
generationMaze[i][WIDTH - 1].setWall(true);
}
}
}
if (HEIGHT % 2 == 0) {
for (int i = 1; i <= WIDTH; i++) {
generationMaze[HEIGHT][i].setWall(true);
if (i % 2 != 0) {
generationMaze[HEIGHT - 1][i].setWall(true);
}
}
}
Coordinate currentLocation(2, 2);
vector<Coordinate> backTracker;
int numberOfSquaresVisited = 1;
while (numberOfSquaresVisited < numberOfSquaresShouldVisit) {
vector<Coordinate> availableNextMoves = Square::suspect(currentLocation, lastVisited, generationMaze, isBacktracking);
if (availableNextMoves.empty()) {
currentLocation = backTracker.back();
backTracker.pop_back();
lastVisited = backTracker.back();
// if doing backtracking, we don't build walls
isBacktracking = true;
} else {
if (isBacktracking) {
isBacktracking = false;
}
lastVisited = currentLocation;
backTracker.push_back(lastVisited);
currentLocation = availableNextMoves[rand() % availableNextMoves.size()];
numberOfSquaresVisited += 1;
}
generationMaze[currentLocation.getY()][currentLocation.getX()].setVisited();
}
Square::suspect(currentLocation, lastVisited, generationMaze, isBacktracking);
for (int i = 1; i <= HEIGHT; i++) {
for (int j = 1; j <= WIDTH; j++) {
maze[i - 1][j - 1] = generationMaze[i][j];
}
}
delete[] cells;
delete[] generationMaze;
}
Square::Square() {
wall = false;
mouse = false;
catNumber = 0;
isCheese = false;
isVisible = false;
isVisited = false;
updateOutput();
}
void Square::setWall(bool isWall) {
Square::wall = isWall;
updateOutput();
}
bool Square::isWall() const {
return wall;
}
bool Square::setMouse(bool isMouse) {
if (wall) {
return false;
}
Square::mouse = isMouse;
updateOutput();
statusCheck();
return true;
}
bool Square::isMouse() const {
return mouse;
}
void Square::setCheese(bool isCheese) {
Square::isCheese = isCheese;
updateOutput();
statusCheck();
}
void Square::setVisible() {
Square::isVisible = true;
updateOutput();
}
void Square::addCat() {
catNumber++;
updateOutput();
statusCheck();
}
void Square::removeCat() {
catNumber--;
updateOutput();
}
void Square::updateOutput() {
if (mouse) {
if (catNumber == 0) {
output = '@';
color = 9;
} else {
output = 'X';
color = 4;
}
} else if (catNumber > 0) {
output = '!';
color = 4;
} else if (isCheese) {
output = '$';
color = 6;
} else if (!isVisible) {
output = '.';
color = 7;
} else if (wall) {
output = '#';
color = 15;
} else {
output = ' ';
color = 15;
}
}
void Square::statusCheck() {
if (catNumber > 0 && mouse) {
eatenByCat();
} else if (mouse && isCheese) {
getCheese();
}
}
ostream &operator<<(ostream &os, const Square &square) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),square.color);
os << square.output;
return os;
}
void Square::setVisited() {
isVisited = true;
updateOutput();
}
vector<Coordinate>
Square::suspect(Coordinate coordinate, Coordinate lastVisited, Square** generationMaze, bool isBacktracking) {
vector<Coordinate> availableNextMoves;
Coordinate leftCoordinate(coordinate.getX() - 2, coordinate.getY());
Coordinate rightCoordinate(coordinate.getX() + 2, coordinate.getY());
Coordinate upCoordinate(coordinate.getX(), coordinate.getY() - 2);
Coordinate downCoordinate(coordinate.getX(), coordinate.getY() + 2);
Square& left = generationMaze[leftCoordinate.getY()][leftCoordinate.getX()];
Square& right = generationMaze[rightCoordinate.getY()][rightCoordinate.getX()];
Square& up = generationMaze[upCoordinate.getY()][upCoordinate.getX()];
Square& down = generationMaze[downCoordinate.getY()][downCoordinate.getX()];
if (leftCoordinate != lastVisited && !left.isWall()) {
if (left.isVisited) {
if (!isBacktracking) {
generationMaze[leftCoordinate.getY()][leftCoordinate.getX() + 1].setWall(true);
generationMaze[leftCoordinate.getY() - 1][leftCoordinate.getX() + 1].setWall(true);
generationMaze[leftCoordinate.getY() + 1][leftCoordinate.getX() + 1].setWall(true);
}
} else {
availableNextMoves.push_back(leftCoordinate);
}
}
if (rightCoordinate != lastVisited && !right.isWall()) {
if (right.isVisited) {
if (!isBacktracking) {
generationMaze[rightCoordinate.getY()][rightCoordinate.getX() - 1].setWall(true);
generationMaze[rightCoordinate.getY() - 1][rightCoordinate.getX() - 1].setWall(true);
generationMaze[rightCoordinate.getY() + 1][rightCoordinate.getX() - 1].setWall(true);
}
} else {
availableNextMoves.push_back(rightCoordinate);
}
}
if (upCoordinate != lastVisited && !up.isWall()) {
if (up.isVisited) {
if (!isBacktracking) {
generationMaze[upCoordinate.getY() + 1][upCoordinate.getX()].setWall(true);
generationMaze[upCoordinate.getY() + 1][upCoordinate.getX() - 1].setWall(true);
generationMaze[upCoordinate.getY() + 1][upCoordinate.getX() + 1].setWall(true);
}
} else {
availableNextMoves.push_back(upCoordinate);
}
}
if (downCoordinate != lastVisited && !down.isWall()) {
if (down.isVisited) {
if (!isBacktracking) {
generationMaze[downCoordinate.getY() - 1][downCoordinate.getX()].setWall(true);
generationMaze[downCoordinate.getY() - 1][downCoordinate.getX() - 1].setWall(true);
generationMaze[downCoordinate.getY() - 1][downCoordinate.getX() + 1].setWall(true);
}
} else {
availableNextMoves.push_back(downCoordinate);
}
}
return availableNextMoves;
}