-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
348 lines (316 loc) · 8.24 KB
/
Maze.cpp
File metadata and controls
348 lines (316 loc) · 8.24 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
#include<iostream>
#include<stack>
#include<list>
#include<random>
#include<time.h>
#include<windows.h>
#include "Maze.h"
#include "Location.h"
using std::cout;
using std::endl;
Maze::Maze()
{
generateMaze();
placeStartPosition();
generatePaths();
placeDestination();
}
void Maze::generateMaze()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
maze[i][j] = WALL;
}
}
}
void Maze::placeStartPosition()
{
srand((unsigned int)time(NULL));
// rand() % COLUMNS shrinks the random number to be an allowable maze width.
int randomColumn = (int)rand() % COLUMNS;
// The if-else statements are used to ensure that randomColumn is an odd number that is not the edge of the maze.
if (randomColumn % 2 == 0 && randomColumn != COLUMNS - 1 || randomColumn == 0)
{
randomColumn++;
}
else if (randomColumn == COLUMNS - 1)
{
randomColumn--;
}
// rand() % ROWS shrinks the random number to be an allowable maze height.
int randomRow = (int)rand() % ROWS;
// The if-else statements are used to ensure that randomRow is an odd number that is not the edge of the maze.
if (randomRow % 2 == 0 && randomRow != ROWS - 1 || randomRow == 0)
{
randomRow++;
}
else if (randomRow == ROWS - 1)
{
randomRow--;
}
startLocation = Location(randomRow, randomColumn);
playerLocation = startLocation;
maze[startLocation.row][startLocation.column] = START;
}
void Maze::generatePaths()
{
Location currentLocation = startLocation;
std::stack <Location> locationStack;
do
{
// Instead of looping through each possible moves, a list of possible moves
// is created in a random order and the front of the list is checked to
// decide which move to perform.
std::list <moveDirection> possibleMoves = {};
listPossibleMoves(possibleMoves, currentLocation);
if (!possibleMoves.empty())
{
locationStack.push(currentLocation);
peformRandomPossibleMove(possibleMoves, currentLocation);
}
else
{
if (!locationStack.empty())
{
currentLocation = locationStack.top();
locationStack.pop();
}
}
} while (!locationStack.empty());
}
void Maze::listPossibleMoves(list<moveDirection>& possibleMoves, Location currentLocation)
{
// Make a list of all possible moves in a random order by either
// inserting a move into the front or back of the possible moves list
// A valid move is two steps away and is a wall character
if (currentLocation.column + 2 < COLUMNS && maze[currentLocation.row][currentLocation.column + 2] == WALL)
{
int randomInsertionSelection = rand() % 2;
if (randomInsertionSelection == 0)
{
possibleMoves.push_front(RIGHT);
}
else
{
possibleMoves.push_back(RIGHT);
}
}
if (currentLocation.column - 2 > 0 && maze[currentLocation.row][currentLocation.column - 2] == WALL)
{
int randomInsertionSelection = rand() % 2;
if (randomInsertionSelection == 0)
{
possibleMoves.push_front(LEFT);
}
else
{
possibleMoves.push_back(LEFT);
}
}
if (currentLocation.row + 2 < ROWS && maze[currentLocation.row + 2][currentLocation.column] == WALL)
{
int randomInsertionSelection = rand() % 2;
if (randomInsertionSelection == 0)
{
possibleMoves.push_front(DOWN);
}
else
{
possibleMoves.push_back(DOWN);
}
}
if (currentLocation.row - 2 > 0 && maze[currentLocation.row - 2][currentLocation.column] == WALL)
{
int randomInsertionSelection = rand() % 2;
if (randomInsertionSelection == 0)
{
possibleMoves.push_front(UP);
}
else
{
possibleMoves.push_back(UP);
}
}
}
void Maze::peformRandomPossibleMove(list<moveDirection> possibleMoves, Location& currentLocation)
{
// Decide which direction to move by getting the first move in the list of possible moves
switch (possibleMoves.front())
{
case RIGHT:
currentLocation.column++;
maze[currentLocation.row][currentLocation.column] = PATH;
currentLocation.column++;
maze[currentLocation.row][currentLocation.column] = PATH;
break;
case LEFT:
currentLocation.column--;
maze[currentLocation.row][currentLocation.column] = PATH;
currentLocation.column--;
maze[currentLocation.row][currentLocation.column] = PATH;
break;
case DOWN:
currentLocation.row++;
maze[currentLocation.row][currentLocation.column] = PATH;
currentLocation.row++;
maze[currentLocation.row][currentLocation.column] = PATH;
break;
case UP:
currentLocation.row--;
maze[currentLocation.row][currentLocation.column] = PATH;
currentLocation.row--;
maze[currentLocation.row][currentLocation.column] = PATH;
break;
}
}
void Maze::placeDestination()
{
// The destination symbol will be placed at a random location in a path.
int randomRow = rand() % ROWS;
int randomColumn = rand() % COLUMNS;
while (maze[randomRow][randomColumn] != PATH)
{
randomRow = rand() % ROWS;
randomColumn = rand() % COLUMNS;
}
destination.row = randomRow;
destination.column = randomColumn;
maze[randomRow][randomColumn] = DESTINATION;
}
void Maze::displayMaze()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (maze[i][j] == START)
{
// 10 sets the start character to be bright green.
SetConsoleTextAttribute(hConsole, 10);
}
else if (maze[i][j] == DESTINATION)
{
// 14 sets the destination character to be yellow.
SetConsoleTextAttribute(hConsole, 14);
}
else
{
// 7 sets the output to light grey.
SetConsoleTextAttribute(hConsole, 7);
}
cout << maze[i][j];
}
cout << endl;
}
}
void Maze::processKeyInputToMovePlayer()
{
bool atDestination = false;
while (!atDestination)
{
Location previousLocation = playerLocation;
switch (getKey())
{
case VK_LEFT:
if (maze[playerLocation.row][playerLocation.column - 1] != WALL)
{
playerLocation.column--;
placePlayer(playerLocation);
fixPath(previousLocation);
fixStart(startLocation);
}
break;
case VK_RIGHT:
if (maze[playerLocation.row][playerLocation.column + 1] != WALL)
{
playerLocation.column++;
placePlayer(playerLocation);
fixPath(previousLocation);
fixStart(startLocation);
}
break;
case VK_UP:
if (maze[playerLocation.row - 1][playerLocation.column] != WALL)
{
playerLocation.row--;
placePlayer(playerLocation);
fixPath(previousLocation);
fixStart(startLocation);
}
break;
case VK_DOWN:
if (maze[playerLocation.row + 1][playerLocation.column] != WALL)
{
playerLocation.row++;
placePlayer(playerLocation);
fixPath(previousLocation);
fixStart(startLocation);
}
break;
}
if (playerLocation.row == destination.row && playerLocation.column == destination.column)
{
atDestination = true;
}
}
for (int i = 0; i < 50; i++)
{
cout << "\n";
}
cout << "*************************" << endl;
cout << "YAY! YOU SOLVED THE MAZE!" << endl;
cout << "*************************" << endl;
}
int Maze::getKey()
{
int result = 0;
short MAX_SHORT = 0x7FFF; //111111111111111
if (GetAsyncKeyState(VK_LEFT) & MAX_SHORT)
{
result = VK_LEFT;
}
else if (GetAsyncKeyState(VK_UP) & MAX_SHORT)
{
result = VK_UP;
}
else if (GetAsyncKeyState(VK_RIGHT) & MAX_SHORT)
{
result = VK_RIGHT;
}
else if (GetAsyncKeyState(VK_DOWN) & MAX_SHORT)
{
result = VK_DOWN;
}
return result;
}
void Maze::placePlayer(Location location)
{
COORD playerCoordinate = {location.column, location.row};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), playerCoordinate);
// Hide the console cursor
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
cursorInfo.bVisible = false; // Set the cursor visibility
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
// 11 sets the Player symbol to bright blue.
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
cout << PLAYER;
}
void Maze::fixPath(Location location)
{
COORD coordinate = { location.column,location.row };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordinate);
maze[location.row][location.column] = PATH;
cout << PATH;
}
void Maze::fixStart(Location location)
{
COORD coordinate = { location.column, location.row };
// A value of 10 makes the Start symbol bright green
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordinate);
cout << START;
}