public static class TowersOfHanoi.Move extends Object
You will need the following fields:
The last field doesn't have a direct analog to the recursive implementation. However, before the first recursive call the disc in question was covered by a tower of smaller discs. After that recursive call returns, the disc is uncovered and can be moved. Then the second recursive call moved the smaller discs back on top of it.
In the recursive version, the runtime stack keeps track of where in the method to jump back to when the recursive call returned, this effectively keeps a bit of state about how far along the computation is.
In the iterative version, we have to store this information explicitly in the object: is the disc covered (and will require work before it can be moved) or is it now uncovered (and ready to be moved before finally moving the smaller discs back on top).
Constructor and Description |
---|
Move(int disc,
int to,
int from,
int auxiliary,
boolean isCovered) |
public Move(int disc, int to, int from, int auxiliary, boolean isCovered)
disc
- the current discto
- which pole the disc is moving tofrom
- the pole the disc is currently onauxiliary
- the auxiliary poleisCovered
- true if the disc is covered, false otherwise.public String toString()