Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please revise this code in java so that it uses the stack data structure and not a list / the program is a game that

please revise this code in java so that it uses the stack data structure and not a list/ the program is a game that impletements the king of the stack game. You can run the program to see how it works, minimun 35 rounds There are three (3) different Stacks in the game.
The game is played for a set number of rounds (n), where a round is when both players
take a turn. There must be at least 30 rounds in the game (n >=30).
Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players
alternate turns throughout the game. Each disk will include some marker to denote to
whom it belongs.
At the end of certain rounds, spaced at regular intervals, the top disk is automatically
popped from the Stacks. The pop timer is staggered so that disks are popped off from
different Stacks at different times.
After n rounds have elapsed, the game is over. The player who has the most disks
remaining on the three Stacks combined is the winner. here is the code import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class KingOfTheStacks {
public static void main(String[] args){
int stackCount =3;
Scanner scanner = new Scanner(System.in);
int n;
do {
System.out.print("Enter the number of rounds (minimum 30): ");
n = scanner.nextInt();
} while (n <30);
int popInterval =5;
// Create stacks to hold disks.
List> stacks = new ArrayList<>(stackCount);
for (int i =0; i < stackCount; i++){
stacks.add(new ArrayList<>());
}
// Create two players.
Player player1= new Player(1);
Player player2= new Player(2);
Random random = new Random();
// Determine which player is playing this round.
for (int round =1; round <= n; round++){
// Choose a random stack to push the disk onto.
int stackIndex = random.nextInt(stackCount);
// Determine which player is playing this round.
Player currentPlayer = round %2==1? player1 : player2;
// Create a new disk owned by the current player.
Disk disk = new Disk(currentPlayer.getPlayerNumber(), round);
stacks.get(stackIndex).add(disk);
//Print the result of this round
System.out.println("Round "+ round +": Player "+ currentPlayer.getPlayerNumber()+" pushed Disk "+ disk.getUniqueID()+" onto Stack "+(stackIndex +1));
// Check if it's time to pop disks from the stacks.
if (round % popInterval ==0){
for (int i =0; i < stackCount; i++){
if (!stacks.get(i).isEmpty()){
Disk poppedDisk = stacks.get(i).remove(stacks.get(i).size()-1);
Player owner = poppedDisk.getOwner()==1? player1 : player2;
owner.incrementScore();
System.out.println("Round "+ round +": Disk "+ poppedDisk.getUniqueID()+" popped from Stack "+(i +1)+" by Player "+ owner.getPlayerNumber());
}
}
}
}
// Determine the winner based on the scores.
int player1Score = player1.getScore();
int player2Score = player2.getScore();
System.out.println("Player 1 Score: "+ player1Score);
System.out.println("Player 2 Score: "+ player2Score);
if (player1Score > player2Score){
System.out.println("Player 1 wins!");
} else if (player2Score > player1Score){
System.out.println("Player 2 wins!");
} else {
System.out.println("It's a tie!");
}
scanner.close();
}
}class Disk {
private int owner; // Player number
private int uniqueID; // Unique identifier for the disk
public Disk(int owner, int uniqueID){
this.owner = owner;
this.uniqueID = uniqueID;
}
public int getOwner(){
return owner;
}
public int getUniqueID(){
return uniqueID;
}
}class Player {
private int playerNumber;
private int score;
public Player(int playerNumber){
this.playerNumber = playerNumber;
this.score =0;
}
public int getPlayerNumber(){
return playerNumber;
}
public int getScore(){
return score;
}
public void incrementScore(){
score++;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is your least favorite U.S. dialect? Why?

Answered: 1 week ago