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