-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
285 lines (270 loc) · 11.8 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
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 <iostream>
#include <chrono>
#include <thread>
#include <filesystem>
#include "sorting.h"
#include <SFML/Graphics.hpp>
#include <mach-o/dyld.h>
#include <limits.h>
using namespace std;
// g++ *.cpp -I/opt/homebrew/Cellar/sfml/2.6.1/include/ -o Simulator -L/opt/homebrew/Cellar/sfml/2.6.1/lib/ -lsfml-graphics -lsfml-window -lsfml-system
// ./Simulator
// Default window sizes
#define WIDTH 700.0
#define HEIGHT 700.0
// Global variables
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Sorting");
sf::Font font;
Screen currentScreen = Screen::Home;
bool cursorOn = true;
bool paused = false;
/* Initialize a vector to have elements [1, n], where n == nums.size() */
void initialize(vector<int>& nums) {
for (int i = 0; i < nums.size(); ++i) {
nums[i] = i + 1;
}
shuffle(nums);
}
/* Show to the user that nums is sorted */
void finishedSorting(vector<int>& nums, string sortTitle = "") {
updateWindow(window, currentScreen, nums, font, cursorOn, -1, false, sortTitle);
// If the sorting stopped from user pausing
if (paused) {
paused = false;
return;
}
for (int i = 0; i < nums.size(); ++i) {
if (checkPause(window, paused)) {
updateWindow(window, currentScreen, nums, font, cursorOn, -1, false, sortTitle);
return;
}
updateWindow(window, currentScreen, nums, font, cursorOn, i, true, sortTitle);
this_thread::sleep_for(chrono::milliseconds(800 / nums.size()));
}
this_thread::sleep_for(chrono::milliseconds(500));
}
int main(int argc, char** argv) {
// Create and initialize all of the different vector sizes that can be used
vector<int> nums5(5, 0);
initialize(nums5);
vector<int> nums10(10, 0);
initialize(nums10);
vector<int> nums50(50, 0);
initialize(nums50);
vector<int> nums100(100, 0);
initialize(nums100);
vector<int> nums250(250, 0);
initialize(nums250);
vector<int> nums500(500, 0);
initialize(nums500);
vector<int> nums1000(1000, 0);
initialize(nums1000);
// Default nums to be a 250-element vector
vector<int> nums;
nums = nums250;
// Boolean for if window is still open
bool done = false;
// Get current path for use of loading font
char buf [PATH_MAX];
uint32_t bufsize = PATH_MAX;
_NSGetExecutablePath(buf, &bufsize);
string path = "";
int i = 0;
while (buf[i] != '\0') {
path += buf[i];
++i;
}
i = path.size() - 1;
while (path[i] != '/') {
--i;
}
path = path.substr(0, i);
// Load font for instructions
if (!font.loadFromFile(path + "/assets/" + "arial.ttf")) {
cout << "ERROR WITH TEXT" << endl;
}
// Slowing the program down for ease CPU and GPU
int waitMS = 30;
// Main program loop
while (window.isOpen()) {
switch (currentScreen) {
// Detect event
sf::Event event;
case Screen::Home:
while (window.pollEvent(event)) {
this_thread::sleep_for(chrono::milliseconds(waitMS));
if (event.type == sf::Event::Closed) {
window.close();
done = true;
} else if (event.type == sf::Event::KeyPressed) {
switch(event.key.scancode) {
// Close window
case sf::Keyboard::Scan::Escape:
window.close();
done = true;
break;
// Switch to Instructions
case sf::Keyboard::Scan::Right:
currentScreen = Screen::Instructions;
break;
// Switch to Sorting
case sf::Keyboard::Scan::Left:
currentScreen = Screen::Sorting;
break;
// Do nothing
default:
break;
}
}
}
break;
case Screen::Instructions:
while (window.pollEvent(event)) {
this_thread::sleep_for(chrono::milliseconds(waitMS));
if (event.type == sf::Event::Closed) {
window.close();
done = true;
} else if (event.type == sf::Event::KeyPressed) {
switch(event.key.scancode) {
// Close window
case sf::Keyboard::Scan::Escape:
window.close();
done = true;
break;
// Switch to Sorting
case sf::Keyboard::Scan::Right:
currentScreen = Screen::Sorting;
break;
// Switch to Home
case sf::Keyboard::Scan::Left:
currentScreen = Screen::Home;
break;
// Do nothing
default:
break;
}
}
}
break;
case Screen::Sorting:
while (window.pollEvent(event)) {
this_thread::sleep_for(chrono::milliseconds(waitMS));
// Close window
if (event.type == sf::Event::Closed) {
window.close();
done = true;
} else if (event.type == sf::Event::KeyPressed) {
// Detect which key is pressed
switch(event.key.scancode) {
// Close window
case sf::Keyboard::Scan::Escape:
window.close();
done = true;
break;
// Switch to Home
case sf::Keyboard::Scan::Right:
currentScreen = Screen::Home;
break;
// Switch to Instructions
case sf::Keyboard::Scan::Left:
currentScreen = Screen::Instructions;
break;
// Toggle Cursor
case sf::Keyboard::Scan::C:
cursorOn = !cursorOn;
break;
// Shuffle nums
case sf::Keyboard::Scan::S:
shuffle(nums);
break;
// Bubble Sort
case sf::Keyboard::Scan::B:
bubbleSort(nums, window, currentScreen, cursorOn, font, paused);
finishedSorting(nums, "BubbleSort");
break;
// Selection Sort
case sf::Keyboard::Scan::E:
selectionSort(nums, window, currentScreen, cursorOn, font, paused);
finishedSorting(nums, "SelectionSort");
break;
// Insertion Sort
case sf::Keyboard::Scan::I:
insertionSort(nums, window, currentScreen, cursorOn, font, paused);
finishedSorting(nums, "InsertionSort");
break;
// Merge Sort
case sf::Keyboard::Scan::M:
mergeSort(nums, 0, nums.size() - 1, window, currentScreen, cursorOn, font, paused);
finishedSorting(nums, "MergeSort");
break;
case sf::Keyboard::Scan::Q:
quickSort(nums, 0, nums.size() - 1, window, currentScreen, cursorOn, font, paused);
finishedSorting(nums, "QuickSort");
break;
// Bogo Sort
case sf::Keyboard::Scan::X:
bogoSort(nums, window, currentScreen, cursorOn, font, paused);
finishedSorting(nums, "BogoSort");
break;
case sf::Keyboard::Scan::Z:
slowSort(nums, 0, nums.size() - 1, window, currentScreen, cursorOn, font, paused);
finishedSorting(nums, "SlowSort");
break;
// Switch to 5-element vector
case sf::Keyboard::Scan::Num1:
nums = nums5;
shuffle(nums);
updateWindow(window, currentScreen, nums, font, cursorOn);
break;
// Switch to 10-element vector
case sf::Keyboard::Scan::Num2:
nums = nums10;
shuffle(nums);
updateWindow(window, currentScreen, nums, font, cursorOn);
break;
// Switch to 50-element vector
case sf::Keyboard::Scan::Num3:
nums = nums50;
shuffle(nums);
updateWindow(window, currentScreen, nums, font, cursorOn);
break;
// Switch to 100-element vector
case sf::Keyboard::Scan::Num4:
nums = nums100;
shuffle(nums);
updateWindow(window, currentScreen, nums, font, cursorOn);
break;
// Switch to 250-element vector
case sf::Keyboard::Scan::Num5:
nums = nums250;
shuffle(nums);
updateWindow(window, currentScreen, nums, font, cursorOn);
break;
// Switch to 500-element vector
case sf::Keyboard::Scan::Num6:
nums = nums500;
shuffle(nums);
updateWindow(window, currentScreen, nums, font, cursorOn);
break;
// Switch to 1000-element vector
case sf::Keyboard::Scan::Num7:
nums = nums1000;
shuffle(nums);
updateWindow(window, currentScreen, nums, font, cursorOn);
break;
// Do nothing
default:
break;
}
}
}
break;
}
// If window is still open, update it
if (!done) {
this_thread::sleep_for(chrono::milliseconds(waitMS));
updateWindow(window, currentScreen, nums, font, cursorOn);
}
}
return 0;
}