title: Towers of Hanoi Program - counter
body: <p>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). 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. 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. For example, 3 discs would take a minimum of 7 moves.</p>
  
  <p><a href="https://www.mathsisfun.com/games/towerofhanoi.html" rel="nofollow">https://www.mathsisfun.com/games/towerofhanoi.html</a></p>
  
  <p>You can see that I have some commented out some Moves++, but wherever I place the counter it never seems to work.</p>
  
  <pre><code>public class TowerOfHanoi
  
      {//open class
  
      public void Answer(int numOfDisks, String Peg1, String Peg2, String Peg3, int Moves)
      {//open public void Answer
  
          Moves++;
  
          if (numOfDisks == 1)
          {//open if
  
              //Moves++;      
              System.out.println("\nNumber of Moves so far: " + Moves + "\nMove disk on Peg " + Peg1 + " to Peg " + Peg3);
  
          }//close if
  
          else
          {//open else
  
              //Moves++;
              Answer(numOfDisks - 1, Peg1, Peg3, Peg2, Moves);
              System.out.println("\nNumber of Moves so far: " + Moves + "\nMove disk on Peg " + Peg1 + " to Peg " + Peg3);
              //Moves++;
              Answer(numOfDisks - 1, Peg2, Peg1, Peg3, Moves);
  
           }//close else
  
      }//close public void Answer
  
      public static void main (String[]args)
      {//open main
  
          TowerOfHanoi TOH = new TowerOfHanoi();  
          String numOfDisks = JOptionPane.showInputDialog(null, "Enter a number!");
          int NumberOfDisks = Integer.parseInt(numOfDisks);
          System.out.println("\nNumber of disks chosen: " + NumberOfDisks);
          int Moves = 0;
          TOH.Answer(NumberOfDisks, "1", "2", "3", Moves);
  
      }//close main
  
  }//close TowerOfHanoi class
  </code></pre>