// Source code example for "A Practical Introduction // to Data Structures and Algorithm Analysis" // by Clifford A. Shaffer, Prentice Hall, 1998. // Copyright 1998 by Clifford A. Shaffer import java.io.*; // Driver class for testing Tower of Hanoi function class TOHmain { static int counter = 1; // Count the number of moves // Do (and count) a move by printing the action static void move(Pole start, Pole goal) { System.out.println(counter + ": Move " + start + " to " + goal); counter++; } //Tower of Hanoi function static void TOH(int n, Pole start, Pole goal, Pole temp) { if (n == 0) return; // Base case TOH(n-1, start, temp, goal); // Recursive call: n-1 rings move(start, goal); // Move bottom disk to goal TOH(n-1, temp, goal, start); // Recursive call: n-1 rings } // Main function for TOH driver public static void main(String args[]) throws IOException { Assert.notFalse(args.length == 1, "Usage: TOHmain "); // Arbitrarily assign identifiers to poles for printing Pole start = new Pole(1); Pole goal = new Pole(2); Pole temp = new Pole(3); // Read in number of disks int disks = Integer.parseInt(args[0]); TOH(disks, start, goal, temp); System.in.read(); } }