From 42fbd9a5646db40f6fcec987d240e717bddfe1bc Mon Sep 17 00:00:00 2001 From: ProfessorAtomicManiac Date: Sun, 18 Sep 2022 01:08:50 -0700 Subject: [PATCH] Added statistics at bottom --- Algorithms/Sorting.py | 2 +- UI/Arrays.py | 17 +++++++++++++++++ UI/UI.py | 39 +++++++++++++++++++++++++++++++++++---- UI/Wrapper.py | 3 +++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Algorithms/Sorting.py b/Algorithms/Sorting.py index 91f2784..71e8cbb 100644 --- a/Algorithms/Sorting.py +++ b/Algorithms/Sorting.py @@ -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()): diff --git a/UI/Arrays.py b/UI/Arrays.py index 60debbd..075312f 100644 --- a/UI/Arrays.py +++ b/UI/Arrays.py @@ -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: @@ -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) @@ -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() @@ -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) @@ -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 diff --git a/UI/UI.py b/UI/UI.py index 855f680..95dc3f9 100644 --- a/UI/UI.py +++ b/UI/UI.py @@ -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 @@ -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() @@ -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 @@ -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) @@ -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 diff --git a/UI/Wrapper.py b/UI/Wrapper.py index 5e205d0..917cf08 100644 --- a/UI/Wrapper.py +++ b/UI/Wrapper.py @@ -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