Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are to create the game for playing the Towers of Hanoi game. Your program wi

ID: 3888634 • Letter: Y

Question

You are to create the game for playing the Towers of Hanoi game. Your program will not play the game itself, but will allow the user to play the game.

This is how the game works:
There are 3 pegs. On the first peg are some number of rings. Each ring is a unique diameter. At the start of the game, all the rings are on the first peg and they are stacked in order of the largest diameter on the bottom to the smallest diameter on the top.

The object of the game is to get all of the rings on to peg number 2 stacked in the same order, that is from the largest diameter ring on the bottom to the smallest diameter ring on the top.

The rules for playing the game are thus:

Only one ring may be moved at a time.

A larger ring may never be stacked on top of a smaller ring at any time.

Only the top ring on a stack may be moved.

A ring can be moved to any peg, so long as rule number 2 is not violated.

A ring must be moved to a different peg than the one it is currently on.

We will number the rings, and the ring number will represent the diameter of the ring in inches. So, ring number 1 will be 1 inch in diameter, ring number 2 will be 2 inches in diameter, ring number 3 will be 3 inches in diameter, etc.

When I play the game I should see some representation of the 3 pegs and the rings that are on them. So if I am playing the game with 5 rings, at the start of the game I should see something like this:

peg1 peg2 peg3

1 2 3 4 5

Then there should be a question asking me which peg to move the top item from and which peg to move it to. Then, after entering that information, I should see the resulting status of the pegs and rings:

peg1 peg2 peg3

21 3
4
5

[page1image20616] [page1image20784] [page1image20944]

Page 1 of 3

Towers Of Hanoi game

[page2image976]

Then there should be a question asking me which peg to move the top item from and which peg to move it to. Then, after entering that information, I should see the resulting status of the pegs and rings:

peg1 peg2 peg3

321 4
5

Then there should be a question asking me which peg to move the top item from and which peg to move it to. Then, after entering that information, I should see the resulting status of the pegs
and rings:

peg1 peg2 peg3

31 42 5

And so on until the game is finished.

You will create three classes, each in a separate file: a class named Peg, a class named Towers, and a class named TowersGame.

The Peg class will keep track of the rings stacked on a peg. The peg may hold up to 64 rings, with each ring having its own diameter. Also, there is a rule that requires each ring to be smaller than any ring underneath it. The class's methods should include:

a) a constructor that places n rings on the peg (where n may be as large as 64), and these n rings have diameters from n inches on the bottom to one inch on the top;

b) an accessor method that returns the number of rings on the peg;

c) an accessor method that returns the diameter of the topmost ring,

d) a method that adds a new ring to the top (with the diameter of the ring as a parameter to the

method);

e) a method that removes the topmost ring.

Make sure that all methods have appropriate preconditions to guarantee that the rule about ring sizes is enforced. Create private instance variables as needed and appropriate.

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 of the Peg class. The towers class methods are:

Towers(int numRings)
Precondition: 1 <= numRings <= 64
Postcondition: The towers have been initialized with numRings 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 numRings 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.

[page2image23360] [page2image23520]

Page 2 of 3

Towers Of Hanoi game

