Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. """
  2. This program animates the Tower of Hanoi puzzle. It has an animate_init()
  3. function to create the initial setup based on the number of disks
  4. input. Then it has a animate_move() function, which takes the list of
  5. stacks and integers indicating the from and to stacks, and adjusts
  6. the animation accordingly.
  7.  
  8. NOTE: students need to follow the interface for these functions,
  9. that is provide the expected inputs to the functions,
  10. but do not need to change anything in the functions.
  11.  
  12. Author: Aaron Deever
  13.  
  14. file: tower_animate.py
  15. """
  16.  
  17. from turtle import *
  18. from myStack import *
  19.  
  20. def drawRec(width):
  21. for i in range(2):
  22. fd(width)
  23. lt(90)
  24. fd(1)
  25. lt(90)
  26.  
  27. def animate_init(disks):
  28. """
  29. This function initializes the canvas for the Tower of Hanoi animation.
  30. input: disks: number of disks in the initial tower.
  31. """
  32.  
  33. setup(900,450)
  34. scale_factor = disks + 2 # make top disk have width 3
  35. setworldcoordinates(-2,-1,4*scale_factor+2,2*scale_factor+1)
  36. speed(0)
  37. hideturtle()
  38.  
  39. # draw the bases of the piles
  40. up()
  41. fd(scale_factor / 6)
  42. lt(90)
  43. fd(scale_factor / 2)
  44. rt(90)
  45. down()
  46. for i in range(2):
  47. fd(scale_factor)
  48. up()
  49. fd(scale_factor / 3)
  50. down()
  51. fd(scale_factor)
  52. up()
  53. fd(-scale_factor * 11/3)
  54. down()
  55.  
  56. # draw the Tower
  57. for i in range(scale_factor, 2, -1):
  58. drawRec(i)
  59. lt(90)
  60. fd(1)
  61. rt(90)
  62. fd(.5)
  63.  
  64. # return to home + offset equal to disks, pen up, facing east for each move
  65. # offset is used so animate_move() calls can deduce worldcoordinates
  66. # without having to be passed the disks argument
  67. up()
  68. home()
  69. fd(disks)
  70.  
  71. def animate_move(stackList, fromPile, toPile):
  72. """
  73. Precondition - move is legal.
  74. Precondition: THIS FUNCTION ASSUMES THAT THE STACKS HAVE ALREADY
  75. BEEN ADJUSTED ACCORDING TO THE MOVE THAT
  76. IT IS ABOUT TO ANIMATE. IT DOES NOT ADJUST THE STACKS!!!
  77. THIS FUNCTION SHOULD BE CALLED
  78. IMMEDIATELY AFTER THE MOVE IS PERFORMED.
  79.  
  80. Precondition - the turtle is offset horizontally from home by
  81. disks units. This allows us to know the worldcoordinates without
  82. having to pass disks as an argument. Turtle is up, facing east.
  83.  
  84. This function identifies the size of the disk that was
  85. at the top of the fromPile, and erases it, and moves it to the toPile.
  86.  
  87. Postcondition = turtle is placed in original state
  88. """
  89.  
  90. # deduce disks from the width of the window
  91. disks,y = position()
  92. fd(-disks) # back to 0,0
  93.  
  94. scale_factor = disks + 2
  95.  
  96. # erase from fromPile
  97. fd(scale_factor / 6)
  98. fd(scale_factor*4/3 * fromPile)
  99. lt(90)
  100. fd(scale_factor/2)
  101. rt(90)
  102.  
  103. fromPileSize = size(stackList[fromPile])
  104. diskSize = top(stackList[toPile])
  105. fd((disks-diskSize) * .5)
  106. lt(90)
  107. fd(fromPileSize)
  108. down()
  109. color("white")
  110. pensize(1) # looks like same pen size erases effectively
  111. fd(1)
  112. rt(90)
  113. fd(diskSize+2)
  114. rt(90)
  115. fd(1)
  116. pensize(1)
  117. up()
  118. color("black")
  119. lt(90) # face east
  120. home()
  121.  
  122. # add it to toPile
  123. fd(scale_factor / 6)
  124. fd(scale_factor*4/3 * toPile)
  125. lt(90)
  126. fd(scale_factor/2)
  127. rt(90)
  128.  
  129. toPileSize = size(stackList[toPile])-1
  130. fd((disks-diskSize) * .5)
  131. lt(90)
  132. fd(toPileSize)
  133. down()
  134. fd(1)
  135. rt(90)
  136. fd(diskSize+2)
  137. rt(90)
  138. fd(1)
  139. up()
  140. lt(90) # face east
  141. home()
  142. # move forward levels
  143. fd(disks)
  144.  
  145.  
  146. def animate_finish():
  147. bye()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement