Non Iterative tower of Hanoi solution import java.util. 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 Description: Tower of Hanoi Interactive Visualization using GLUT,OPENGL and basic fundamentals of Computer Graphics. 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). Daniel Liang Tower of Hanoi Animation Usage: Click the Start button to move all the discs from Tower A to Tower B. ("\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) So you move the top tower of size n-1 to an extra peg (by), move the bottom 'tower' of size 1 to the destination peg, and move the top tower from by to dest. Recursive case: your tower is of size n > 1. You can see that I have some commented out some Moves++, but wherever I place the counter it never seems to work. So you can do it in one move, from source directly to dest. Click the Reset button to reset to the initial state. For example, 3 discs would take a minimum of 7 moves. Usage: Click the Start button to move all the discs from Tower A to Tower B. 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. Sorry I can't really help you with a fix (way to tired, and the move function would need some thinking which I can't do well), but hopefully this will help you understand where the problem is coming from and you will be able to fix the move function.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). Move(0, 2) (But C(0) = 0, so you can't remove elements).Move(2, 0, 2, 1) ***Another problem, 2 moves left but only 1 element.lets see what happens.You actually want to stack things in 1 for numbers greater than three (so Move(3, 0,1,2) instead of Move(3,0,2,1)).Move(3, 2, 0, 1) ***There is a problem here, A = 2 but C(2) = 1.so you are calling Move 3 with only 1 element in 2.lets see what happens.Could you (in a text editor) write out the expected output if you were able to print the display vertically. One of the first steps of formatting output is visualizing what the output will look like. Move(3, 0, 2, 1) (From example, C(0) = 3, C(2) = 1) I asked my professor to check my code before submitting it.Now finally for the problem, Move(4, 0, 1, 2) (C(0) = 4) So by calling move, the effect is A - 1, B + 1.However I believe this should help explain thing (I did this by hand so it could be wrong, sorry too lazy load up the project on my java computer).Ĭonsider Move(2, A=0, B=1, C=2) (C(0) = 3, count of stack 0.I gave it 3 so I could use it later on) Well if I had to recommend, I would say put the debugger on the move, and look at how it flows, and you should be able to track down the null pointer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |