answer.
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
charle
28 days 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:
Natasha_Volkova [897]28 days ago
7 0

[A] Let’s first clarify the getPlayer2Move rules.

  • If the round is divisible by 3, return 3.

A number is divisible by another if the result of the division leaves no remainder. The code representation would be:

if (round%3 == 0)

result = 3;

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

Using the remainder operation again, we can implement this with an else-if statement:

else if (round%2 == 0)

result = 2;

  • If neither condition holds, return 1.

Using an else statement works well here.

else

result = 1;

The complete code for part A is listed below.

[B]

playGame method:

Let’s examine the game rules quickly:

  • If the coin expenditure is identical for both players, player 2 receives 1 coin.

This can be expressed as if player1Spending == player2Spending, resulting in player 2 acquiring a coin.

  • If the spent amounts differ by 1 coin, player 2 earns 1 coin.

In cases where the absolute difference of (player1Spending - player2Spending == 1) , player 2 receives a coin.

  • If the difference is 2 coins, player 1 earns 2 coins.

Here, either using an else statement or a separate condition is viable, though I prefer the else statement.

A while loop can be employed to recognize when the game concludes, either when a player has fewer than 3 coins or when maxRounds have transpired.

You might be interested in
Allison wants to use equations to simplify the process of explaining something to the Sales team, but the default eq
ivann1987 [930]

Response:

Look for a solution that meets her requirements

Clarification:

Various strategies exist for problem-solving, with the method chosen depending on how the problem is defined. Not every process can be expressed with mathematical equations; some are theoretical and can be clarified using simple logic.

If Allison can't find suitable equations among the standard options, she might create one that addresses the ongoing process. If she can't devise such an equation, other suitable methods should be utilized to convey the necessary concept to the team.

7 0
14 days ago
Read 2 more answers
A router has a valid operating system and a configuration file stored in nvram. the configuration file contains an enable secret
oksian1 [797]
The mode displayed is user EXEC mode. This mode is utilized for configuring, observing, and testing system processes. Typically, user EXEC commands allow connection to remote devices, temporary modification of terminal line settings, conducting basic tests, and displaying system details. This mode is commonly accessed by standard system admins, while privileged EXEC mode is reserved for the root administrator. Switch between these two levels using the enable and disable commands. A valid password is required to access the user-level EXEC command line.
8 0
12 days ago
Any software or program that comes in many forms and is designed to disrupt the normal operation of a computer by allowing an un
ivann1987 [930]
Trojan horse. They arrive disguised.
8 0
15 days ago
Tanya wants to include a video with all its controls on her web page. The dimensions of the video are as follows:
Natasha_Volkova [897]
I believe the correct answer is formats
4 0
12 days ago
Read 2 more answers
6. A small design agency you are consulting for will be creating client websites and wants to purchase a web server so they can
amid [800]

Answer:

Clarification:

The most effective recommendation for the agency would be to ensure they fully grasp the overall ownership costs of the server. This encompasses not only the server itself but also various factors including necessary software, an IT server manager, facility expenses, security investments, and backup options. Although these are key costs they will face, there may be additional unexpected expenses. Consequently, the best approach is to supply them with comprehensive information for making an informed decision.

3 0
18 days ago
Other questions:
  • 9) If you are working on the part of 5-15 minutes, time-stamping (every 30 seconds) should start at: a) [00:05:00] b) [00:00:00]
    9·1 answer
  • In what areas is Leslie's underspending hurting her budget select all that apply
    5·1 answer
  • Which of the following type of online advertising intermediaries decide the placement and pricing of online display ads by using
    9·1 answer
  • Remember for a moment a recent trip you have made to the grocery store to pick up a few items. What pieces of data did the Point
    10·1 answer
  • Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is: 3 8 the output is
    5·1 answer
  • Form the recurrence relations (RRs) for the number of vertices and the number of edges of a hypercube of n dimensions, Hn. Solve
    13·1 answer
  • A developer writes a trigger on the Account object on the before update event that increments a count field. A workflow rule als
    12·1 answer
  • Assign max_sum with the greater of num_a and num_b, PLUS the greater of num_y and num_z. Use just one statement. Hint: Call find
    5·1 answer
  • Fill the validateForm function to check that the phone number contains a number (use the isNaN function) and that the user name
    10·1 answer
  • The principal that users have access to only network resources when an administrator explicitly grants them is called __________
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!