This project uses the Towers class from Chapter 3s Programming Project 12. For the project, write a
Question:
This project uses the Towers class from Chapter 3’s Programming Project 12. For the project, write a recursive methodxtxhxat computes and prints a solution to the Towers of Hanoi game. The method should meet this specification:
Your solution should have a simple stopping case: When n is zero, there is no work to do. When n is more than zero, use a recursive call to move n–1 rings from the start peg to the spare peg. Then call the move method to move one ring from the start peg to the target peg. Finally, make another recursive call to move n–1 rings from the spare peg to the target peg.
Programming Project 12
In this project, you will design and implement a class called Towers, which is part of a program that lets a child play a game called Towers of Hanoi. The game consists of three pegs and a collection of rings that stack on the pegs. The rings are different sizes. The initial configuration for a five-ring game is shown here, with the first tower having rings from one inch (on the top) to five inches (on the bottom).
The rings are stacked in decreasing order of their size, and the second and third towers are initially empty. During the game, the child may transfer rings one at a time from the top of one peg to the top of another. The goal is to move all the rings from the first peg to the second peg. The difficulty is that the child may not place a ring on top of one with a smaller diameter. There is the one extra peg to hold rings temporarily, but the prohibition against a larger ring on a smaller ring applies to it as well as to the other two pegs. A solution for a three-ring game is shown at the top of the next page. The Towers class must keep track of the status of all three pegs. You might use an array of three pegs, where each peg is an object from the previous project. The Towers methods are specified here:
Towers(int n);
// Precondition: 1 <= n <= 64.
// Postcondition: The towers have been initialized
// with n rings on the first peg and no rings on
// the other two pegs. The diameters of the first
// peg’s rings are from one inch (on the top) to n
// inches (on the bottom).
int countRings(int pegNumber)
// Precondition: pegNumber is 1, 2, or 3.
// Postcondition: The return value is the number
// of rings on the specified peg.
int getTopDiameter(int pegNumber)
// Precondition: pegNumber is 1, 2, or 3.
// Postcondition: If countRings(pegNumber) > 0,
// then the return value is the diameter of the top
// ring on the specified peg; otherwise, the return
// value is zero.
void move(int startPeg, int endPeg)
// Precondition: startPeg is a peg number
// (1, 2, or 3), and countRings(startPeg) > 0;
// endPeg is a different peg number (not equal
// to startPeg), and if endPeg has at least one
// ring, then getTopDiameter(startPeg) is
// less than getTopDiameter(endPeg).
// Postcondition: The top ring has been moved
// from startPeg to endPeg.
Also include a method so that a Towers object may be displayed easily.
Use the Towers object in a program that allows a child to play Towers of Hanoi. Make sure you don’t allow the child to make any illegal moves.
Step by Step Answer: