~kkissling/python-snippets/testzweig

« back to all changes in this revision

Viewing changes to pyturtle/tdemo_minimal_hanoi.py

  • Committer: Grant Bowman
  • Date: 2010-04-19 11:22:17 UTC
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: grantbow@gmail.com-20100419112217-rw0i2xlr622wc3ag
rough conversion

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# [SNIPPET_NAME: Turtle Hanoi]
 
4
# [SNIPPET_CATEGORIES: PyTurtle]
 
5
# [SNIPPET_DESCRIPTION: Turtle plays Towers of Hanoi.]
 
6
# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html]
 
7
# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>]
 
8
# [SNIPPET_LICENSE: PSF]
 
9
 
 
10
"""       turtle-example-suite:
 
11
 
 
12
         tdemo_minimal_hanoi.py
 
13
 
 
14
A minimal 'Towers of Hanoi' animation:
 
15
A tower of 6 discs is transferred from the
 
16
left to the right peg.
 
17
 
 
18
An imho quite elegant and concise
 
19
implementation using a tower class, which
 
20
is derived from the built-in type list.
 
21
 
 
22
Discs are turtles with shape "square", but
 
23
stretched to rectangles by shapesize()
 
24
 ---------------------------------------
 
25
       To exit press STOP button
 
26
 ---------------------------------------
 
27
"""
 
28
from turtle import *
 
29
 
 
30
class Disc(Turtle):
 
31
    def __init__(self, n):
 
32
        Turtle.__init__(self, shape="square", visible=False)
 
33
        self.pu()
 
34
        self.shapesize(1.5, n*1.5, 2) # square-->rectangle
 
35
        self.fillcolor(n/6., 0, 1-n/6.)
 
36
        self.st()
 
37
 
 
38
class Tower(list):
 
39
    "Hanoi tower, a subclass of built-in type list"
 
40
    def __init__(self, x):
 
41
        "create an empty tower. x is x-position of peg"
 
42
        self.x = x
 
43
    def push(self, d):
 
44
        d.setx(self.x)
 
45
        d.sety(-150+34*len(self))
 
46
        self.append(d)
 
47
    def pop(self):
 
48
        d = list.pop(self)
 
49
        d.sety(150)
 
50
        return d
 
51
 
 
52
def hanoi(n, from_, with_, to_):
 
53
    if n > 0:
 
54
        hanoi(n-1, from_, to_, with_)
 
55
        to_.push(from_.pop())
 
56
        hanoi(n-1, with_, from_, to_)
 
57
 
 
58
def play():
 
59
    onkey(None,"space")
 
60
    clear()
 
61
    hanoi(6, t1, t2, t3)
 
62
    write("press STOP button to exit",
 
63
          align="center", font=("Courier", 16, "bold"))
 
64
 
 
65
def main():
 
66
    global t1, t2, t3
 
67
    ht(); penup(); goto(0, -225)   # writer turtle
 
68
    t1 = Tower(-250)
 
69
    t2 = Tower(0)
 
70
    t3 = Tower(250)
 
71
    # make tower of 6 discs
 
72
    for i in range(6,0,-1):
 
73
        t1.push(Disc(i))
 
74
    # prepare spartanic user interface ;-)
 
75
    write("press spacebar to start game",
 
76
          align="center", font=("Courier", 16, "bold"))
 
77
    onkey(play, "space")
 
78
    listen()
 
79
    return "EVENTLOOP"
 
80
 
 
81
if __name__=="__main__":
 
82
    msg = main()
 
83
    print msg
 
84
    mainloop()