-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.py
More file actions
132 lines (98 loc) · 4.42 KB
/
program.py
File metadata and controls
132 lines (98 loc) · 4.42 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
import pygame
import random
import sys
from visualization import Visualization
from draw_info import DrawInfo
from sorting_algorithms import SortingAlgorithms
TRANSITION_SOUND = pygame.mixer.Sound("sounds.mp3")
class Program:
def __init__(self):
pygame.mixer.init()
pygame.init()
self.visual = Visualization()
self.algo = SortingAlgorithms()
self.driver_program()
def play_sound(self) -> None:
if not pygame.mixer.get_busy():
TRANSITION_SOUND.play(-1) # Play sound in loop
def stop_sound(self) -> None:
if pygame.mixer.get_busy():
pygame.mixer.stop()
def generate_starting_list(self, n, min_value, max_value) -> list:
return [random.randint(min_value, max_value) for _ in range(n)]
def driver_program(self):
run = True
clock = pygame.time.Clock()
# -> PARAMETERS:
N = 1000
MIN_VALUE = 0
MAX_VALUE = 500
random_list = self.generate_starting_list(N, MIN_VALUE, MAX_VALUE)
draw_info = DrawInfo(width=1280, height=650, list=random_list)
sorting = False
ascending = True
sorting_algorithm_name = "Bubble Sort"
sorting_algorithm = self.algo.bubble_sort
sorting_algorithm_generator = None
while run:
# clock.tick(60)
if sorting:
try:
next(sorting_algorithm_generator)
except StopIteration:
sorting = False
self.stop_sound() # Stops sound when sorting ends
else:
self.visual.draw(draw_info, sorting_algorithm_name, ascending)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type != pygame.KEYDOWN:
continue
elif event.key == pygame.K_r:
random_list = self.generate_starting_list(N, MIN_VALUE, MAX_VALUE)
draw_info.set_list(random_list)
sorting = False
self.stop_sound()
elif event.key == pygame.K_SPACE and not sorting:
self.play_sound()
sorting = True
sorting_algorithm_generator = sorting_algorithm(
draw_info, ascending
)
elif event.key == pygame.K_a and not sorting:
ascending = True
elif event.key == pygame.K_d and not sorting:
ascending = False
elif event.key == pygame.K_b and not sorting:
sorting_algorithm = self.algo.bubble_sort
sorting_algorithm_name = "Bubble Sort"
elif event.key == pygame.K_i and not sorting:
sorting_algorithm = self.algo.insertion_sort
sorting_algorithm_name = "Insertion Sort"
elif event.key == pygame.K_s and not sorting:
sorting_algorithm = self.algo.selection_sort
sorting_algorithm_name = "Selection Sort"
elif event.key == pygame.K_m and not sorting:
sorting_algorithm = self.algo.merge_sort
sorting_algorithm_name = "Merge Sort"
elif event.key == pygame.K_q and not sorting:
sorting_algorithm = self.algo.quick_sort
sorting_algorithm_name = "Quick Sort"
elif event.key == pygame.K_h and not sorting:
sorting_algorithm = self.algo.heap_sort
sorting_algorithm_name = "Heap Sort"
elif event.key == pygame.K_c and not sorting:
sorting_algorithm = self.algo.counting_sort
sorting_algorithm_name = "Counting Sort"
elif event.key == pygame.K_x and not sorting:
sorting_algorithm = self.algo.radix_sort
sorting_algorithm_name = "Radix Sort"
elif event.key == pygame.K_k and not sorting:
sorting_algorithm = self.algo.comb_sort
sorting_algorithm_name = "Comb Sort"
elif event.key == pygame.K_l and not sorting:
sorting_algorithm = self.algo.shell_sort
sorting_algorithm_name = "Shell Sort"
pygame.quit()
sys.exit()