/* A simple implementation of a recursive method to list the moves to solve a Towers of Hanoi puzzle of size specified by the user. This version simply prints each move of a single disk to the console screen. It could easily be adapted to count moves. Record of revisions: Date Programmer Description of change ==== ========== ===================== March 2003 J. Rodger Original coding from web examples May 2003 J. Rodger I/O conversion to hsa classes */ import hsa.*; public class SimpleHanoi { // move a stack of height h from source to dest, using spare public static void moveDisks (int h, char source, char dest, char spare) { if (h > 0) // a tower of height 0 is "already moved" { // move top h-1 disks from source to spare peg moveDisks (h - 1, source, spare, dest); // move the bottom (biggest) disk to destination Stdout.print ("move Disk " + h); Stdout.println (" from " + source + " to " + dest); // move top h-1 disks from spare peg to destination moveDisks (h - 1, spare, dest, source); } } // moveDisks public static void main (String [] args) { // prompt user for number of disks in stack Stdout.println ("Demonstration of Moves to Solve Towers of Hanoi Puzzle"); Stdout.print ("\tEnter number of disks (height of stack) to move:"); int height = Stdin.readInt (); Stdout.println ("Tower of Hanoi of height " + height); moveDisks (height, 'A', 'C', 'B'); Stdout.println ("Normal Program Termination"); } // main } // SimpleHanoi