|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | + |
| 4 | +from tkinter import * |
| 5 | +import sys |
| 6 | +import time |
| 7 | +import subprocess |
| 8 | +import os |
| 9 | +from math import sqrt |
| 10 | + |
| 11 | + |
| 12 | +""" |
| 13 | +__project__ = "push_swap visualizer" |
| 14 | +__author__ = "Emmanuel Ruaud" |
| 15 | + |
| 16 | +This python script was created to visualize your work with the PUSH_SWAP |
| 17 | +42 Project. |
| 18 | +You must put this script in the same path or in a sibling path of your program |
| 19 | +Of course you need Python3 with the builtin Tkinter. |
| 20 | +You can install it with Brew. |
| 21 | +--> Brew install python3 |
| 22 | +Execute the script with : |
| 23 | +--> python3 pyviz.py `ruby -e "puts (-200..200).to_a.shuffle.join(' ')"` |
| 24 | +You can change the PUSHS_PATH to get to the relative path of your push_swap |
| 25 | +You can decrease or increase the speed with the matching buttons. |
| 26 | +""" |
| 27 | + |
| 28 | + |
| 29 | +RELATIVE_PATH = r'push_swap' |
| 30 | + |
| 31 | + |
| 32 | +class PsGui: |
| 33 | + def __init__(self, master): |
| 34 | + ww = 600 |
| 35 | + wh = 600 |
| 36 | + self.i = 0 |
| 37 | + self.speed = 0 |
| 38 | + dirname = os.path.dirname(os.path.abspath(__file__)) |
| 39 | + PUSHS_PATH = os.path.join(dirname, RELATIVE_PATH) |
| 40 | + self.pile_a = [int(num) for num in sys.argv[1:]] |
| 41 | + self.first_pile = self.pile_a[:] |
| 42 | + self.pile_b = [] |
| 43 | + self.cmds = subprocess.check_output([PUSHS_PATH] + sys.argv[1:], stderr=subprocess.STDOUT, |
| 44 | + timeout=12).splitlines() |
| 45 | + if len(self.pile_a) != 0: |
| 46 | + self.prespeed = 1 / len(self.pile_a) |
| 47 | + else: |
| 48 | + self.prespeed = 0 |
| 49 | + self.master = master |
| 50 | + master.title("Push_swap viewer") |
| 51 | + self.mainframe = Frame(master) |
| 52 | + self.mainframe.pack(fill=BOTH) |
| 53 | + self.can = Canvas(self.mainframe, width=ww, height=wh, bg="black") |
| 54 | + self.can.pack(side=LEFT) |
| 55 | + self.toolframe = Frame(self.mainframe) |
| 56 | + self.toolframe.pack(side=RIGHT, fill=BOTH) |
| 57 | + self.butframe = Frame(self.toolframe) |
| 58 | + self.butframe.pack(side=TOP, fill=Y) |
| 59 | + self.PrevCtl = Button(self.butframe, text="<<", command=self.speed_down) |
| 60 | + self.PrevCtl.pack(side=LEFT) |
| 61 | + self.PauseCtl = Button(self.butframe, text=">", command=self.pause) |
| 62 | + self.PauseCtl.pack(side=LEFT) |
| 63 | + self.NextCtl = Button(self.butframe, text=">>", command=self.speed_up) |
| 64 | + self.NextCtl.pack(side=LEFT) |
| 65 | + self.ResetCtl = Button(self.butframe, text="R", command=self.reset) |
| 66 | + self.ResetCtl.pack(side=LEFT) |
| 67 | + self.listbox = Listbox(self.toolframe, bg='black', fg='light cyan', |
| 68 | + font=("monospace", 12), relief=FLAT) |
| 69 | + self.listbox.pack(fill=BOTH, expand=1) |
| 70 | + for cmd in self.cmds: |
| 71 | + self.listbox.insert(END, cmd) |
| 72 | + self.statusframe = Frame(master) |
| 73 | + self.statusframe.pack(side=BOTTOM, fill=X) |
| 74 | + self.speedmeter = Label(self.statusframe, |
| 75 | + text='frame rate = ' + str(self.speed), |
| 76 | + font=("monospace", 10)) |
| 77 | + self.speedmeter.pack(side=LEFT) |
| 78 | + self.totalcount = Label(self.statusframe, |
| 79 | + text='- operations = ' + str(len(self.cmds)), |
| 80 | + font=("monospace", 10)) |
| 81 | + self.totalcount.pack(side=LEFT) |
| 82 | + self.draw_rectangles() |
| 83 | + self.launch() |
| 84 | + |
| 85 | + def reset(self): |
| 86 | + self.speed = 0 |
| 87 | + self.i = 0 |
| 88 | + del self.pile_a[:] |
| 89 | + self.pile_a = self.first_pile[:] |
| 90 | + del self.pile_b[:] |
| 91 | + self.can.delete("all") |
| 92 | + self.draw_rectangles() |
| 93 | + self.listbox.see(0) |
| 94 | + self.PauseCtl.config(text='>') |
| 95 | + self.launch() |
| 96 | + |
| 97 | + def pause(self): |
| 98 | + if self.speed != 0: |
| 99 | + self.prespeed = self.speed |
| 100 | + self.speed = 0 |
| 101 | + self.speedmeter.config(text='frame rate = 0') |
| 102 | + self.PauseCtl.config(text='>') |
| 103 | + else: |
| 104 | + self.speed = self.prespeed |
| 105 | + self.speedmeter.config(text='frame rate = ' \ |
| 106 | + + '{:.2e}'.format(self.speed)) |
| 107 | + self.PauseCtl.config(text='||') |
| 108 | + |
| 109 | + def speed_up(self): |
| 110 | + if self.speed == 0: |
| 111 | + self.PauseCtl.config(text='||') |
| 112 | + self.speed = self.prespeed |
| 113 | + self.speed = self.speed ** 2 |
| 114 | + self.speedmeter.config(text='frame rate = ' \ |
| 115 | + + '{:.2e}'.format(self.speed)) |
| 116 | + |
| 117 | + def speed_down(self): |
| 118 | + self.speed = sqrt(self.speed) |
| 119 | + self.speedmeter.config(text='frame rate = ' \ |
| 120 | + + '{:.2e}'.format(self.speed)) |
| 121 | + |
| 122 | + def launch_cmds(self, cmd): |
| 123 | + if cmd == b'sa' and len(self.pile_a) >= 2: |
| 124 | + self.pile_a[0], self.pile_a[1] = self.pile_a[1], self.pile_a[0] |
| 125 | + if cmd == b'sb' and len(self.pile_b) >= 2: |
| 126 | + self.pile_b[0], self.pile_b[1] = self.pile_b[1], self.pile_b[0] |
| 127 | + if cmd == b'ss': |
| 128 | + if (len(self.pile_a) >= 2): |
| 129 | + self.pile_a[0], self.pile_a[1] = self.pile_a[1], self.pile_a[0] |
| 130 | + if (len(self.pile_b) >= 2): |
| 131 | + self.pile_b[0], self.pile_b[1] = self.pile_b[1], self.pile_b[0] |
| 132 | + if cmd == b'ra' and len(self.pile_a) >= 2: |
| 133 | + self.pile_a.append(self.pile_a[0]) |
| 134 | + del self.pile_a[0] |
| 135 | + if cmd == b'rb' and len(self.pile_b) >= 2: |
| 136 | + self.pile_b.append(self.pile_b[0]) |
| 137 | + del self.pile_b[0] |
| 138 | + if cmd == b'rr': |
| 139 | + if (len(self.pile_a) >= 2): |
| 140 | + self.pile_a.append(self.pile_a[0]) |
| 141 | + del self.pile_a[0] |
| 142 | + if (len(self.pile_b) >= 2): |
| 143 | + self.pile_b.append(self.pile_b[0]) |
| 144 | + del self.pile_b[0] |
| 145 | + if cmd == b'rra' and len(self.pile_a) >= 2: |
| 146 | + self.pile_a = [self.pile_a[-1]] + self.pile_a |
| 147 | + del self.pile_a[-1] |
| 148 | + if cmd == b'rrb' and len(self.pile_b) >= 2: |
| 149 | + self.pile_b = [self.pile_b[-1]] + self.pile_b |
| 150 | + del self.pile_b[-1] |
| 151 | + if cmd == b'rrr': |
| 152 | + if (len(self.pile_a) >= 2): |
| 153 | + self.pile_a = [self.pile_a[-1]] + self.pile_a |
| 154 | + del self.pile_a[-1] |
| 155 | + if (len(self.pile_b) >= 2): |
| 156 | + self.pile_b = [self.pile_b[-1]] + self.pile_b |
| 157 | + del self.pile_b[-1] |
| 158 | + if cmd == b'pa' and len(self.pile_b) >= 1: |
| 159 | + self.pile_a = [self.pile_b[0]] + self.pile_a |
| 160 | + del self.pile_b[0] |
| 161 | + if cmd == b'pb' and len(self.pile_a) >= 1: |
| 162 | + self.pile_b = [self.pile_a[0]] + self.pile_b |
| 163 | + del self.pile_a[0] |
| 164 | + return self.pile_a, self.pile_b |
| 165 | + |
| 166 | + def set_color(self, index): |
| 167 | + col = '#%02x%02x%02x' % (int(255 * (index - 0.3) * (index > 0.3)), |
| 168 | + int(255 * index |
| 169 | + - ((510 * (index - 0.6)) * (index > 0.6))), |
| 170 | + int((255 - 510 * index) * (index < 0.5))) |
| 171 | + return col |
| 172 | + |
| 173 | + def draw_rectangles(self): |
| 174 | + vi = 0 |
| 175 | + ww = 600 |
| 176 | + wh = 600 |
| 177 | + hw = ww / 2 |
| 178 | + hm = len(self.pile_a) + len(self.pile_b) |
| 179 | + mx, mn = (0, 0) |
| 180 | + if (hm != 0): |
| 181 | + mx = max(self.pile_a + self.pile_b) |
| 182 | + mn = min(self.pile_a + self.pile_b) |
| 183 | + rects = [] |
| 184 | + if len(self.pile_a) > 0: |
| 185 | + a_val = [(num - mn) / (mx - mn) for num in self.pile_a] |
| 186 | + for vala in a_val: |
| 187 | + rects.append(self.can.create_rectangle(0, vi, |
| 188 | + 10 + vala * (hw - 100), vi + wh / hm, |
| 189 | + fill=self.set_color(vala), outline="")) |
| 190 | + vi += wh / hm |
| 191 | + vi = 0 |
| 192 | + if len(self.pile_b) > 0: |
| 193 | + b_val = [(num - mn) / (mx - mn) for num in self.pile_b] |
| 194 | + for valb in b_val: |
| 195 | + rects.append(self.can.create_rectangle(hw, vi, |
| 196 | + hw + 10 + valb * (hw - 100), vi + wh / hm, |
| 197 | + fill=self.set_color(valb), outline="")) |
| 198 | + vi += wh / hm |
| 199 | + |
| 200 | + def launch(self): |
| 201 | + while self.i < len(self.cmds): |
| 202 | + if self.speed != 0: |
| 203 | + while self.i < len(self.cmds): |
| 204 | + self.listbox.activate(self.i) |
| 205 | + self.can.delete("all") |
| 206 | + self.pile_a, self.pile_b = \ |
| 207 | + self.launch_cmds(self.cmds[self.i]) |
| 208 | + self.draw_rectangles() |
| 209 | + time.sleep(2 * self.speed) |
| 210 | + self.can.update() |
| 211 | + self.listbox.yview_scroll(1, 'units') |
| 212 | + self.i += 1 |
| 213 | + if self.speed == 0: |
| 214 | + break |
| 215 | + time.sleep(0.25) |
| 216 | + self.can.update() |
| 217 | + self.PauseCtl.config(text='>') |
| 218 | + |
| 219 | + |
| 220 | +root = Tk() |
| 221 | +root.resizable(width=False, height=False) |
| 222 | +gui = PsGui(root) |
| 223 | +root.mainloop() |
0 commit comments