Answered step by step
Verified Expert Solution
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 rounds There are three 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 rounds in the game n
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 mainString args
int stackCount ;
Scanner scanner new ScannerSystemin;
int n;
do
System.out.printEnter the number of rounds minimum : ;
n scanner.nextInt;
while n ;
int popInterval ;
Create stacks to hold disks.
List stacks new ArrayListstackCount;
for int i ; i stackCount; i
stacks.addnew ArrayList;
Create two players.
Player player new Player;
Player player new Player;
Random random new Random;
Determine which player is playing this round.
for int round ; round n; round
Choose a random stack to push the disk onto.
int stackIndex random.nextIntstackCount;
Determine which player is playing this round.
Player currentPlayer round player : player;
Create a new disk owned by the current player.
Disk disk new DiskcurrentPlayergetPlayerNumber round;
stacks.getstackIndexadddisk;
Print the result of this round
System.out.printlnRound round : Player currentPlayer.getPlayerNumber pushed Disk disk.getUniqueID onto Stack stackIndex ;
Check if it's time to pop disks from the stacks.
if round popInterval
for int i ; i stackCount; i
if stacks.getiisEmpty
Disk poppedDisk stacks.getiremovestacksgetisize;
Player owner poppedDisk.getOwner player : player;
owner.incrementScore;
System.out.printlnRound round : Disk poppedDisk.getUniqueID popped from Stack i by Player owner.getPlayerNumber;
Determine the winner based on the scores.
int playerScore playergetScore;
int playerScore playergetScore;
System.out.printlnPlayer Score: playerScore;
System.out.printlnPlayer Score: playerScore;
if playerScore playerScore
System.out.printlnPlayer wins!";
else if playerScore playerScore
System.out.printlnPlayer wins!";
else
System.out.printlnIts a tie!";
scanner.close;
class Disk
private int owner; Player number
private int uniqueID; Unique identifier for the disk
public Diskint 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 Playerint playerNumber
this.playerNumber playerNumber;
this.score ;
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
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