annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/minimal_hanoi.py @ 68:5028fdace37b

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