Towers of Hanoi solution with a recursive method. - Java Object Oriented Design

Java examples for Object Oriented Design:Method

Description

Towers of Hanoi solution with a recursive method.

Demo Code

public class Main
{
   // recursively move disks between towers
   public static void solveTowers(int disks, int sourcePeg, 
      int destinationPeg, int tempPeg)
   {//  w w w  . j a v a  2s . c om
      // base case -- only one disk to move
      if (disks == 1)
      {
         System.out.printf("%n%d --> %d", sourcePeg, destinationPeg);
         return;
      } 

      // recursion step -- move (disk - 1) disks from sourcePeg
      // to tempPeg using destinationPeg
      solveTowers(disks - 1, sourcePeg, tempPeg, destinationPeg);

      // move last disk from sourcePeg to destinationPeg
      System.out.printf("%n%d --> %d", sourcePeg, destinationPeg);

      // move (disks - 1) disks from tempPeg to destinationPeg
      solveTowers(disks - 1, tempPeg, destinationPeg, sourcePeg);
   }

   public static void main(String[] args)
   {
      int startPeg = 1; // value 1 used to indicate startPeg in output
      int endPeg = 3; // value 3 used to indicate endPeg in output
      int tempPeg = 2; // value 2 used to indicate tempPeg in output
      int totalDisks = 3; // number of disks
      
       solveTowers(totalDisks, startPeg, endPeg, tempPeg);
   }
}

Result


Related Tutorials