Hanoi towers python4/30/2023 ![]() moveDisc is the "base case" (climb the first step), moveTower is the recursion (how to go from step n to n+1). If you know of to move a tower of height-1 then, just do the 3 steps described in your algorithm. The diagram in this link best describs what I am trying to say ("A picture is worth a thousand words"). So you will moveTower(height-2,y,z) then move the 2nd largest disc to its destination, and move the tower height-2 again. ![]() To do moveTower(height-1,w,x) you are allowed to place all the remaining disc in all the 3 towers. That works because you can move each disc of the tower of height h-1 on the largest disc. import turtle import colorsys import math tup (1000,1000) turtle. Solve and animate the Tower of Hanoi problem with Python and Turtle. You can not put a larger ring on top of a smaller ring. That is the usual solution for Hanoi: move the tower of height h-1 to the withPole, move the largest disc to the endPole and move tower of height h-1 to the endPole. Tower of Hanoi is a puzzle where you need to move all the rings from peg 1 to peg 3. Then with moveTower(2,fromA,toC, withB) the result is: A|3Īnd finally moveTower(2,fromC, toB) ends the game A| Sorry if this is bit of a broad question! Any help would be greatly appreciated! My question is, how does it do so?! could someone go over the lines of code so that I understand how it prints the correct moves? I'm mainly confused with how the value of fp and tp can change from A to B to C. Which prints the correct moves for solving the towers of hanoi problem with 3 discs: MoveTower(height-1,withPole,toPole,fromPole) MoveTower(height-1,fromPole,withPole,toPole) I thought that I understood recursion until they gave this example: def moveTower(height,fromPole, toPole, withPole): The logic of the solving is taken from StackOverflow (see links in docstring), I wrote the part dealing with the graphics, so review is most welcome on the graphical part that I wrote. disks 3 fromtower A totower C usingtower B def hanoi(n, fromtower, totower, usingtower): if. I'm completely new to Python and I am currently going over a tutorial about The Towers of Hanoi and recursion. I wrote a program to show graphically the solution of the problem of the Tower of Hanoi.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |