-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhanoi.py
More file actions
70 lines (54 loc) · 1.91 KB
/
hanoi.py
File metadata and controls
70 lines (54 loc) · 1.91 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
import curses
import time
def hanoi(n, s, d, a):
if n == 1:
return [(1, s, d)]
return hanoi(n-1, s, a, d) + [(n, s, d)] + hanoi(n-1, a, d, s)
def show(towers, m, i):
h, w = stdscr.getmaxyx()
stdscr.clear()
stdscr.addstr(h-1, 0, "%4d: %s " %(i+1, m[0]))
stdscr.addstr(h-1, 8, "%s" %(m[1]), curses.COLOR_RED)
stdscr.addstr(h-1, 11, "->")
stdscr.addstr(h-1, 13, "%s" %(m[2]), curses.COLOR_GREEN)
stdscr.addstr(h-3, 0, "=" * (6*n+7))
stdscr.addstr(h-3, 1*n+0, "SRC")
stdscr.addstr(h-3, 3*n+2, "DST")
stdscr.addstr(h-3, 5*n+4, "AUX")
for t, (tower, stack) in enumerate(towers.items()):
for l in range(n):
if l >= len(stack):
stdscr.addstr(h-2*l-4, n+t*2*(n+1)+1, "|")
else:
if tower == m[2]:
stdscr.addstr(h-2*l-4, n+t*2*(n+1)-stack[l], "<"+"-"*(2*stack[l]+1)+">", curses.COLOR_GREEN)
stdscr.addstr(h-2*l-4, n+t*2*(n+1)+1, str(stack[l]), curses.COLOR_GREEN)
stdscr.addstr(h-2*l-4, n+t*2*(n+1)-stack[l], "<"+"-"*(2*stack[l]+1)+">")
stdscr.addstr(h-2*l-4, n+t*2*(n+1)+1, str(stack[l]))
stdscr.addstr(h-2*l-5, n+t*2*(n+1)+1, "|")
stdscr.refresh()
n = int(input("Disks: "))
hz = int(input("Display: "))
dsply = hz != 0
if dsply:
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
h = hanoi(n, "SRC", "DST", "AUX")
towers = {"SRC": list(range(n, 0, -1)), "DST": [], "AUX": []}
if dsply:
show(towers, (0, "NUL", "NUL"), -1)
stdscr.getch()
for i, m in enumerate(h):
if dsply:
assert m[0] == towers[m[1]].pop()
if len(towers[m[2]]): assert m[0] < towers[m[2]][-1]
towers[m[2]].append(m[0])
show(towers, m, i)
time.sleep(1/hz)
else:
print("%4d: %s %s->%s" %(i+1, *m))
if dsply:
stdscr.getch()
curses.endwin()