-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
77 lines (61 loc) · 2.61 KB
/
main.cpp
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
#include <SFML/Graphics.hpp>
#include <iostream>
#include "maze.h"
int main() {
std::srand(static_cast<unsigned>(std::time(nullptr)));
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Maze Generation and Solving");
Maze maze(WIDTH / CELL_SIZE, HEIGHT / CELL_SIZE);
sf::RectangleShape generateButton(sf::Vector2f(150, 30));
generateButton.setPosition(200, HEIGHT - 40); // Adjusted button position
generateButton.setFillColor(sf::Color::Blue);
sf::RectangleShape solveButton(sf::Vector2f(150, 30));
solveButton.setPosition(360, HEIGHT - 40); // Adjusted button position
solveButton.setFillColor(sf::Color::Green);
sf::Font font;
if (!font.loadFromFile("arial.ttf")) {
std::cerr << "Error loading font\n";
return 1;
}
sf::Text generateButtonText("Generate Maze", font, 16);
generateButtonText.setPosition(210, HEIGHT - 38);
generateButtonText.setFillColor(sf::Color::White);
sf::Text solveButtonText("Solve Maze", font, 16);
solveButtonText.setPosition(380, HEIGHT - 38);
solveButtonText.setFillColor(sf::Color::White);
bool visualizeSolutionFlag = false;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::MouseButtonPressed) {
// Check if the mouse click is within the generate button area
if (event.mouseButton.x >= 200 && event.mouseButton.x <= 350 &&
event.mouseButton.y >= HEIGHT - 40 && event.mouseButton.y <= HEIGHT - 10) {
maze.initialize();
visualizeSolutionFlag = false;
}
// Check if the mouse click is within the solve button area
if (event.mouseButton.x >= 360 && event.mouseButton.x <= 510 &&
event.mouseButton.y >= HEIGHT - 40 && event.mouseButton.y <= HEIGHT - 10) {
if (!visualizeSolutionFlag) {
maze.solveDFS();
visualizeSolutionFlag = true;
}
}
}
}
if (visualizeSolutionFlag) {
maze.visualizeSolution(window);
visualizeSolutionFlag = false; // Reset the flag after visualization
} else {
maze.draw(window);
window.draw(generateButton);
window.draw(generateButtonText);
window.draw(solveButton);
window.draw(solveButtonText);
}
window.display();
}
return 0;
}