annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/minimal_hanoi.py @ 69:33d812a61356

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