-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
325 lines (268 loc) · 10.5 KB
/
main.cpp
File metadata and controls
325 lines (268 loc) · 10.5 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
#include "Maze.h"
#include <iostream>
#include <string>
#include <chrono>
#include <limits>
/**
* Display the main menu
*/
void displayMenu() {
std::cout << "\n" << std::string(50, '=') << "\n";
std::cout << " RECURSIVE MAZE GENERATOR\n";
std::cout << std::string(50, '=') << "\n";
std::cout << "1. Generate maze (Iterative - Stack-based)\n";
std::cout << "2. Generate maze (Recursive)\n";
std::cout << "3. Generate custom size maze\n";
std::cout << "4. Generate maze with custom seed\n";
std::cout << "5. Show maze in ASCII format\n";
std::cout << "6. Show detailed maze information\n";
std::cout << "7. Solve current maze\n";
std::cout << "8. Generate multiple mazes comparison\n";
std::cout << "9. Performance test\n";
std::cout << "A. Check maze connectivity (debug)\n";
std::cout << "0. Exit\n";
std::cout << std::string(50, '=') << "\n";
std::cout << "Choose an option: ";
}
/**
* Get integer input with validation
*/
int getIntInput(const std::string& prompt, int min = 0, int max = 1000) {
int value;
while (true) {
std::cout << prompt;
if (std::cin >> value && value >= min && value <= max) {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return value;
} else {
std::cout << "Invalid input. Please enter a number between "
<< min << " and " << max << ".\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}
/**
* Generate and display a basic maze
*/
void generateBasicMaze(Maze& maze, bool useRecursive = false) {
std::cout << "\nGenerating maze using "
<< (useRecursive ? "recursive" : "iterative")
<< " algorithm...\n";
auto start = std::chrono::high_resolution_clock::now();
if (useRecursive) {
maze.generateMazeRecursive();
} else {
maze.generateMazeIterative();
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "Generation completed in " << duration.count() << " microseconds.\n";
maze.printMaze();
}
/**
* Generate maze with custom dimensions
*/
void generateCustomMaze(Maze ¤tMaze) {
int width = getIntInput("Enter maze width (3-50): ", 3, 50);
int height = getIntInput("Enter maze height (3-50): ", 3, 50);
Maze customMaze(width, height);
std::cout << "\nChoose algorithm:\n";
std::cout << "1. Iterative (Stack-based)\n";
std::cout << "2. Recursive\n";
int choice = getIntInput("Choice (1-2): ", 1, 2);
generateBasicMaze(customMaze, choice == 2);
// Replace the currentMaze with the newly generated maze so option 7
// (solve current maze) operates on the maze the user just created.
currentMaze = std::move(customMaze);
}
/**
* Generate maze with custom seed
*/
void generateSeededMaze(Maze ¤tMaze) {
int width = getIntInput("Enter maze width (3-30): ", 3, 30);
int height = getIntInput("Enter maze height (3-30): ", 3, 30);
unsigned int seed = getIntInput("Enter seed value: ", 0, 999999);
Maze seededMaze(width, height, seed);
std::cout << "\nChoose algorithm:\n";
std::cout << "1. Iterative (Stack-based)\n";
std::cout << "2. Recursive\n";
int choice = getIntInput("Choice (1-2): ", 1, 2);
std::cout << "\nUsing seed: " << seed << "\n";
generateBasicMaze(seededMaze, choice == 2);
// Make the seeded maze the current maze for subsequent operations
currentMaze = std::move(seededMaze);
}
/**
* Compare multiple maze generation algorithms
*/
void compareMazes() {
int width = getIntInput("Enter maze width for comparison (5-20): ", 5, 20);
int height = getIntInput("Enter maze height for comparison (5-20): ", 5, 20);
std::cout << "\nGenerating mazes for comparison...\n";
// Iterative maze
Maze iterativeMaze(width, height, 12345);
std::cout << "\n--- ITERATIVE (STACK-BASED) ALGORITHM ---";
generateBasicMaze(iterativeMaze, false);
// Recursive maze
Maze recursiveMaze(width, height, 12345);
std::cout << "\n--- RECURSIVE ALGORITHM ---";
generateBasicMaze(recursiveMaze, true);
std::cout << "\nNote: Both mazes use the same seed (12345) for comparison.\n";
}
/**
* Performance testing
*/
void performanceTest() {
std::cout << "\nPerformance Test - Maze Generation Times\n";
std::cout << std::string(45, '-') << "\n";
std::vector<std::pair<int, int>> sizes = {{10, 10}, {20, 20}, {30, 30}, {40, 40}};
for (const auto& size : sizes) {
int width = size.first;
int height = size.second;
std::cout << "Testing " << width << "x" << height << " maze:\n";
// Test iterative algorithm
Maze iterativeMaze(width, height);
auto start = std::chrono::high_resolution_clock::now();
iterativeMaze.generateMazeIterative();
auto end = std::chrono::high_resolution_clock::now();
auto iterativeTime = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// Test recursive algorithm
Maze recursiveMaze(width, height);
start = std::chrono::high_resolution_clock::now();
recursiveMaze.generateMazeRecursive();
end = std::chrono::high_resolution_clock::now();
auto recursiveTime = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << " Iterative: " << std::setw(8) << iterativeTime.count() << " μs\n";
std::cout << " Recursive: " << std::setw(8) << recursiveTime.count() << " μs\n";
std::cout << " Difference: " << std::setw(7) << (recursiveTime.count() - iterativeTime.count()) << " μs\n\n";
}
}
/**
* Demonstrate maze solving
*/
void solveMazeDemo(Maze& maze) {
std::cout << "\nMaze Solving Demo\n";
std::cout << "Attempting to solve maze from (0,0) to ("
<< (maze.getWidth()-1) << "," << (maze.getHeight()-1) << ")...\n";
auto start = std::chrono::high_resolution_clock::now();
bool solved = maze.solveMaze();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
if (solved) {
std::cout << "Maze solved in " << duration.count() << " microseconds!\n";
} else {
std::cout << "Maze could not be solved.\n";
}
}
/**
* Main program loop
*/
int main() {
std::cout << "Welcome to the Recursive Maze Generator!\n";
std::cout << "This program demonstrates various maze generation algorithms.\n";
// Default maze
Maze currentMaze(10, 10);
currentMaze.generateMazeIterative(); // Generate initial maze
int choice;
do {
displayMenu();
std::string input;
std::cin >> input;
// Handle both numeric and letter input
if (input == "A" || input == "a") {
choice = 10; // Use 10 for connectivity check
} else {
try {
choice = std::stoi(input);
} catch (...) {
choice = -1; // Invalid input
}
}
switch (choice) {
case 1:
currentMaze = Maze(10, 10);
generateBasicMaze(currentMaze, false);
break;
case 2:
currentMaze = Maze(10, 10);
generateBasicMaze(currentMaze, true);
break;
case 3:
generateCustomMaze(currentMaze);
break;
case 4:
generateSeededMaze(currentMaze);
break;
case 5:
std::cout << "\nASCII representation of current maze:\n";
currentMaze.printMazeASCII();
break;
case 6:
currentMaze.printMazeDetailed();
break;
case 7:
solveMazeDemo(currentMaze);
break;
case 8:
compareMazes();
break;
case 9:
performanceTest();
break;
case 10:
std::cout << "\nChecking maze connectivity...\n";
currentMaze.isMazeConnected();
break;
case 0:
std::cout << "\nThank you for using the Recursive Maze Generator!\n";
std::cout << "Goodbye!\n";
break;
default:
std::cout << "Invalid option. Please try again.\n";
}
if (choice != 0) {
std::cout << "\nPress Enter to continue...";
// Clear any leftover input (e.g., newline from previous >> reads)
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
}
} while (choice != 0);
return 0;
}
/**
* Additional utility functions for demonstration
*/
namespace MazeUtils {
/**
* Create a demo showcasing different maze sizes
*/
void sizeDemonstration() {
std::cout << "\n=== SIZE DEMONSTRATION ===\n";
std::vector<std::pair<int, int>> sizes = {{5, 5}, {8, 8}, {12, 12}};
for (const auto& size : sizes) {
Maze demo(size.first, size.second);
demo.generateMazeIterative();
std::cout << "\nMaze " << size.first << "x" << size.second << ":\n";
demo.printMaze();
}
}
/**
* Show algorithm complexity information
*/
void showComplexityInfo() {
std::cout << "\n=== ALGORITHM COMPLEXITY ===\n";
std::cout << "Iterative Depth-First Search (Stack-based):\n";
std::cout << " Time Complexity: O(n)\n";
std::cout << " Space Complexity: O(n)\n";
std::cout << " Where n = width × height\n\n";
std::cout << "Recursive Depth-First Search:\n";
std::cout << " Time Complexity: O(n)\n";
std::cout << " Space Complexity: O(n) - due to recursion stack\n";
std::cout << " Where n = width × height\n\n";
std::cout << "Both algorithms ensure:\n";
std::cout << " - Every cell is visitable\n";
std::cout << " - Only one path between any two cells\n";
std::cout << " - Perfect maze generation (no loops)\n";
}
}