Question

In: Computer Science

HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM WITH THE DIFFERNT CLASSES LISTED BELOW. I WILL...

HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM WITH THE DIFFERNT CLASSES LISTED BELOW. I WILL LEAVE AWESOME RATING. THANK YOU IN ADVANCE.

The code needed for this assignment has to somehow implement a stack interface (including a vector stack, array stack, and a linked stack). The classes needed are the Game class, the disk class, the driver, and the stack interface (including arraystack, linkedstack, and vectorstack)

QUESTION

Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:

  •  The game is played with two (2) players.

  •  There are three (3) different Stacks in the game.

  •  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 turns, 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.

  •  The game is played for a set number of turns (N), but there must be at least 20

    turns.

  •  After N turns have elapsed, the game is over. The player who has the most

    disks remaining on the three Stacks combined is the winner

Your task is to write a program that will implement and simulate the King of the Stacks game.

Some requirements for your simulation:

  •  Your program should ask how many turns should be played (N >= 20)

  •  The game will be simulated according to the rules listed above

  •  Your program should simulate the entire game without any user intervention

    after the number of turns is entered. Each player's turn will be simulated by having a random integer (0, 1, 2) generated. Based on the random integer drawn, the player will push her disk onto the appropriate Stack.

  •  The pop timers should be staggered so that a disk is popped from each Stack at the end of every 3rd, 5th, and 7th turns, respectively. (In other words, Stack A will pop off a disk every 3 turns, Stack B will pop off a disk every 5 turns; and Stack C will pop off a disk every 7 turns).

  •  Your simulation should include a way to handle a potential EmptyStackException that is thrown by attempting to pop an empty Stack.

  •  Your program should include print statements stating which turn it is and describing the events of each turn (Player _ push disk onto Stack _; A disk was popped from Stack __; etc.)

  •  At the end of the game, all of the disks should be popped off from each Stack and scores tallied for each player. The results should be announced via a print statement.

  1. Some ground rules:

    •  Your simulation should work for any of the implementations of the Stack ADT. If one were to change the driver to use a different Stack implementation class, your simulation should still work.

    •  Your program should be generalized where possible so that the simulation can easily be modified (e.g., number of turns played, pop timers for each stack, etc.)

    •  Object-oriented principles must be followed; define classes to represent game objects as needed.

    •  Functional decomposition must be followed and helper methods be used where appropriate.

    •  Your program should be well organized and properly commented.

    •  You must test your program to confirm that it works.

Solutions

Expert Solution

Ans)

Java Program



import java.util.Stack;
import java.util.Random;

public class KingOfStacks{
  
// Assume each disk is represented by an integer.
// A value of 1 in stack means the disk was pushed by 1st player
// and similarly for the second player.

// Setting 3 stacks
private Stack<Integer> stackArray[];
  
// Number of turns.
private int N;
// popTimers for each stack
private int popTimer[];
  

public KingOfStacks(int N, int popTimer[]){
this.N = N;
this.popTimer = popTimer;

stackArray = new Stack[3];
for(int i=0; i < 3; i++){
stackArray[i] = new Stack<>();
}
}

void play(){

Random rand = new Random();
// Turns from 1 to N-
for(int turn = 1; turn <= N; turn++){
// stack in which to push the current disk.
int stack_number = rand.nextInt(3);

// If turn is odd, first players turn.
if(turn % 2 == 1){
stackArray[stack_number].push(1);
System.out.printf("Player 1 pushed onto Stack %d\n",stack_number+1);
}
// If turn is even, second players turn.
else{
stackArray[stack_number].push(2);
System.out.printf("Player 2 pushed onto Stack %d\n",stack_number+1);
}

  
// Check if current turn is popTimer of any stack, if it is pop the stack.
for(int i = 0; i < 3; i++){
if(turn % popTimer[i] == 0){
// Check if particular stack is non empty before popping from it.
if(!stackArray[i].empty()){
stackArray[i].pop();
System.out.printf("A disk was popped from Stack %d\n",i+1);
}
}
}
}


// After playing completely, count the score.
int score1 = 0;
int score2 = 0;

for(Stack<Integer> s : stackArray){
while(!s.empty()){
int top = s.pop();
if(top == 1){
score1++;
}
else{
score2++;
}
}
}

System.out.printf("The score of player 1 is : %d\n", score1);
System.out.printf("The score of player 2 is : %d\n", score2);

if(score1 > score2){
System.out.println("The winner is player 1");
}
else if(score2 > score1){
System.out.println("The winner is player 2");
}
else{
System.out.println("The match ends in a draw");
}
}

public static void main(String[] args){
int popTimer[] = {3,5,7};

// Instantiate the object.
KingOfStacks game = new KingOfStacks(20, popTimer);

game.play();
}


}

​​​Output

if your satisfy above answer please give positive rating or?

please don't dislike

Thank you!


Related Solutions

HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN ADVANCE. QUESTION Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each...
Hello, Please write this program in java and include a lot of comments and please try...
Hello, Please write this program in java and include a lot of comments and please try to make it as simple/descriptive as possible since I am also learning how to code. The instructions the professor gave was: Create your own class Your own class can be anything you want Must have 3 instance variables 1 constructor variable → set the instance variables 1 method that does something useful in relation to the class Create a driver class that creates an...
Java Program. Please read carefully. i need the exact same output as below. if you unable...
Java Program. Please read carefully. i need the exact same output as below. if you unable to write the code according to the question, please dont do it. thanks To the HighArray class in the highArray.java program (Listing 2.3), add the following methods: 1.      getMax() that returns the value of the highest key (value) in the array without removing it from the array, or –1 if the array is empty. 2.      removeMax() that removes the item with the highest key from the...
Hello! I am having trouble starting this program in Java. the objective is as follows: "...
Hello! I am having trouble starting this program in Java. the objective is as follows: " I will include a text file with this assignment. It is a text version of this assignment. Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number. " my question is, how do...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Hello, A summary of the article below I need a summary of the article below please....
Hello, A summary of the article below I need a summary of the article below please. Thank you As Coronavirus Surveillance Escalates, Personal Privacy Plummets Tracking entire populations to combat the pandemic now could open the doors to more invasive forms of government snooping later. In January, South Korea began posting detailed location histories about people who tested positive for the coronavirus, leading to public blaming and shaming.Credit...Woohae Cho for The New York Times By Natasha Singer and Choe Sang-Hun...
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
Hello, This is an Array Search Program. Details will be provided below and please don't hesitate...
Hello, This is an Array Search Program. Details will be provided below and please don't hesitate to contact me if you need anything. 1- Brief Introduction: Swapping Values Swapping consecutive values in an array: ... Before After |---| |---| arr[2] | 8 | | 1 | |---| |---| arr[3] | 1 | | 8 | |---| |---| ... # Define an array, print the array before the swap arr = [4, 0, 8, 1, 7] print(arr) # Output: [4, 0,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT