-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tower.java
41 lines (36 loc) · 1.27 KB
/
Tower.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Tower {
private Stack<Integer> first = new Stack<Integer>();
private Stack<Integer> second = new Stack<Integer>();
private Stack<Integer> third = new Stack<Integer>();
private int total = 0;
public static void main(String[] args) {
Tower tower = new Tower(args);
tower.print();
tower.moveTo3();
tower.print();
}
public void print() {
System.out.println("Tower looks like this:\n");
System.out.println("First:\n" + first.toString());
System.out.println("Second:\n" + second.toString());
System.out.println("Thrid:\n" + third.toString());
}
public Tower(String[] args) {
this.total = args.length;
for(String arg : args) {
first.push(Integer.parseInt(arg));
}
}
public void moveTo3() {
System.out.println("-----------Shuffling around--------");
move(total, first, third, second);
}
public void move(int total, Stack<Integer> source, Stack<Integer> destination, Stack<Integer> buffer) {
if(total > 0) {
move(total - 1, source, buffer, destination);
Integer last = source.pop();
destination.push(last);
move(total - 1, buffer, destination, source);
}
}
}