| 87 | |
| 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 |
| 94 | |
| 95 | # Lift the piece above peg a |
| 96 | ax1, ay1, ax2, ay2 = c.bbox(self.pegs[a]) |
| 97 | while True: |
| 98 | x1, y1, x2, y2 = c.bbox(p) |
| 99 | if y2 < ay1: break |
| 100 | c.move(p, 0, -1) |
| 101 | self.tk.update() |
| 102 | |
| 103 | # Move it towards peg b |
| 104 | bx1, by1, bx2, by2 = c.bbox(self.pegs[b]) |
| 105 | newcenter = (bx1+bx2)//2 |
| 106 | while True: |
| 107 | x1, y1, x2, y2 = c.bbox(p) |
| 108 | center = (x1+x2)//2 |
| 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() |
| 113 | |
| 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 True: |
| 118 | x1, y1, x2, y2 = c.bbox(p) |
| 119 | if y2 >= newbottom: break |
| 120 | c.move(p, 0, 1) |
| 121 | self.tk.update() |
| 122 | |
| 123 | # Update peg state |
| 124 | self.pegstate[b].append(i) |
| 125 | |
| 126 | |
| 127 | def main(): |