here is what it does. The starting position is:
A|321
B|
C|
then with moveTower(2,fromA,toC, withB)
the result is:
A|3
B|
C|21
then, moveDisk(fromA, toB)
does
A|
B|3
C|21
and finally moveTower(2,fromC, toB)
ends the game
A|
B|
C|321
That is the usual solution for Hanoi: move the tower of height h-1
to the withPole
, move the largest disc to the endPole
and move tower of height h-1
to the endPole
.
That works because you can move each disc of the tower of height h-1
on the largest disc.
To do moveTower(height-1,w,x)
you are allowed to place all the remaining disc in all the 3 towers.
So you will moveTower(height-2,y,z)
then move the 2nd largest disc to its destination, and move the tower height-2 again.
Edit:
The diagram in this link best describs what I am trying to say ("A picture is worth a thousand words").
If you know of to move a tower of height-1
then, just do the 3 steps described in your algorithm. moveDisc
is the "base case" (climb the first step), moveTower is the recursion (how to go from step n
to n+1
).