A peg object might have attributes for its position and which disc objects are attached to it. Each disc might have attributes for its size and color along with which peg it is stacked. In the Tower of Hanoi an object oriented model might consist of a class for a disc and another for a peg. Some applications may have multiple views. The View outputs the model, either presenting a visual repretation, outputting information from the model, responding to signals from the Control or all of these. The Control is makes changes to the contents of the model and also passes the changing model along with other signals to the view. The Model is a structure for the data being manipulated by the program The MVC discipline strives to find separate responsibilities of a program into code modules that are independent and loosely linked with each other. And I think the problem is the difficulty in visualizing the nested context of the recursive calls. It is much better than the usual factorial example which does nothing more than use recursion to replace a simple for loop.īut students have a hard time getting it. I have tried a couple of times to teach the Hanoi puzzle as a good example of recursion. Here is a screen shot of the game in progress.Īnd here is a javascript animation. The other 99% of the program involves doing TK graphics to make it come alive. Guido's hanoi.py in the Python Demo area is a nice demonstration of this recursive process. Can't do 4 discs? Well then, move just 3. If you don't know how to move 5 discs from A to C (using B for intermediate storage), then just move 4 discs from A to B, one disk from A to C (this is really easy) and then move 4 discs from B to C. The problem is to move the discs, one at a time from peg to peg in such a way that this is always true and to finally end up with all of the discs on peg 3 in the original order. A disc is never stacked on top of a smaller disc. In the initial configuration all the discs are stacked on the first peg with the largest disc on the bottom and the smallest on top. Each disc can fit on any of 3 pegs and each peg is high enough to hold all the discs in a stack. There are a number of discs each with a hole in the center. Non Iterative tower of Hanoi solution import Tower of Hanoi is a classic game that is often emulated on computers to demonstrate recursion. To make it a bit easier to see, here the version which only counts: let rec hanoi1 n p1 p2 p3 acc = Hanoi (n-1) p1 p3 p2 // this is where a move is hanoi (n-1) p2 p1 p3 acc In order to show this in a compact form, here a very short version of towers of hanoi (consider this a rapid prototype for your java program ) It also shows how to use the return value of the function to obtain - here the move list - in your case, your counter. This is where you should increase your move counter. Then you move p1 to p3 and then you move the rest from p2 to p3.įrom this textual description, it is obvious, that you only really make a move in the "move p1 to p3" section. In order to move the bottom most (n'th) disk from p1 to p3, you first need to move the n-1'th disk from p1 to p2 (and cleanup). ("\nNumber of disks chosen: " + NumberOfDisks) Int NumberOfDisks = Integer.parseInt(numOfDisks) String numOfDisks = JOptionPane.showInputDialog(null, "Enter a number!") ("\nNumber of Moves so far: " + Moves + "\nMove disk on Peg " + Peg1 + " to Peg " + Peg3) Īnswer(numOfDisks - 1, Peg1, Peg3, Peg2, Moves) Īnswer(numOfDisks - 1, Peg2, Peg1, Peg3, Moves) Public void Answer(int numOfDisks, String Peg1, String Peg2, String Peg3, int Moves) You can see that I have some commented out some Moves++, but wherever I place the counter it never seems to work. For example, 3 discs would take a minimum of 7 moves. So far I have gotten the correct placement of disks to work (each disk is being moved correctly), now I need to create a counter variable in order, to show how many moves it took for a certain number of disks inputted by the user. What you have to do now is move all the disks from peg 1 to peg 3, you can use peg 2 as a storage space for other disks. I am doing a Tower Of Hanoi program - there are 3 pegs and a stack of disks on peg 1 in the order of largest to smallest (largest on bottom, smallest on top).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |