-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (58 loc) · 2.07 KB
/
main.cpp
File metadata and controls
72 lines (58 loc) · 2.07 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
#include "raylib.h"
#include <vector>
#include <random>
#include <thread>
#include <chrono>
void DrawArray(const std::vector<int>& array, int highlightedIndex = -1) {
int screenWidth = GetScreenWidth();
int screenHeight = GetScreenHeight();
int barWidth = screenWidth / array.size();
for(int i = 0; i < array.size(); i++) {
Color barColor = GRAY;
if(i == highlightedIndex) {
barColor = RED;
}
DrawRectangle(i * barWidth, screenHeight - array[i], barWidth, array[i], barColor);
}
}
void BubbleSortVisualized(std::vector<int>& array, Sound swapSound) {
int n = array.size();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - i - 1; j++) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawArray(array, j); // Draw random array and sort
EndDrawing();
PlaySound(swapSound); // Plays sound when changed highlighted bar
//Delay for 50ms
std::this_thread::sleep_for(std::chrono::milliseconds(1));
// Swapping
if(array[j] > array[j + 1]) {
std::swap(array[j], array[j + 1]);
}
}
}
}
int main(){
/* Window initialization */
InitWindow(800, 600, "Bubble sort visualisation");
InitAudioDevice(); // Initializing audio
Sound swapSound = LoadSound("res/sound.wav"); // Adding sound file
/* Creating random list */
std::random_device rd; // Source of randomness
std::mt19937 gen(rd()); // Generator random number of Mersenne Twister
std::uniform_int_distribution<> randomRangeGenerator(0, GetScreenHeight()); // Range of value
std::vector<int> array(100);
for(int& val : array) {
val = randomRangeGenerator(gen); // Random number
}
/* Main Loop */
while(!WindowShouldClose()) {
BubbleSortVisualized(array, swapSound); // Sort with visualization
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // Before end, watch how good ;)
break;
}
/* End of ray-lib */
CloseWindow();
return 0;
}