// Purpose. Domain layer to Towers of Hanoi - algorithmic solution import java.util.List; import java.util.ArrayList; class LeafMove { public int from, to; public LeafMove( int f, int t ) { from = f; to = t; } } class TowersOfHanoiAlgo { static void move( int n, int from, int to, int aux, List instructions ) { if (n == 1) { instructions.add( new LeafMove(from,to) ); return; } move( n-1, from, aux, to, instructions ); instructions.add( new LeafMove(from,to) ); move( n-1, aux, to, from, instructions ); } public static void main( String[] args ) { int size = 5; if (args.length > 0) size = Integer.parseInt( args[0] ); List instructions = new ArrayList(); move( size, 0, 2, 1, instructions ); System.out.println( "disks is " + size + ", moves is " + instructions.size() ); new TowersOfHanoiPresentation( size, instructions ); } } // number of disks number of moves // 2 3 // 3 7 // 4 15 // 5 31 // 6 63 // 7 127