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
2 months 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 [1K]2 months 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
Delete Prussia from country_capital. Sample output with input: 'Spain:Madrid,Togo:Lome,Prussia: Konigsberg' Prussia deleted? Yes
Amiraneli [1052]

Answer:

Explanation:

When removing items from a dictionary, always specify the key in quotation marks.

For example: del country_capital['Prussia']

Failing to place it in quotes will cause the interpreter to view Prussia as a variable, resulting in an error saying that Prussia is not defined.

Code:

user_input=input("") #taking input from user

entries=user_input.split(',')    

country_capital=dict(pair.split(':') for pair in entries) #creating the dictionary from user input

del country_capital['Prussia'] #removing Prussia; failure to include quotes results in an error

print('Prussia deleted?', end=' ')

if 'Prussia' in country_capital: #verifying the presence of Prussia in the dictionary

print('No.')

else:

print('Yes.')

print ('Spain deleted?', end=' ')    

if 'Spain' in country_capital: #checking if Spain is present in the dictionary

print('No.')

else:

print('Yes.')

print ('Togo deleted?', end=' ') #verifying the existence of Togo in country_capital

if 'Togo' in country_capital:

print('No.')

else:

print('Yes.')

Explanation:

3 0
2 months ago
A method countDigits(int num) of class Digits returns the remainder when the input argument num(num > 0) is divided by the nu
ivann1987 [1066]

Answer:

#include <iostream>

using namespace std;

class Digits

{

   public:

   int num;

   int read()       //method to read num from user

   {

       cout<<"Enter number(>0)\n";

       cin>>num;

       return num;

   }

   int digit_count(int num)  //method to count number of digits of num

   {

       int count=0;

       while(num>0)    //loop till num>0

       {

           num/=10;

           count++;   //counter which counts number of digits

       }

       return count;

   }

   int countDigits(int num)   //method to return remainder

   {

       int c=digit_count(num); //calls method inside method

       return num%c;  

   }

};

int main()

{

   Digits d;    //object of class Digits is created

   int number=d.read();   //num is read from user

   cout<<"\nRemainder is: "<<d.countDigits(number);  //used to find remainder

   return 0;

}

Output:

Enter number(>0)

343

Remainder is: 1

Explanation:

The program has a logical error that needs rectification. A correctly structured program calculates the remainder when a number is divided by the count of its digits. A class named Digits is created, consisting of the public variable 'num' and methods for reading input, counting digits, and calculating the remainder.

  • read() - This function asks the user to enter the value for 'num' and returns it.
  • digit_count() - This function accepts an integer and counts how many digits it has, incrementing a counter until 'num' is less than or equal to 0. It ultimately returns the digit count.
  • countDigits() - This function takes an integer and delivers the remainder from dividing that number by its digit count. The digit count is computed using the 'digit_count()' method.

Finally, in the main function, a Digits object is instantiated, and its methods are utilized to produce an output.

7 0
2 months ago
Define a method printAll() for class PetData that prints output as follows with inputs "Fluffy", 5, and 4444. Hint: Make use of
ivann1987 [1066]

Answer:

public void printAll(){ // member function petAll()

   super.printAll(); //  invokes printAll() method from the superclass (base class) AnimalData using the super keyword

   System.out.print(", ID: " + idNum);} // displays the ID saved in the idNum field of the PetData class

Explanation:

This is the full PetData class:

public class PetData extends AnimalData { //

private int idNum;

public void setID(int petID) {

idNum = petID; }

// FIXME: Implement printAll() member method

/* Your solution goes here */

public void printAll(){

   super.printAll();

   System.out.print(", ID: " + idNum);}  }

The PetData class is a subclass that inherits from the base class AnimalData.

This class contains a private field, idNum, which is designated for storing the ID value.

It includes a method setID which takes petID as a parameter that corresponds to idNum. Essentially, this acts as a mutator to set the user ID.

Additionally, there is a printAll() method that makes use of the super keyword to call the printAll() method from the base class, AnimalData.

Using super refers to objects of the base class.

Here, the super keyword is utilized with the method name from the subclass PetData to avoid confusion, as both AnimalData and PetData have a method named printAll().

During the main() method, users will enter values for userName, UserAge, and UserID. For example, if the user inputs Fluffy as the name, 5 as the age, and 4444 as the userID. Then, calling userPet.printAll(); will execute the printAll() method from the PetData class since userPet is an instance of PetData.

When this method executes, it will call the printAll() of AnimalData class which outputs the Name and Age, while printAll() of PetData prints the ID. Consequently, the resulting output will be:

Name: Fluffy, Age: 5, ID: 4444

8 0
2 months ago
What are some commands found in the Sort Options dialog box? Check all that apply.
8_murik_8 [964]
1) Arrange from top to bottom; 2) Sort from A to Z; 3) 4) Organize left to right; 5) Case sensitive. Explanation: It is vital to sort data when analyzing it. You can organize text alphabetically, create a list of items from lowest to highest, or sort rows based on colors or icons. Effective data sorting enhances both organization and comprehension. Some of the commands available in the Sort Options dialog box include: Sort by A-Z or Z-A (organizing names or texts alphabetically in increasing or decreasing order); Case sensitive sorting, found under options in the sort dialog; Sort top to bottom, which can also be reversed; Sort left to right, located under orientation in the Sort Options dialog.
3 0
1 month ago
Read 2 more answers
The machine has to be returned to the vendor for proper recycling. Which stage of the hardware lifecycle does this scenario belo
Natasha_Volkova [1026]

Answer:

Decommission/Recycle

Explanation:

The situation being described pertains to the last phase of the hardware lifecycle called Decommission/Recycle. At this stage, the asset has fulfilled its purpose for a long time and is not performing optimally, or newer models have been released. Once it reaches this phase, the hardware is either repaired or dismantled for components that can be used to produce new products.

8 0
1 month ago
Other questions:
  • Explain how abstraction is used in a GPS system
    12·2 answers
  • In this code, identify the repeated pattern and replace it with a function called month_days, that receives the name of the mont
    14·1 answer
  • A retailer is able to track which products draw the most attention from its customers through the use of 5g-enabled motion senso
    5·1 answer
  • Define a method calcPyramidVolume with double data type parameters baseLength, baseWidth, and pyramidHeight, that returns as a d
    8·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
  • Tanya wants to include a video with all its controls on her web page. The dimensions of the video are as follows:
    6·2 answers
  • Write a generator function named count_seq that doesn't take any parameters and generates a sequence that starts like this: 2, 1
    5·1 answer
  • The ____________ protocol enables two users to establish a secret key using a public-key scheme based on discrete logarithms
    10·1 answer
  • Your employer, yPlum Corporation is manufacturing two types of products: Mirabelle smartphone, and Blackamber laptop. The compan
    14·1 answer
  • Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!