a friend of mine named Till told me about the riddle of hanoi. it took me 80 min to code my solution without surfing or any other external knowledge base. if u think u can beat me try to do it without looking at any solution and post me your high scores ;) (no competition - it is really fun and not so hard to code)
have fun !
Ermm .. yes i think no one will post anything but i can try, can"t i ;)
import java.util.ArrayList;
public class Hanoi {
ArrayList[] base;
public Hanoi(int size) {
base = new ArrayList[] {
new ArrayList(size),
new ArrayList(size),
new ArrayList(size) };
for (int i = size; i > 0;) {
base[0].add(new Integer(i--));
}
move(size, base[0], base[1], base[2]);
}
private void move(int n, final ArrayList startList,
final ArrayList targetList,
final ArrayList tempList) {
ArrayList temp, target, start;
start = startList;
target = targetList;
temp = tempList;
Object plate;
if (n == 1) {
plate = start.remove(start.size() - 1);
target.add(plate);
} else if (n == 2) {
plate = start.remove(start.size() - 1);
temp.add(plate);
plate = start.remove(start.size() - 1);
target.add(plate);
plate = temp.remove(temp.size() - 1);
target.add(plate);
} else if (n > 2) {
move(n - 1, start, temp, target);
plate = start.remove(start.size() - 1);
target.add(plate);
move(n - 1, temp, target, start);
}
}
public static void main(String[] args) {
new Hanoi(4);
}
}
