1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
AnnyKZ [126]
3 years ago
7

This question involves a simulation of a two-player game. In the game, two simulated players each start out with an equal number

of coins. In each round, each player chooses to spend either 1, 2, or 3 coins. Coins are then awarded to each player according to the following rules.
Same rule: If both players spend the same number of coins, player 2 gains 1 coin.
Off-by-one rule: If the players do not spend the same number of coins and the positive difference between the number of coins spent by the two players is 1, player 2 is awarded 1 coin.
Off-by-two rule: If the players do not spend the same number of coins and the positive difference between the number of coins spent by the two players is 2, player 1 is awarded 2 coins.

The game ends when the specified number of rounds have been played or when a player’s coin count is less than 3 at the end of a round.

The CoinGame class is shown below. You will write two methods in the CoinGame class.

public class CoinGame

{

private int startingCoins; // starting number of coins

private int maxRounds; // maximum number of rounds played



public CoinGame(int s, int r)

{

startingCoins = s;

maxRounds = r;

}



/** Returns the number of coins (1, 2, or 3) that player 1 will spend.

*/

public int getPlayer1Move()

{

/* implementation not shown. */

}



/** Returns the number of coins (1, 2, or 3) that player 2 will spend, as described in part (a).

*/

public int getPlayer2Move(int round)

{

/* to be implemented in part (a) */

}



/** Plays a simulated game between two players, as described in part (b).

*/

public void playGame()

{

/* to be implemented in part (b) */

}

}

In the simulation, player 2 will always play according to the same strategy. The number of coins player 2 spends is based on what round it is, as described below.

(a) You will write method getPlayer2Move, which returns the number of coins that player 2 will spend in a given round of the game. In the first round of the game, the parameter round has the value 1, in the second round of the game, it has the value 2, and so on. The method returns 1, 2, or 3 based on the following rules.

If round is divisible by 3, then return 3.
If round is not divisible by 3 but is divisible by 2, then return 2.
If round is not divisible by 3 and is not divisible by 2, then return 1.
Complete method getPlayer2Move below by assigning the correct value to result to be returned.

/** Returns the number of coins (1, 2, or 3) that player 2 will spend, as described in part (a).

*/

public int getPlayer2Move(int round)

{

int result;

return result;

}

Write the method playGame, which simulates a game between player 1 and player 2, based on the rules and example shown at the beginning of the question. Both player 1 and player 2 start the game with startingCoins coins. Computer player 1 spends 1, 2, or 3 coins based on the value returned by the method getPlayer1Move(). Computer player 2 spends 1, 2, or 3 coins based on the value returned by the method getPlayer2Move().

The game ends when maxRounds rounds have been played or when a player’s coin count is less than 3 at the end of a round.

At the end of the game, the winner is determined according to the following rules.

If both players have the same number of coins at the end of the game, the method prints "tie game".
If player 1 has more coins than player 2, the method prints "player 1 wins".
If player 2 has more coins than player 1, the method prints "player 2 wins".
(b) Assume that getPlayer2Move works as specified, regardless of what you wrote in part (a) . You must use getPlayer1Move and getPlayer2Move appropriately to receive full credit.

Complete method playGame below.

/** Plays a simulated game between two players, as described in part (b).

*/

public void playGame()
Computers and Technology
1 answer:
Vlada [557]3 years ago
7 0

[A] Let's simplify the <em>getPlayer2Move </em>rules first.

  • If round is divisible by 3, then return 3

if a number is divisible by another number that means the remainder of the division is 0. We can write the code as follows:

if (round%3 == 0)

result = 3;

  • If round is not divisible by 3 but is divisible by 2, then return 2.

Same as the previous one, we are going to use the remainder operation. We can use an else-if condition here.

else if (round%2 == 0)

result = 2;

  • If round is not divisible by 3 and is not divisible by 2, then return 1.

An else would work fine here.

else

result = 1;

<em>The full code of part A is attached below.</em>

[B]

<em>playGame </em>method:

Let's have a quick look at the game rules first:

  • If both players spend the same number of coins, player 2 gains 1 coin.

This can be translated to <em>if player1Spending == player2Spending</em>, then player2 gets a coin.

  • If the players do not spend the same number of coins and the positive difference between the number of coins spent by the two players is 1, player 2 is awarded 1 coin.

If the absolute value of <em>(player1Spending - player2Spending == 1) </em>then player2 gets awarded.

  • If the players do not spend the same number of coins and the positive difference between the number of coins spent by the two players is 2, player 1 is awarded 2 coins.

We can either use an else statement or a separate conditional statement here, but I'll go for the else statement.

We can use a while loop to detect when the game ends, either a player's coins is less than 3 OR when the maxRounds have been played.

You might be interested in
The analysts at Techno Info Systems are considering the four-model approach to system development for a new client. If they make
Natali [406]

Answer:

Option B is correct.

Explanation:

Within Techno Info Systems the user has been analyst contemplating that four-model concept towards computer development for such its new customer. When they adopt that four-model method, thus the result seems to be the time taken to implement the rises in the method.

So, therefore the other options are incorrect according to the following scenario.

4 0
3 years ago
when you are editing a word precessing document, what will tell the word processing program where in the document you are making
vfiekz [6]
You are usually editing a word document when you are finished typing up and assignment or what not, then highlighting what you want to edit will tell the computer/word document what and where your making your edits.
4 0
3 years ago
Who are the four main antagonists in fnaf 1
ikadub [295]

Answer:

