Skip to content

Commit

Permalink
Added statistics at bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
ProfessorAtomicManiac committed Sep 18, 2022
1 parent 9330905 commit 42fbd9a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Algorithms/Sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def bucketSort(arr, event, digit, aux):
aux.list = []
for i in range(length):
buckets.append([])
aux.setList(length, arr.length())
aux.setList(length, 0, arr.length())


for i in range(arr.length()):
Expand Down
17 changes: 17 additions & 0 deletions UI/Arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def __init__(self, array_group, dim, coords, beg, end, midi, delay = default_del

self.saved_config = [size, beg, end]
self.saved_list = [i for i in range(1, self.end+1)]

self.access = 0
self.swaps = 0
self.changes = 0

def change_delay(self, delay):
try:
Expand Down Expand Up @@ -145,6 +149,9 @@ def load_config(self):
self.createList()

def createList(self):
self.access = 0
self.swaps = 0
self.changes = 0
width = self.dim[0]/len(self.list)
y = self.coords[1] + self.dim[1]
height_unit = self.dim[1] / (self.end - self.beg)
Expand Down Expand Up @@ -172,6 +179,9 @@ def getList(self):
# TODO: As part of the challenge, code ur own shuffle method
def shuffle(self):
random.shuffle(self.list)
self.access = 0
self.swaps = 0
self.changes = 0

def generate(self):
self.array_group.empty()
Expand All @@ -186,17 +196,21 @@ def play(self, index):
self.midi.play(int((self.list[index] - self.beg + 1)/(self.end - self.beg + 1)*127))

def get(self, index):
self.access += 1
self.visited[index] = True
self.play(index)
time.sleep(self.delay)
return self.list[index]

def replace(self, index, val):
self.access += 1
self.changes += 1
self.visited[index] = True
self.play(index)
time.sleep(self.delay)
self.list[index] = val

# No sorting algos actually use these, probably initialization
def insert(self, index, val):
self.list.insert(index, val)
self.visited.insert(index, True)
Expand All @@ -214,6 +228,9 @@ def remove(self, index):
self.createList()

def swap(self, ind1, ind2):
self.access += 2
self.changes += 2
self.swaps += 1
if (ind1 == ind2):
return
self.visited[ind1] = True
Expand Down
39 changes: 35 additions & 4 deletions UI/UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,15 @@ def __init__(self, window, screen_group, sorting_group, aux_sorting_group, scrol
text_box_group.empty()
# Constants
self.TITLE_Y = 50

# Text at the bottom of the screen
self.TITLE_HEIGHT = 100
# Margins for all boxes
self.MARGIN_X = 25
self.MARGIN_Y = 10
# Calculations for certain values
self.SORTING_WIDTH = (window.window.get_size()[0] - 3*self.MARGIN_X)*(3/4) # Ideally is ~800
self.SORTING_HEIGHT = (window.window.get_size()[1] - 2*self.TITLE_Y - 2*self.MARGIN_Y)
self.SORTING_HEIGHT = (window.window.get_size()[1] - 2*self.TITLE_Y - 2*self.MARGIN_Y - self.TITLE_HEIGHT)
self.CONFIG_WIDTH = 300
#CONFIG_X = SORTING_MARGIN_X
#CONFIG_Y = SORTING_HEIGHT + 2*SORTING_MARGIN_Y
Expand All @@ -176,7 +179,7 @@ def __init__(self, window, screen_group, sorting_group, aux_sorting_group, scrol
self.file = None

self.SORTING_X = (window.window.get_size()[0] - self.CONFIG_WIDTH - 2*self.MARGIN_X)/2 + self.MARGIN_X
self.SORTING_Y = 2*self.TITLE_Y + self.MARGIN_Y + (window.window.get_size()[1] - 2*self.TITLE_Y - 2*self.MARGIN_Y)/2
self.SORTING_Y = 2*self.TITLE_Y + self.MARGIN_Y + (window.window.get_size()[1] - 2*self.TITLE_Y - 2*self.MARGIN_Y - self.TITLE_HEIGHT)/2
self.sorting_group = sorting_group
self.aux_sorting_group = aux_sorting_group
sorting_group.empty()
Expand All @@ -189,8 +192,25 @@ def __init__(self, window, screen_group, sorting_group, aux_sorting_group, scrol

self.handled = 0

self.accessed = None
self.changes = None
self.aux_count = None
self.swaps = None

def update(self):
if (self.accessed != None):
#print("Accessed Array Count: {}".format(self.array.access))
self.accessed.change_text(Wrapper.DefaultText.text("Accessed Array Count: {}".format(self.array.access), Wrapper.FontSizes.BUTTON_SIZE))
if (self.changes != None):
self.changes.change_text(Wrapper.DefaultText.text("Changes: {}".format(self.array.changes), Wrapper.FontSizes.BUTTON_SIZE))
if (self.aux_count != None and self.aux_array != None):
self.aux_count.change_text(Wrapper.DefaultText.text("Accessed Auxillary Array Count: {}".format(self.aux_array.access), Wrapper.FontSizes.BUTTON_SIZE))
else:
self.aux_count.change_text(Wrapper.DefaultText.text("", Wrapper.FontSizes.BUTTON_SIZE))

if (self.swaps != None):
self.swaps.change_text(Wrapper.DefaultText.text("Swaps: {}".format(self.array.swaps), Wrapper.FontSizes.BUTTON_SIZE))

# self.scroll_bar[0] = reset scroll
# self.scroll_bar[1] = sort scroll
# Glitch where it will click the button behind it
Expand Down Expand Up @@ -261,9 +281,9 @@ def display_sorting(self, screen_group, options_screen_group, slide_in = True):
self.scroll_group[0].empty()
self.scroll_group[1].empty()
self.SORTING_WIDTH = (3*self.window.window.get_size()[0]/4 - 2*self.MARGIN_X) # Ideally is ~800
self.SORTING_HEIGHT = (self.window.window.get_size()[1] - 2*self.TITLE_Y - 2*self.MARGIN_Y)
self.SORTING_HEIGHT = (self.window.window.get_size()[1] - 2*self.TITLE_Y - 2*self.MARGIN_Y - self.TITLE_HEIGHT)
self.SORTING_X = (3*self.window.window.get_size()[0]/4 - 2*self.MARGIN_X)/2 + self.MARGIN_X
self.SORTING_Y = 2*self.TITLE_Y + self.MARGIN_Y + (self.window.window.get_size()[1] - 2*self.TITLE_Y - 2*self.MARGIN_Y)/2
self.SORTING_Y = 2*self.TITLE_Y + self.MARGIN_Y + (self.window.window.get_size()[1] - 2*self.TITLE_Y - 2*self.MARGIN_Y - self.TITLE_HEIGHT)/2
#print(self.SORTING_X, self.SORTING_Y, self.SORTING_WIDTH, self.SORTING_HEIGHT)
self.array = Array(self.sorting_group, (self.SORTING_WIDTH, self.SORTING_HEIGHT), (self.MARGIN_X, self.SORTING_Y - self.SORTING_HEIGHT/2), 1, self.array_length, self.midi)

Expand Down Expand Up @@ -315,15 +335,26 @@ def func():
self.scroll_bar.append(Wrapper.ScrollBar(buttons, ((5*self.window.window.get_size()[0]/6), button_col_top + 2 * button_margin), (200, 50), self.scroll_group[1], self.window, "Choose Sorted", slide_in))
# Options button

self.accessed = Wrapper.Text(Wrapper.DefaultText.text("Accessed Array Count: ", Wrapper.FontSizes.BUTTON_SIZE), (self.MARGIN_X + 200, self.SORTING_HEIGHT + 2*self.TITLE_Y + self.MARGIN_Y + self.TITLE_HEIGHT/4), self.window, slide_in)
self.changes = Wrapper.Text(Wrapper.DefaultText.text("Swaps: ", Wrapper.FontSizes.BUTTON_SIZE), (self.MARGIN_X + 600, self.SORTING_HEIGHT + 2*self.TITLE_Y + self.MARGIN_Y + self.TITLE_HEIGHT/4), self.window, slide_in)
self.aux_count = Wrapper.Text(Wrapper.DefaultText.text("", Wrapper.FontSizes.BUTTON_SIZE), (self.MARGIN_X + 225, self.SORTING_HEIGHT + 2*self.TITLE_Y + self.MARGIN_Y + self.TITLE_HEIGHT/2 + self.MARGIN_Y), self.window, slide_in)
self.swaps = Wrapper.Text(Wrapper.DefaultText.text("Changes: ", Wrapper.FontSizes.BUTTON_SIZE), (self.MARGIN_X + 600, self.SORTING_HEIGHT + 2*self.TITLE_Y + self.MARGIN_Y + self.TITLE_HEIGHT/2 + self.MARGIN_Y), self.window, slide_in)

self.screen_group.add(self.accessed)
self.screen_group.add(self.swaps)
self.screen_group.add(self.aux_count)
self.screen_group.add(self.changes)
# TODO: RETURNS STUPID STUFF, reduce returns and make it so that you don't have to call update manually in main file
self.options_actions = OptionActions(self.window, screen_group, options_screen_group, self.midi, slide_in)
return self.scroll_bar

def toggle_aux_array(self):
if self.aux_array == None:
self.aux_count.change_text(Wrapper.DefaultText.text("Accessed Auxillary Array Count: ", Wrapper.FontSizes.BUTTON_SIZE))
self.array.change((self.SORTING_WIDTH, self.SORTING_HEIGHT/2), (self.SORTING_X - self.SORTING_WIDTH/2, self.SORTING_Y - self.SORTING_HEIGHT/2))
self.aux_array = Array(self.aux_sorting_group, (self.SORTING_WIDTH, self.SORTING_HEIGHT/2), (self.SORTING_X - self.SORTING_WIDTH/2, self.SORTING_Y), 1, self.array_length, self.midi)
else:
self.aux_count.change_text(Wrapper.DefaultText.text("", Wrapper.FontSizes.BUTTON_SIZE))
self.aux_array.kill()
self.array.change((self.SORTING_WIDTH, self.SORTING_HEIGHT), (self.SORTING_X - self.SORTING_WIDTH/2, self.SORTING_Y - self.SORTING_HEIGHT/2))
self.aux_array = None
Expand Down
3 changes: 3 additions & 0 deletions UI/Wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ def __init__(self, text_arg, coords, window, will_slide = True, screen = None):
image = text_arg.render()
super().__init__(image, coords, (0, 0), window, 400, will_slide, screen)

def change_text(self, text_arg):
self.image = text_arg.render()

# TODO: There is a double where a click registers as a double click
class Button(ButtonConfig, Slide):
'''This is an icon button class
Expand Down

0 comments on commit 42fbd9a

Please sign in to comment.