Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
hanoi.Tkhanoi Class Reference

List of all members.

Public Member Functions

def __init__
def run
def report

Public Attributes

 n
 tk
 canvas
 bitmap
 pegs
 pegstate
 pieces

Detailed Description

Definition at line 27 of file hanoi.py.


Constructor & Destructor Documentation

def hanoi.Tkhanoi.__init__ (   self,
  n,
  bitmap = None 
)

Definition at line 30 of file hanoi.py.

00030 
00031     def __init__(self, n, bitmap = None):
00032         self.n = n
00033         self.tk = tk = Tk()
00034         self.canvas = c = Canvas(tk)
00035         c.pack()
00036         width, height = tk.getint(c['width']), tk.getint(c['height'])
00037 
00038         # Add background bitmap
00039         if bitmap:
00040             self.bitmap = c.create_bitmap(width//2, height//2,
00041                                           bitmap=bitmap,
00042                                           foreground='blue')
00043 
00044         # Generate pegs
00045         pegwidth = 10
00046         pegheight = height//2
00047         pegdist = width//3
00048         x1, y1 = (pegdist-pegwidth)//2, height*1//3
00049         x2, y2 = x1+pegwidth, y1+pegheight
00050         self.pegs = []
00051         p = c.create_rectangle(x1, y1, x2, y2, fill='black')
00052         self.pegs.append(p)
00053         x1, x2 = x1+pegdist, x2+pegdist
00054         p = c.create_rectangle(x1, y1, x2, y2, fill='black')
00055         self.pegs.append(p)
00056         x1, x2 = x1+pegdist, x2+pegdist
00057         p = c.create_rectangle(x1, y1, x2, y2, fill='black')
00058         self.pegs.append(p)
00059         self.tk.update()
00060 
00061         # Generate pieces
00062         pieceheight = pegheight//16
00063         maxpiecewidth = pegdist*2//3
00064         minpiecewidth = 2*pegwidth
00065         self.pegstate = [[], [], []]
00066         self.pieces = {}
00067         x1, y1 = (pegdist-maxpiecewidth)//2, y2-pieceheight-2
00068         x2, y2 = x1+maxpiecewidth, y1+pieceheight
00069         dx = (maxpiecewidth-minpiecewidth) // (2*max(1, n-1))
00070         for i in range(n, 0, -1):
00071             p = c.create_rectangle(x1, y1, x2, y2, fill='red')
00072             self.pieces[i] = p
00073             self.pegstate[0].append(i)
00074             x1, x2 = x1 + dx, x2-dx
00075             y1, y2 = y1 - pieceheight-2, y2-pieceheight-2
00076             self.tk.update()
00077             self.tk.after(25)

Here is the caller graph for this function:


Member Function Documentation

def hanoi.Tkhanoi.report (   self,
  i,
  a,
  b 
)

Definition at line 89 of file hanoi.py.

00089 
00090     def report(self, i, a, b):
00091         if self.pegstate[a][-1] != i: raise RuntimeError # Assertion
00092         del self.pegstate[a][-1]
00093         p = self.pieces[i]
00094         c = self.canvas
00095 
00096         # Lift the piece above peg a
00097         ax1, ay1, ax2, ay2 = c.bbox(self.pegs[a])
00098         while 1:
00099             x1, y1, x2, y2 = c.bbox(p)
00100             if y2 < ay1: break
00101             c.move(p, 0, -1)
00102             self.tk.update()
00103 
00104         # Move it towards peg b
00105         bx1, by1, bx2, by2 = c.bbox(self.pegs[b])
00106         newcenter = (bx1+bx2)//2
00107         while 1:
00108             x1, y1, x2, y2 = c.bbox(p)
00109             center = (x1+x2)//2
00110             if center == newcenter: break
00111             if center > newcenter: c.move(p, -1, 0)
00112             else: c.move(p, 1, 0)
00113             self.tk.update()
00114 
00115         # Move it down on top of the previous piece
00116         pieceheight = y2-y1
00117         newbottom = by2 - pieceheight*len(self.pegstate[b]) - 2
00118         while 1:
00119             x1, y1, x2, y2 = c.bbox(p)
00120             if y2 >= newbottom: break
00121             c.move(p, 0, 1)
00122             self.tk.update()
00123 
00124         # Update peg state
00125         self.pegstate[b].append(i)
00126 

Here is the call graph for this function:

Here is the caller graph for this function:

def hanoi.Tkhanoi.run (   self)

Definition at line 79 of file hanoi.py.

00079 
00080     def run(self):
00081         while 1:
00082             hanoi(self.n, 0, 1, 2, self.report)
00083             hanoi(self.n, 1, 2, 0, self.report)
00084             hanoi(self.n, 2, 0, 1, self.report)
00085             hanoi(self.n, 0, 2, 1, self.report)
00086             hanoi(self.n, 2, 1, 0, self.report)
00087             hanoi(self.n, 1, 0, 2, self.report)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 39 of file hanoi.py.

Definition at line 33 of file hanoi.py.

Definition at line 31 of file hanoi.py.

Definition at line 49 of file hanoi.py.

Definition at line 64 of file hanoi.py.

Definition at line 65 of file hanoi.py.

Definition at line 32 of file hanoi.py.


The documentation for this class was generated from the following file: