school.cs.indiana.edu%ls -l Hanoi*
-rw------- 1 dgerman 577 Mar 31 16:39 Hanoi.java
school.cs.indiana.edu%cat Hanoi.java
class Hanoi {
public static void main(String[] args) {
Hanoi game = new Hanoi();
int numberOfDisks = 3;
game.solve("NY", "SF", "DC", numberOfDisks);
}
void solve(String source, String target, String temp, int n) {
if (n == 1)
System.out.println("MOVEDISK from peg " + source + " to peg " + target);
else {
solve(source, temp, target, n - 1);
System.out.println("MOVEDISK from peg " + source + " to peg " + target);
solve(temp, target, source, n - 1);
}
}
}
school.cs.indiana.edu%javac Hanoi.java
school.cs.indiana.edu%java Hanoi
MOVEDISK from peg NY to peg SF
MOVEDISK from peg NY to peg DC
MOVEDISK from peg SF to peg DC
MOVEDISK from peg NY to peg SF
MOVEDISK from peg DC to peg NY
MOVEDISK from peg DC to peg SF
MOVEDISK from peg NY to peg SF
school.cs.indiana.edu%
Now the question, of course, is: once you understand this, what changes do
you need to make to obtain the output illustrated in lecture notes 17, similar
to the one presented below: