Advertisement
Guest User

mitko pls

a guest
Feb 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """ turtle-example-suite:
  3.  
  4. tdemo_minimal_hanoi.py
  5.  
  6. A minimal 'Towers of Hanoi' animation:
  7. A tower of 6 discs is transferred from the
  8. left to the right peg.
  9.  
  10. An imho quite elegant and concise
  11. implementation using a tower class, which
  12. is derived from the built-in type list.
  13.  
  14. Discs are turtles with shape "square", but
  15. stretched to rectangles by shapesize()
  16. ---------------------------------------
  17. To exit press STOP button
  18. ---------------------------------------
  19. """
  20. from turtle import *
  21. import time
  22. import sys
  23. from tkinter import *
  24. """from tkinter import ttk"""
  25. import tkinter.simpledialog
  26.  
  27. root = Tk()
  28. w = Label(root, text="Dialog Practice")
  29. w.pack()
  30. name = tkinter.simpledialog.askstring("DISX", "Tell us how many discs you want today (min=0;max=10)")
  31. title("The Towers of Hanoi by Aleksandar Stefanov / Student ID:51768282")
  32.  
  33.  
  34.  
  35. class Disc(Turtle):
  36. def __init__(self, n):
  37. Turtle.__init__(self, shape="square", visible=False)
  38. self.speed(10 - n)
  39. self.pu()
  40. self.shapesize(1.5, n*1.5, 2) # square-->rectangle
  41. self.fillcolor(n/10., 0, 1-n/10.)
  42. self.st()
  43.  
  44. class Tower(list):
  45. "Hanoi tower, a subclass of built-in type list"
  46. def __init__(self, x):
  47. "create an empty tower. x is x-position of peg"
  48. self.x = x
  49. def push(self, d):
  50. d.setx(self.x)
  51. d.sety(-150+34*len(self))
  52. self.append(d)
  53. def pop(self):
  54. d = list.pop(self)
  55. d.sety(150)
  56. return d
  57.  
  58.  
  59. moves = 0
  60.  
  61. def hanoi(n, from_, with_, to_):
  62. global moves
  63. if int(n) > 0:
  64. hanoi(int(n)-1, from_, to_, with_)
  65. moves += 1
  66. to_.push(from_.pop())
  67. hanoi(int(n)-1, with_, from_, to_)
  68. clear()
  69. write("Total moves = "+ str(moves),
  70. align="center", font=("Courier", 16, "bold"))
  71.  
  72.  
  73. def play():
  74. onkey(None,"space")
  75. clear()
  76. hanoi(name, t1, t2, t3)
  77. sety(-250)
  78. write("press STOP button to exit",
  79. align="center", font=("Courier", 16, "bold"))
  80.  
  81. def pause():
  82. time.sleep(3)
  83.  
  84. def quit():
  85. onkey(None,"q")
  86. bye()
  87.  
  88.  
  89. def main():
  90. global t1, t2, t3
  91. ht(); penup(); goto(0, -225) # writer turtle
  92. t1 = Tower(-250)
  93. t2 = Tower(0)
  94. t3 = Tower(250)
  95. # make tower of 6 discs
  96. for i in range(int(name),0,-1):
  97. t1.push(Disc(i))
  98. # prepare spartanic user interface 😉
  99. write("Aleksandar Stefanov's ToH. Press spacebar to start game",
  100. align="center", font=("Courier", 16, "bold"))
  101. onkey(play, "space")
  102. sety(-260)
  103. write("Press p to Pause",
  104. align="center", font=("Courier", 16, "bold"))
  105. onkey(pause, "p")
  106. sety(-300)
  107. write("Press q to Quit",
  108. align="center", font=("Courier", 16, "bold"))
  109. onkey(quit, "q")
  110. listen()
  111. return "EVENTLOOP"
  112.  
  113. if __name__=="__main__":
  114. msg = main()
  115. print(msg)
  116. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement