Question
P13.7, P13.8 P13.7 Implement an iterator that produces the moves for the Towers of Hanoi puzzle described in Worked Example 13.2Links to an external site..
P13.7, P13.8
P13.7 Implement an iterator that produces the moves for the Towers of Hanoi puzzle described in Worked Example 13.2Links to an external site.. 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:
DiskMover mover = new DiskMover(5, 1, 3);
while (mover.hasMoreMoves())
{
System.out.println(mover.nextMove());
}
Hint: 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.
P13.8 Escaping a Maze. You are currently located inside a maze. The walls of the maze are indicated by asterisks (*).
* *******
* * *
* ***** *
* * * *
* * *** *
* * *
*** * * *
* * *
******* *
Use the following recursive approach to check whether you can escape from the maze: If you are at an exit, return true. Recursively check whether you can escape from one of the empty neighboring locations without visiting the current location. This method merely tests whether there is a path out of the maze. Extra credit if you can print out a path that leads to an exit.
(C++)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started