public class Towers { public static void main(String[] args) { solveTowers(3, 'A', 'C'); } public static char otherPole(char from, char to) { char[] letters = { 'A', 'B', 'C' }; for (int i = 0; i < letters.length; i++) { if (from != letters[i] && to != letters[i]) { return letters[i]; } } return '-'; // should not get here } public static void solveTowers(int num, char from, char to) { if (num != 0) { solveTowers(num - 1, from, otherPole(from, to)); System.out.println("Move disc #" + num + " from " + from + " to " + to); solveTowers(num - 1, otherPole(from, to), to); } } }