[page3image960]

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: stargPeg is a pegnumber (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.

void displayTowers()
Precondition: the game has started
Postcondition: a display indicating each peg and the stack of rings on each peg has been

displayed with the smallest ring shown on top.

The TowersGame class will contain the main method for the program which will contain the game loop. The main method will ask the user how many rings does he/she wish to play with. The main method will contain a loop that will loop as long as the user wishes to keep making moves. There will also be a method to validate each move:

boolean validMove(Towers game, int startPeg, int endPeg)
Precondition: game is a reference to the Towers object; startPeg contains the peg number

that the ring is being moved from; endPeg contains the peg number that the

ring is being moved to.
Posecondition: It has been determined whether or not the appropriate game rules have

been violated. If any of them have, an appropriate error message is displayed and false has been returned. If no rules have been violated, true has been returned

Explanation / Answer

Given below are the classes needed. Since the programming language is not specified , implemented in Java. Also the images are not loading for the output. Please do rate the answer if it helped. Thank you very much.

Peg.java

public class Peg {

private int rings[];

private int numRings; // number of rings on the peg

public Peg(int n) //constructro initializes the peg with specified number of rings

{

rings = new int[64];

for(int i = 0 ; i < n; i++ )

rings[i] = n - i;

numRings = n;

}

public int getNumRings() //reutrn the number of rings on teh peg

{

return numRings;

}

//precondition: getNumRings() != 0

public int getTopDiameter() //gets the diameter of top ring

{

return rings[numRings - 1];

}

//precondition: getTopDiameter() < ringDiameter

public void placeOnTop(int ringDiameter) //places the ring with specified diameter on top

{

rings[numRings ++] = ringDiameter;

}

//precondition : getNumRings() != 0

public int removeTopRing() //returns the diameter of the top ring removed

{

int r = rings[numRings - 1];

numRings--;

return r;

}

public void display()

{

for(int i = numRings - 1; i >= 0; i--)

System.out.println(rings[i]);

System.out.println();

}

}

Towers.java

public class Towers {

private Peg pegs[];

public Towers(int numRings)

{

pegs = new Peg[3];

pegs[0] = new Peg(numRings);

pegs[1] = new Peg(0);

pegs[2] = new Peg(0);

}

//precondition: 1<= pegNumber <= 3

public int countRings(int pegNumber)

{

return pegs[pegNumber - 1].getNumRings();

}

//precondition: 1<= pegNumber <= 3

public int getTopDiameter(int pegNumber)

{

return pegs[pegNumber - 1].getTopDiameter();

}

//precondition: 1<= startPeg <= 3, 1<= endPeg <= 3, startPeg != endPeg, rules of TOH are followed

public void move(int startPeg, int endPeg)

{

int ring = pegs[startPeg - 1].removeTopRing();

pegs[endPeg - 1].placeOnTop(ring);

}

public void displayTowers()

{

for(int i = 0; i < 3; i++)

{

System.out.println("Peg " + (i+1));

pegs[i].display();

}

}

}

TowerGame.java


import java.util.Scanner;
public class TowersGame {
private static boolean validMove(Towers game, int startPeg, int endPeg)
{
if(startPeg != endPeg && game.countRings(startPeg) != 0)
{
if(game.countRings(endPeg) == 0)
return true;
else
{
if(game.getTopDiameter(startPeg) < game.getTopDiameter(endPeg))
return true;
else
return false;
}
}
return false;
}
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
int n ;
do
{
System.out.print("How many rings [1-64] ? ");
n = keybd.nextInt();
}while(n < 1 || n > 64);
Towers game = new Towers(n);
String ans = "y";
game.displayTowers();
int from, to;
while(true)
{
System.out.print("Make a move? (y/n) ");
ans = keybd.next();
if(!ans.equalsIgnoreCase("y"))
break;
do
{
System.out.print("From Peg [1-3]: ");
from = keybd.nextInt();
}while(from < 1 || from > 3);
do
{
System.out.print("To Peg [1-3]: ");
to = keybd.nextInt();
}while(to < 1 || to > 3);
if(validMove(game, from, to))
{
game.move(from, to);
game.displayTowers();
}
else
{
System.out.println("Not a valid move!");
}
}
}
}

output

How many rings [1-64] ? 4
Peg 1
1
2
3
4

Peg 2

Peg 3

Make a move? (y/n) y
From Peg [1-3]: 1
To Peg [1-3]: 2
Peg 1
2
3
4

Peg 2
1

Peg 3

Make a move? (y/n) y
From Peg [1-3]: 1
To Peg [1-3]: 3
Peg 1
3
4

Peg 2
1

Peg 3
2

Make a move? (y/n) y
From Peg [1-3]: 2
To Peg [1-3]: 3
Peg 1
3
4

Peg 2

Peg 3
1
2

Make a move? (y/n) y
From Peg [1-3]: 1
To Peg [1-3]: 3
Not a valid move!
Make a move? (y/n) y
From Peg [1-3]: 2
To Peg [1-3]: 3
Not a valid move!
Make a move? (y/n) y
From Peg [1-3]: 1
To Peg [1-3]: 2
Peg 1
4

Peg 2
3

Peg 3
1
2

Make a move? (y/n) y
From Peg [1-3]: 3
To Peg [1-3]: 1
Peg 1
1
4

Peg 2
3

Peg 3
2

Make a move? (y/n) y
From Peg [1-3]: 3
To Peg [1-3]: 2
Peg 1
1
4

Peg 2
2
3

Peg 3

Make a move? (y/n) y
From Peg [1-3]: 1
To Peg [1-3]: 2
Peg 1
4

Peg 2
1
2
3

Peg 3

Make a move? (y/n) y
From Peg [1-3]: 1
To Peg [1-3]: 3
Peg 1

Peg 2
1
2
3

Peg 3
4

Make a move? (y/n) n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote