Question: Implement an iterator that produces the moves for the Towers of Hanoi puzzle described in Worked Example 13.2. Provide methods hasMoreMoves and nextMove. The nextMove
Implement an iterator that produces the moves for the Towers of Hanoi puzzle described in Worked Example 13.2. Provide methods hasMoreMoves and nextMove. The nextMove method should yield a string describing the next move. For example, the following code prints all moves needed to move five disks from peg 1 to peg 3:

A disk mover that moves a single disk from one peg to another simply has a nextMove method that returns a string Move disk from peg source to target A disk mover with more than one disk to move must work harder. It needs another DiskMover to help it move the first d – 1 disks. Then nextMove asks that disk mover for its next move until it is done. Then the nextMove method issues a command to move the dth disk. Finally, it constructs another disk mover that generates the remaining moves.
It helps to keep track of the state of the disk mover:
• BEFORE_LARGEST: A helper mover moves the smaller pile to the other peg.
• LARGEST: Move the largest disk from the source to the destination.
• AFTER_LARGEST: The helper mover moves the smaller pile from the other peg to the target.
• DONE: All moves are done.
DiskMover mover = new DiskMover(5, 1, 3); while (mover.hasMoreMoves()) { } System.out.println (mover.next Move());
Step by Step Solution
3.49 Rating (156 Votes )
There are 3 Steps involved in it
To provide an iterator for the Towers of Hanoi puzzle as described in your question we can implement a DiskMover class in Java This class will simulat... View full answer
Get step-by-step solutions from verified subject matter experts
