1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | Animated Towers of Hanoi using Tk with optional bitmap file in background. |
---|
5 | n/a | |
---|
6 | n/a | Usage: hanoi.py [n [bitmapfile]] |
---|
7 | n/a | |
---|
8 | n/a | n is the number of pieces to animate; default is 4, maximum 15. |
---|
9 | n/a | |
---|
10 | n/a | The bitmap file can be any X11 bitmap file (look in /usr/include/X11/bitmaps for |
---|
11 | n/a | samples); it is displayed as the background of the animation. Default is no |
---|
12 | n/a | bitmap. |
---|
13 | n/a | """ |
---|
14 | n/a | |
---|
15 | n/a | from tkinter import Tk, Canvas |
---|
16 | n/a | |
---|
17 | n/a | # Basic Towers-of-Hanoi algorithm: move n pieces from a to b, using c |
---|
18 | n/a | # as temporary. For each move, call report() |
---|
19 | n/a | def hanoi(n, a, b, c, report): |
---|
20 | n/a | if n <= 0: return |
---|
21 | n/a | hanoi(n-1, a, c, b, report) |
---|
22 | n/a | report(n, a, b) |
---|
23 | n/a | hanoi(n-1, c, b, a, report) |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | # The graphical interface |
---|
27 | n/a | class Tkhanoi: |
---|
28 | n/a | |
---|
29 | n/a | # Create our objects |
---|
30 | n/a | def __init__(self, n, bitmap = None): |
---|
31 | n/a | self.n = n |
---|
32 | n/a | self.tk = tk = Tk() |
---|
33 | n/a | self.canvas = c = Canvas(tk) |
---|
34 | n/a | c.pack() |
---|
35 | n/a | width, height = tk.getint(c['width']), tk.getint(c['height']) |
---|
36 | n/a | |
---|
37 | n/a | # Add background bitmap |
---|
38 | n/a | if bitmap: |
---|
39 | n/a | self.bitmap = c.create_bitmap(width//2, height//2, |
---|
40 | n/a | bitmap=bitmap, |
---|
41 | n/a | foreground='blue') |
---|
42 | n/a | |
---|
43 | n/a | # Generate pegs |
---|
44 | n/a | pegwidth = 10 |
---|
45 | n/a | pegheight = height//2 |
---|
46 | n/a | pegdist = width//3 |
---|
47 | n/a | x1, y1 = (pegdist-pegwidth)//2, height*1//3 |
---|
48 | n/a | x2, y2 = x1+pegwidth, y1+pegheight |
---|
49 | n/a | self.pegs = [] |
---|
50 | n/a | p = c.create_rectangle(x1, y1, x2, y2, fill='black') |
---|
51 | n/a | self.pegs.append(p) |
---|
52 | n/a | x1, x2 = x1+pegdist, x2+pegdist |
---|
53 | n/a | p = c.create_rectangle(x1, y1, x2, y2, fill='black') |
---|
54 | n/a | self.pegs.append(p) |
---|
55 | n/a | x1, x2 = x1+pegdist, x2+pegdist |
---|
56 | n/a | p = c.create_rectangle(x1, y1, x2, y2, fill='black') |
---|
57 | n/a | self.pegs.append(p) |
---|
58 | n/a | self.tk.update() |
---|
59 | n/a | |
---|
60 | n/a | # Generate pieces |
---|
61 | n/a | pieceheight = pegheight//16 |
---|
62 | n/a | maxpiecewidth = pegdist*2//3 |
---|
63 | n/a | minpiecewidth = 2*pegwidth |
---|
64 | n/a | self.pegstate = [[], [], []] |
---|
65 | n/a | self.pieces = {} |
---|
66 | n/a | x1, y1 = (pegdist-maxpiecewidth)//2, y2-pieceheight-2 |
---|
67 | n/a | x2, y2 = x1+maxpiecewidth, y1+pieceheight |
---|
68 | n/a | dx = (maxpiecewidth-minpiecewidth) // (2*max(1, n-1)) |
---|
69 | n/a | for i in range(n, 0, -1): |
---|
70 | n/a | p = c.create_rectangle(x1, y1, x2, y2, fill='red') |
---|
71 | n/a | self.pieces[i] = p |
---|
72 | n/a | self.pegstate[0].append(i) |
---|
73 | n/a | x1, x2 = x1 + dx, x2-dx |
---|
74 | n/a | y1, y2 = y1 - pieceheight-2, y2-pieceheight-2 |
---|
75 | n/a | self.tk.update() |
---|
76 | n/a | self.tk.after(25) |
---|
77 | n/a | |
---|
78 | n/a | # Run -- never returns |
---|
79 | n/a | def run(self): |
---|
80 | n/a | while 1: |
---|
81 | n/a | hanoi(self.n, 0, 1, 2, self.report) |
---|
82 | n/a | hanoi(self.n, 1, 2, 0, self.report) |
---|
83 | n/a | hanoi(self.n, 2, 0, 1, self.report) |
---|
84 | n/a | hanoi(self.n, 0, 2, 1, self.report) |
---|
85 | n/a | hanoi(self.n, 2, 1, 0, self.report) |
---|
86 | n/a | hanoi(self.n, 1, 0, 2, self.report) |
---|
87 | n/a | |
---|
88 | n/a | # Reporting callback for the actual hanoi function |
---|
89 | n/a | def report(self, i, a, b): |
---|
90 | n/a | if self.pegstate[a][-1] != i: raise RuntimeError # Assertion |
---|
91 | n/a | del self.pegstate[a][-1] |
---|
92 | n/a | p = self.pieces[i] |
---|
93 | n/a | c = self.canvas |
---|
94 | n/a | |
---|
95 | n/a | # Lift the piece above peg a |
---|
96 | n/a | ax1, ay1, ax2, ay2 = c.bbox(self.pegs[a]) |
---|
97 | n/a | while 1: |
---|
98 | n/a | x1, y1, x2, y2 = c.bbox(p) |
---|
99 | n/a | if y2 < ay1: break |
---|
100 | n/a | c.move(p, 0, -1) |
---|
101 | n/a | self.tk.update() |
---|
102 | n/a | |
---|
103 | n/a | # Move it towards peg b |
---|
104 | n/a | bx1, by1, bx2, by2 = c.bbox(self.pegs[b]) |
---|
105 | n/a | newcenter = (bx1+bx2)//2 |
---|
106 | n/a | while 1: |
---|
107 | n/a | x1, y1, x2, y2 = c.bbox(p) |
---|
108 | n/a | center = (x1+x2)//2 |
---|
109 | n/a | if center == newcenter: break |
---|
110 | n/a | if center > newcenter: c.move(p, -1, 0) |
---|
111 | n/a | else: c.move(p, 1, 0) |
---|
112 | n/a | self.tk.update() |
---|
113 | n/a | |
---|
114 | n/a | # Move it down on top of the previous piece |
---|
115 | n/a | pieceheight = y2-y1 |
---|
116 | n/a | newbottom = by2 - pieceheight*len(self.pegstate[b]) - 2 |
---|
117 | n/a | while 1: |
---|
118 | n/a | x1, y1, x2, y2 = c.bbox(p) |
---|
119 | n/a | if y2 >= newbottom: break |
---|
120 | n/a | c.move(p, 0, 1) |
---|
121 | n/a | self.tk.update() |
---|
122 | n/a | |
---|
123 | n/a | # Update peg state |
---|
124 | n/a | self.pegstate[b].append(i) |
---|
125 | n/a | |
---|
126 | n/a | |
---|
127 | n/a | def main(): |
---|
128 | n/a | import sys |
---|
129 | n/a | |
---|
130 | n/a | # First argument is number of pegs, default 4 |
---|
131 | n/a | if sys.argv[1:]: |
---|
132 | n/a | n = int(sys.argv[1]) |
---|
133 | n/a | else: |
---|
134 | n/a | n = 4 |
---|
135 | n/a | |
---|
136 | n/a | # Second argument is bitmap file, default none |
---|
137 | n/a | if sys.argv[2:]: |
---|
138 | n/a | bitmap = sys.argv[2] |
---|
139 | n/a | # Reverse meaning of leading '@' compared to Tk |
---|
140 | n/a | if bitmap[0] == '@': bitmap = bitmap[1:] |
---|
141 | n/a | else: bitmap = '@' + bitmap |
---|
142 | n/a | else: |
---|
143 | n/a | bitmap = None |
---|
144 | n/a | |
---|
145 | n/a | # Create the graphical objects... |
---|
146 | n/a | h = Tkhanoi(n, bitmap) |
---|
147 | n/a | |
---|
148 | n/a | # ...and run! |
---|
149 | n/a | h.run() |
---|
150 | n/a | |
---|
151 | n/a | |
---|
152 | n/a | # Call main when run as script |
---|
153 | n/a | if __name__ == '__main__': |
---|
154 | n/a | main() |
---|