/* Towers of Hanoi * * Author: Carsten Butz * Date: September 2002 * Modified: February 2003 * * Program to list the moves needed so solve the puzzle * Towers of Hanoi. The three pegs have numbers 1, 2, and * 3, respectively. */ import tio.*; class Hanoi{ public static void main (String[] args){ int howMany; System.out.print("How many disks do you want to move? "); howMany = Console.in.readInt(); while(howMany != 0) { move(howMany,1,3); System.out.print("How many disks do you want to move? "); howMany = Console.in.readInt(); } } public static void move(int n, int a, int b){ int c; if(n == 1) { System.out.println(a + " -> " + b); } else { // clever way of finding the number of the free pole: c = 6-(a+b); move(n-1,a,c); System.out.println(a + " -> " + b); move(n-1,c,b); } return; } }