Java Reference
In-Depth Information
Move disk from peg 1 to peg 3
Hint: Implement a class DiskMover . The constructor takes
ȗ
The source peg from which to move the disks (1, 2, or 3)
ȗ
The target peg to which to move the disks (1, 2, or 3)
ȗ
The number of disks to move
Figure 5
Towers of Hanoi
622
623
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 peg target
A disk mover with more than one disk to move must work harder. It needs
another DiskMover to help it. In the constructor, construct a
DiskMover(source, other, disks - 1) where other is the
peg other than from and target .
The nextMove asks that disk mover for its next move until it is done. The
effect is to move the first disks - 1 disks to the other peg. Then the
nextMove method issues a command to move a disk from the from peg
to the to peg. Finally, it constructs another disk mover
DiskMover(other, target, disks - 1) that generates the
moves that move the disks from the other peg to the target peg.
Hint: It helps to keep track of the state of the disk mover:
Search WWH ::




Custom Search