public class Hanoi { private int moveCount; public void solve (int n) { System.out.println("Towers of Hanoi with " + n + " disks"); System.out.println("----------------------------------"); moveCount = 0; //recursive start solveAuxiliary(n, 1, 2, 3); System.out.println("----------------------------------"); System.out.println("# Moves: " + moveCount); } private void solveAuxiliary(int disks, int startPeg, int intermediatePeg, int destinationPeg) { if (disks > 0) { //recursive call to move the rest of the disks from start to the middle peg solveAuxiliary(disks - 1, startPeg, destinationPeg, intermediatePeg); //actually make a move System.out.println("Move disk " + disks + " from " + startPeg + " to " + destinationPeg); moveCount++; //another recursive call to move the rest of the disks from middle peg to destination solveAuxiliary(disks - 1, intermediatePeg, startPeg, destinationPeg); } } }