Question

Create in JAVA Suppose you are designing a game called King of the Stacks. The rules...

Create in JAVA

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.

    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.

Homework Answers

Answer #1

Answer:

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:

Note: If you have any doubts or queries please comment I will get back to you.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
*PYTHON 3 * Coin taking game Game rules a) The game starts with 22 coins in...
*PYTHON 3 * Coin taking game Game rules a) The game starts with 22 coins in a common pool b) The goal of the game is to take the last coin c) Players take turns removing 1, 2, or 3 coins from the pool d) A player cannot take more coins than remain in the pool e) After each turn, the number of coins that remain in the pool is announced f) When the last coin is taken, the winner...
You are coding a simple game called Pig. Players take turns rolling a die. The die...
You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If it’s is a: 1: You lose your turn,...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
One of the most popular games of chance is a dice game known as “craps”, played...
One of the most popular games of chance is a dice game known as “craps”, played in casinos around the world. Here are the rules of the game: A player rolls two six-sided die, which means he can roll a 1, 2, 3, 4, 5 or 6 on either die. After the dice come to rest they are added together and their sum determines the outcome. If the sum is 7 or 11 on the first roll, the player wins....
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
After reading the following article, how would you summarize it? What conclusions can be made about...
After reading the following article, how would you summarize it? What conclusions can be made about Amazon? Case 12: Amazon.com Inc.: Retailing Giant to High-Tech Player? (Internet Companies) Overview Founded by Jeff Bezos, online giant Amazon.com, Inc. (Amazon), was incorporated in the state of Washington in July 1994, and sold its first book in July 1995. In May 1997, Amazon (AMZN) completed its initial public offering and its common stock was listed on the NASDAQ Global Select Market. Amazon quickly...
provide 3-4 paragraphs post (team 2) 1-What are 4 key things you learned about the topic...
provide 3-4 paragraphs post (team 2) 1-What are 4 key things you learned about the topic from reading their paper? 2-How does the topic relate to you and your current or past job? 3-Critique the paper in terms of the organization and quality.1- Employee Stress and how it has an Adverse Effect on a Company This paper explores employee stress and how it has an adverse effect on a company, its employees and the organization. Job stress can have a...
What role could the governance of ethics have played if it had been in existence in...
What role could the governance of ethics have played if it had been in existence in the organization? Assess the leadership of Enron from an ethical perspective. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies, collapsed in 2001 under a mountain of debt...