TowersOfHanoiInstructions.java.html

  1  /**
  2     This program prints instructions for solving a Towers of Hanoi puzzle.
  3  */
  4  public class TowersOfHanoiInstructions
  5  {
  6     public static void main(String[] args)
  7     {
  8        move(5, 1, 3);
  9     }
 10  
 11     /**
 12        Print instructions for moving a pile of disks from one peg to another.
 13        @param disks the number of disks to move
 14        @param from the peg from which to move the disks
 15        @param to the peg to which to move the disks
 16     */
 17     public static void move(int disks, int from, int to)
 18     {
 19        if (disks > 0) 
 20        {
 21           int other = 6 - from - to;
 22           move(disks - 1, from, other);
 23           System.out.println("Move disk from peg " + from + " to " + to);
 24           move(disks - 1, other, to);
 25        }
 26     }
 27  }