Question

In: Computer Science

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 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

Solution:

Givendata:

Code:

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:

PLEASE GIVEME THUMBUP..........


Related Solutions

Can you please do this in C asap. Promise to leave a awesome rating! Thank you...
Can you please do this in C asap. Promise to leave a awesome rating! Thank you in advance. Question and template below Instructions- LINKED LIST In C 'objects' => structs (struct - collection of data, not methods) Linked List Collection of objects which are all connected to each other Each Object 'points' to the next item in the list Node (struct) ' O ' - data - nextNode Our linked list will store data(structs containing integers) in order from smallest...
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...
C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :)...
C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :) I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each group of adjacent vowels (a, e, i, o, u, y) counts...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
Please can I kindly get a flowchart for this java code. Thank you. //import the required...
Please can I kindly get a flowchart for this java code. Thank you. //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in));...
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...
hello, can you please answer this question. thank you Choose 2 signs or symptoms that are...
hello, can you please answer this question. thank you Choose 2 signs or symptoms that are characteristic of Hilda’s respiratory disease and link them to the pathophysiology of her condition (i.e., explain how the pathophysiological changes cause the signs and symptoms you specified). below is given case study for this question. hope this help Hilda Wilde is a 45-year-old woman who was diagnosed with asthma as a child. She recalls her first asthma attack being horrendous; chest tightness, difficulty breathing,...
I need to update this program to follow the these requirements please and thank you :...
I need to update this program to follow the these requirements please and thank you : Do not use packages Every class but the main routine class must have a toString method . The toString method for Quadrilateral should display its fields: 4 Points. All instance variables should be declared explicitly private . Area incorrect : enter points no: 1 1 3 enter points no: 2 6 3 enter points no: 3 0 0 enter points no: 4 5 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT