-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
285 lines (250 loc) · 15.4 KB
/
main.cpp
File metadata and controls
285 lines (250 loc) · 15.4 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
#include "Menu.h"
#include "AlgorithmVisualizer.h"
#include "LinkedList.h"
#include "Stack.h"
#include "Queue.h"
#include "AVLTree.h"
#include "Dijkstra.h"
#include "Maze.h"
#include "GraphTraversal.h"
#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Data Structures and Algorithms");
sf::Font font;
font.loadFromFile("arial.ttf");
// Menú principal
Menu mainMenu({"Search Algorithms", "Sorting Algorithms", "Linear Structures", "Non-Linear Structures", "Exit"}, window.getSize().x, window.getSize().y);
AlgorithmVisualizer visualizer;
LinkedList linkedList;
Stack stack;
Queue queue;
AVLTree avlTree;
Dijkstra dijkstra(5);
GraphTraversal graph(10,10);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
mainMenu.moveUp();
} else if (event.key.code == sf::Keyboard::Down) {
mainMenu.moveDown();
} else if (event.key.code == sf::Keyboard::Enter) {
int mainMenuIndex = mainMenu.getSelectedIndex();
// Search Algorithms
if (mainMenuIndex == 0) {
Menu searchMenu({"Linear Search", "Binary Search", "Back"}, window.getSize().x, window.getSize().y);
bool inSearchMenu = true;
while (window.isOpen() && inSearchMenu) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
searchMenu.moveUp();
} else if (event.key.code == sf::Keyboard::Down) {
searchMenu.moveDown();
} else if (event.key.code == sf::Keyboard::Enter) {
int searchMenuIndex = searchMenu.getSelectedIndex();
if (searchMenuIndex == 0) { // Linear Search
visualizer.runLinearSearch(window, font);
} else if (searchMenuIndex == 1) { // Binary Search
visualizer.runBinarySearch(window, font);
} else if (searchMenuIndex == 2) { // Back
inSearchMenu = false;
}
} else if (event.key.code == sf::Keyboard::Escape) {
inSearchMenu = false; // Salir del submenú
}
}
}
if (!inSearchMenu) break;
window.clear();
searchMenu.draw(window, font);
window.display();
}
}
// Sorting Algorithms
else if (mainMenuIndex == 1) {
Menu sortingMenu({"Bubble Sort", "Selection Sort", "Insertion Sort", "Merge Sort", "Quick Sort", "Back"}, window.getSize().x, window.getSize().y);
bool inSortingMenu = true;
while (window.isOpen() && inSortingMenu) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
sortingMenu.moveUp();
} else if (event.key.code == sf::Keyboard::Down) {
sortingMenu.moveDown();
} else if (event.key.code == sf::Keyboard::Enter) {
int sortingMenuIndex = sortingMenu.getSelectedIndex();
if (sortingMenuIndex == 0) { // Bubble Sort
visualizer.runBubbleSort(window, font);
} else if (sortingMenuIndex == 1) { // Selection Sort
visualizer.runSelectionSort(window, font);
} else if (sortingMenuIndex == 2) { // Insertion Sort
visualizer.runInsertionSort(window, font);
} else if (sortingMenuIndex == 3) { // Merge Sort
visualizer.runMergeSort(window, font);
} else if (sortingMenuIndex == 4) { // Quick Sort
visualizer.runQuickSort(window, font);
} else if (sortingMenuIndex == 5) { // Back
inSortingMenu = false;
}
} else if (event.key.code == sf::Keyboard::Escape) {
inSortingMenu = false; // Salir del submenú
}
}
}
if (!inSortingMenu) break;
window.clear();
sortingMenu.draw(window, font);
window.display();
}
}
// Linear Structures
else if (mainMenuIndex == 2) {
Menu linearMenu({"Linked List", "Stack", "Queue", "Back"}, window.getSize().x, window.getSize().y);
bool inLinearMenu = true;
while (window.isOpen() && inLinearMenu) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
linearMenu.moveUp();
} else if (event.key.code == sf::Keyboard::Down) {
linearMenu.moveDown();
} else if (event.key.code == sf::Keyboard::Enter) {
int linearMenuIndex = linearMenu.getSelectedIndex();
if (linearMenuIndex == 0) { // Linked List
linkedList.interactiveMode(window, font);
} else if (linearMenuIndex == 1) { // Stack
stack.interactiveMode(window, font);
} else if (linearMenuIndex == 2) { // Queue
queue.interactiveMode(window, font);
} else if (linearMenuIndex == 3) { // Back
inLinearMenu = false;
}
} else if (event.key.code == sf::Keyboard::Escape) {
inLinearMenu = false; // Salir del submenú
}
}
}
if (!inLinearMenu) break;
window.clear();
linearMenu.draw(window, font);
window.display();
}
}
// Non-Linear Structures
else if (mainMenuIndex == 3) {
Menu nonLinearMenu({"AVL Tree", "Dijkstra's Algorithm", "Graph Traversal", "Back"}, window.getSize().x, window.getSize().y);
bool inNonLinearMenu = true;
while (window.isOpen() && inNonLinearMenu) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
nonLinearMenu.moveUp();
} else if (event.key.code == sf::Keyboard::Down) {
nonLinearMenu.moveDown();
} else if (event.key.code == sf::Keyboard::Enter) {
int nonLinearMenuIndex = nonLinearMenu.getSelectedIndex();
if (nonLinearMenuIndex == 0) { // AVL Tree
avlTree.interactiveMode(window, font);
} else if (nonLinearMenuIndex == 1) { // Dijkstra's Algorithm
Dijkstra dijkstra(5);
dijkstra.addEdge(0, 1, 4);
dijkstra.addEdge(1, 2, 1);
dijkstra.addEdge(2, 3, 4);
dijkstra.addEdge(3, 4, 2);
dijkstra.addEdge(0, 2, 5);
auto distances = std::vector<int>(5, std::numeric_limits<int>::max());
auto parents = dijkstra.calculateShortestPaths(0); //Fuente
dijkstra.visualizeGraph(window, font, parents, distances);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
} else if (event.type == sf::Event::KeyPressed) {
return 0; // Salir del algoritmo
}
}
}
} else if (nonLinearMenuIndex == 2) { // Graph Traversal
Menu graphMenu({"DFS", "BFS", "Back"}, window.getSize().x, window.getSize().y);
bool inGraphTraversal = true;
while (window.isOpen() && inGraphTraversal) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
graphMenu.moveUp();
} else if (event.key.code == sf::Keyboard::Down) {
graphMenu.moveDown();
} else if (event.key.code == sf::Keyboard::Enter) {
int graphMenuIndex = graphMenu.getSelectedIndex();
if (graphMenuIndex == 0) { // DFS
graph.generateMaze();
auto dfsOrder = graph.depthFirstSearch(0);
graph.visualizeMaze(window, font, dfsOrder, true);
} else if (graphMenuIndex == 1) { // BFS
graph.generateMaze();
auto bfsOrder = graph.breadthFirstSearch(0);
graph.visualizeMaze(window, font, bfsOrder, false);
} else if (graphMenuIndex == 2) { // Back
inGraphTraversal = false;
}
} else if (event.key.code == sf::Keyboard::Escape) {
inGraphTraversal = false; // Salir del submenú
}
}
}
if (!inGraphTraversal) break;
window.clear();
graphMenu.draw(window, font);
window.display();
}
} else if (nonLinearMenuIndex == 3) { // Back
inNonLinearMenu = false;
}
} else if (event.key.code == sf::Keyboard::Escape) {
inNonLinearMenu = false; // Salir del submenú
}
}
}
if (!inNonLinearMenu) break;
window.clear();
nonLinearMenu.draw(window, font);
window.display();
}
}
// Exit
else if (mainMenuIndex == 4) {
window.close();
}
} else if (event.key.code == sf::Keyboard::Escape) {
window.close();
}
}
}
window.clear();
mainMenu.draw(window, font);
window.display();
}
return 0;
}