Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Answer must be in C++. Thanks. Children often play the game of rock, paper, and scissors. This game has two players, each of whom chooses

Answer must be in C++. Thanks.

Children often play the game of rock, paper, and scissors. This game has two players, each of whom chooses one of the three objects: rock, paper, or scissors. If player 1 chooses rock and player 2 chooses paper, player 2 wins the game because paper covers the rock. The game is played according to the following rules:

If both players choose the same object, this play is a tie.

If one player chooses rock and the other chooses scissors, the player choosing the rock wins this play because the rock breaks the scissors.

If one player chooses rock and the other chooses paper, the player choosing the paper wins this play because the paper covers the rock.

If one player chooses scissors and the other chooses paper, the player choosing the scissors wins this play because the scissors cut the paper.

Write an interactive program that allows two people to play this game.

Problem Analysis and Algorithm Design

Two players play this game. Players enter their choices via the keyboard. Each player enters R or r for Rock, P or p for Paper, or S or s for Scissors. While the first player enters a choice, the second player looks elsewhere. Once both entries are in, if the entries are valid, the program outputs the players choices and declares the winner of the play. The game continues until one of the players decides to quit the game. After the game ends, the program outputs the total number of plays and the number of times that each player won. This discussion translates into the following algorithm:

Provide a brief explanation of the game and how it is played. You can simply use the code below:

void displayRules()

{

cout

cout

cout

cout

cout

cout

cout

cout

cout

}

Ask the users if they want to play the game.

Get plays for both players.

If the plays are valid, output the plays and the winner.

Update the total game count and winner count.

Repeat Steps 2 through 5 while the users agree to play the game.

Output the number of plays and times that each player won.

We will use the following enumeration type to describe the objects.

image text in transcribed

It is clear that you need the following variables in the function main:

image text in transcribed

This program is divided into the following functions, which the ensuing sections describe in detail.

displayRules: This function displays some brief information about the game and its rules.

validSelection: This function checks whether a players selection is valid. The only valid selections are R, r, P, p, S, and s. Checks the validity of user input and if valid returns true, returns false otherwise.

retrievePlay: Because enumeration types cannot be read directly, this function converts the entered choice (R, r, P, p, S, or s) and returns the appropriate object type. Assigns an enumeration type to object depending on entered choice (R, r, P, p, S, or s).

gameResult: This function outputs the players choices and the winner of the game. The following method is used to print players choices.

convertEnum: This function is called by the function gameResult to output the enumeration type values.

winningObject: This function determines and returns the winning object. i.e. Rock VS Scissors the winning object would be Rock and it will be returned.

displayResults: After the game is over, this function displays the final results. How many rounds of games played, player1s score and player2s score will be printed.

Main Method:

Declare the variables.

Initialize the variables.

Display the rules.

Prompt the users to play the game.

Get the users responses to play the game.

while (response is yes)

{

Prompt player 1 to make a selection.

Get the play for player 1.

Prompt player 2 to make a selection.

Get the play for player 2.

If both plays are legal:

{

Increment the total game count.

Declare the winner of the game.

Increment the winners game win count by 1.

}

Prompt the users to determine whether they want to play again.

Get the players responses.

}

Output the game results.

enum objectType { ROCK, PAPER, SCISSORS }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago