![]() 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. |