lol it's foxy freddy bonnie and chica

5 0
3 years ago
Read 2 more answers
The sender side of rdt3.0 simply ignores (that is, takes no action on) all received packets that are either in error or have the
Rom4ik [11]

Answer:

It might not work efficiently.

Explanation:

The function of the protocol rtd3.0 is to transfer data to a receiver from a sender.

As soon as the receiver received the packet transferred by the sender, we respond acknowledge (Ack) to the sender so that sender can confirm the receiver has gotten it.  

The receiver will not send any acknowledgment if the packet he receives are such that bits contain error or not in order.

After that timeout, the packet will be re-transmitted by the sender.

It is then possible the protocol might seem to be inefficient if a packet is sent many times, because other packets will have to wait to sent until the current packet is sent successfully.

The solution to prevent this kind of issue is to allow for the occurrence of premature timeouts.

7 0
3 years ago
Write a program that creates a map containing the U.S. states as keys, and their capitals as values. (Use the Internet to get a
seraphim [82]

Answer:

Here is the Python program:

import random  #  to generate random numbers

def quiz():  #method quiz to ask state's capital

   capitals={"Washington":"Olympia","Oregon":"Salem",\

                   "California":"Sacramento","Ohio":"Columbus",\

                   "Nebraska":"Lincoln","Colorado":"Denver",\

                   "Michigan":"Lansing","Massachusetts":"Boston",\

                   "Florida":"Tallahassee","Texas":"Austin",\

                   "Oklahoma":"Oklahoma City","Hawaii":"Honolulu",\

                   "Alaska":"Juneau","Utah":"Salt Lake City",\

                   "New Mexico":"Santa Fe","North Dakota":"Bismarck",\

                   "South Dakota":"Pierre","West Virginia":"Charleston",\

                   "Virginia":"Richmond","New Jersey":"Trenton",\

                   "Minnesota":"Saint Paul","Illinois":"Springfield",\

                   "Indiana":"Indianapolis","Kentucky":"Frankfort",\

                   "Tennessee":"Nashville","Georgia":"Atlanta",\

                   "Alabama":"Montgomery","Mississippi":"Jackson",\

                   "North Carolina":"Raleigh","South Carolina":"Columbia",\

                   "Maine":"Augusta","Vermont":"Montpelier",\

                   "New Hampshire":"Concord","Connecticut":"Hartford",\

                   "Rhode Island":"Providence","Wyoming":"Cheyenne",\

                   "Montana":"Helena","Kansas":"Topeka",\

                   "Iowa":"Des Moines","Pennsylvania":"Harrisburg",\

                   "Maryland":"Annapolis","Missouri":"Jefferson City",\

                   "Arizona":"Phoenix","Nevada":"Carson City",\

                   "New York":"Albany","Wisconsin":"Madison",\

                   "Delaware":"Dover","Idaho":"Boise",\

                   "Arkansas":"Little Rock","Louisiana":"Baton Rouge"}

#dictionary of states along with their capitals    

   quit=False   #exit option

   print ("Welcome to the States capitals quiz game!")  

   wrong_ans = False   # boolean variable for incorrect answer

   while not quit and len(capitals)>0:  

#loop continues until all state capitals are finished and user presses q

       choice = random.choice(list(capitals.keys()))  

#choice variable stores randomly generated list of pick

       right_ans = capitals.get(choice)  #stores correct answer

       print ("\nWhat is the capital city of",choice,"?")

       user_ans = input("Your guess:  ")  #answer entered by user

       if user_ans.lower()=='q':  #if user presses q to quit

           quit=True  #quit value become true

           print("The quiz is over! Bubye!")  

#displays the above message and quits the program

       elif user_ans.lower()==right_ans.lower():  #if user enters correct guess

           print ("Right Answer! \n")  # displays this message

       else:  #if user enters wrong guess

           print ("Wrong Answer!\n")  #displays wrong answer message

           print ("The right answer is ",right_ans)  #prints right capital

           wrong_ans = True          

quiz()       #calls quiz function to begin with the quiz

Explanation:

The program has a dictionary that contains the US states as keys and their capitals as values. The program asks the user to enter the capital of a random state. This state is selected randomly from the list of states using random function and keys() method is used to display list of all keys in the capital states dictionary. The while loop starts which keeps asking the user to guess the capital of states. This loop ends when the user types q to quit the program or all the capitals of the states are covered. Now the user guess is compared to the right answer, if the user has correctly guessed the capital then the Right answer message is displayed otherwise Wrong answer is displayed. After Wrong answer message the correct answer  is displayed and the quiz starts again until the user enters q to exit. When the user types q the message: The quiz is over! Bubye! is displayed and the programs exits.

8 0
4 years ago
Other questions:
  • A business that subscribes to a specific computing model has its entire system fully functional within a short time. What benefi
    7·1 answer
  • The purpose of the ________ element is used to configure the headings for a web page document or section of a web page document.
    11·1 answer
  • What does festive mean
    11·2 answers
  • Which of the following is a camera problem that dirt can cause?
    8·2 answers
  • Which of the following is NOT an example of a crowdsourcing website?
    15·1 answer
  • 2. How do cell phone users stay connected to the network as users move between cells?
    10·1 answer
  • Suppose you are given a sequence that is described by a formula, starting at index n=0. If you need to change the starting index
    6·1 answer
  • Requirements description:
    6·1 answer
  • How to create a trace table
    10·1 answer
  • What mechanism can organizations use to prevent accidental changes by authorized users?.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!