forked from OPCODE-Open-Spring-Fest/CPP_Mini_Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3DMAZE_GeneratorSolver.cpp
More file actions
175 lines (151 loc) · 4.43 KB
/
3DMAZE_GeneratorSolver.cpp
File metadata and controls
175 lines (151 loc) · 4.43 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
#include <bits/stdc++.h>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <ctime>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
const int SIZE = 10;
int delay_ms = 120;
bool visited[SIZE * SIZE];
bool walls[SIZE * SIZE][2] = {0};
int startPos = 0;
int goalPos = SIZE * SIZE - 1;
int path[SIZE * SIZE] = {0};
int pos = startPos;
int rot = 0;
int pathIndex = 0;
// ------------------------------------------------------------
void drawMazeConsole(bool showPath = false, bool clearScreen = true) {
if (clearScreen) {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
cout << "\n";
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE; i++) {
cout << "+";
if (walls[i + j * SIZE][1])
cout << " ";
else
cout << "---";
}
cout << "+\n";
for (int i = 0; i < SIZE; i++) {
if (walls[i + j * SIZE][0])
cout << " ";
else
cout << "|";
int position = i + j * SIZE;
if (position == startPos)
cout << " S ";
else if (position == goalPos)
cout << " G ";
else if (position == pos && showPath)
cout << " P ";
else if (showPath && path[position])
cout << " . ";
else
cout << " ";
}
cout << "|\n";
}
for (int i = 0; i < SIZE; i++) cout << "+---";
cout << "+\n";
cout.flush();
sleep_for(milliseconds(delay_ms));
}
// ------------------------------------------------------------
int nextCell(int pos, int size) {
int options[4], count = 0;
if (pos >= size && !visited[pos - size]) options[count++] = -size;
if ((pos + 1) % size != 0 && !visited[pos + 1]) options[count++] = 1;
if (pos < size * (size - 1) && !visited[pos + size]) options[count++] = size;
if (pos % size != 0 && !visited[pos - 1]) options[count++] = -1;
if (count == 0) return 0;
return options[rand() % count];
}
void connect(int pos1, int pos2) {
if (pos2 > pos1) {
if (pos2 == pos1 + 1) walls[pos2][0] = 1;
else walls[pos2][1] = 1;
} else {
if (pos1 == pos2 + 1) walls[pos1][0] = 1;
else walls[pos1][1] = 1;
}
}
void randomDFS(int pos, int size) {
visited[pos] = 1;
int next = pos + nextCell(pos, size);
while (next != pos) {
connect(pos, next);
pos = next;
drawMazeConsole(false);
randomDFS(pos, size);
next = pos + nextCell(pos, size);
}
}
// ------------------------------------------------------------
bool move() {
char dir;
if (abs(rot) % 4 == 0) dir = 'N';
else if (abs(rot) % 4 == 1) dir = 'E';
else if (abs(rot) % 4 == 2) dir = 'S';
else dir = 'W';
switch (dir) {
case 'E':
if ((pos + 1) % SIZE != 0 && walls[pos + 1][0]) { pos += 1; path[pos] = ++pathIndex; return true; }
return false;
case 'S':
if (pos < SIZE * (SIZE - 1) && walls[pos + SIZE][1]) { pos += SIZE; path[pos] = ++pathIndex; return true; }
return false;
case 'W':
if (pos % SIZE != 0 && walls[pos][0]) { pos -= 1; path[pos] = ++pathIndex; return true; }
return false;
case 'N':
if (pos >= SIZE && walls[pos][1]) { pos -= SIZE; path[pos] = ++pathIndex; return true; }
return false;
}
return false;
}
void solveMazeRightHand() {
path[0] = 1;
rot = 1;
pos = startPos;
while (pos != goalPos) {
rot++;
if (!move()) {
rot--;
if (!move()) {
rot--;
if (!move()) {
rot--;
move();
}
}
}
drawMazeConsole(true);
}
}
// ------------------------------------------------------------
int main() {
srand(time(0));
memset(visited, 0, sizeof(visited));
cout << "\nGenerating maze...\n";
randomDFS(0, SIZE);
cout << "\nMaze generation complete!\n";
drawMazeConsole(false, false); // show final maze once
sleep_for(seconds(2));
cout << "\nSolving maze (Right-Hand Rule)...\n";
solveMazeRightHand();
cout << "\nFinal solved maze:\n";
drawMazeConsole(true, false);
cout << "\nPress Enter to exit...";
cin.ignore();
cin.get();
return 0;
}