Write a
Program plan:
- Include necessary header files.
- Declare the namespace.
- Define the class “Player”.
- Declare the necessary functions within the “public” access specifier.
- Declare the necessary variables with the “private” access specifier.
- The initializer sets “Player::totWins” to “0”.
- Define the function “play()”.
- Print the statement.
- Get the input “choice” from the user.
- Call the function “toupper()” and assign the result into the variable “choice”.
- Define the function “Ch()”.
- Return the value of the variable “choice”.
- Define the function “AccWins()”.
- Return the value of the variable “totWins”
- Define the function “IncWins()”.
- Increment the value of the variable “totWins” by “1”.
- Declare the function “wins()”.
- Define the function “wins()”.
- The “if” loop check the expression.
- True, user 1 wins by calling the function “IncWins()”.
- Return “1”.
- The “else if” loop check the expression.
- True, user 2 wins by calling the function “IncWins()”.
- Return “2”.
- Otherwise, return zero.
- The “if” loop check the expression.
- Define the “main()” function.
- Create objects for the class “Player”.
- Initialize the variable.
- The “while” loop check the condition.
- True, objects call the function “play()”.
- Define the “switch” case.
- Define “case 0” for no winner.
- Define “case 1” for player 1 wins.
- Define “case 2” for player 2 wins.
- Call the function “toupper()” and assign the result in the variable “answer”.
- Return “0”.
Program to score the paper-rock-scissor game.
Explanation of Solution
Program:
//Include necessary header files
#include <iostream>
#include <cctype>
//Declare the namespace
using namespace std;
//Define the class Player
class Player
{
//Access specifier
public:
//Constructor, declare the function Player()
Player();
//Declare the function play()
void play();
//Declare the function Ch()
char Ch();
//Declare the function AccumulatedWins
int AccWins();
//Deeclare the function IncWins()
void IncWins();
//Access specifier
private:
//Variable declaration
char choice;
int totWins;
};
//Initializer sets Player::totWins to 0
Player::Player():totWins(0)
{
}
//Define the function play()
void Player::play()
{
//Print the statement
cout << "Please enter either R)Rock, P)Paper, or S)Scissor." << endl;
//Get the input from the user
cin >> choice;
//Call the function toupper() and assign the result in choice
choice = toupper(choice);
}
//Define the function Ch()
char Player::Ch()
{
//Return the value of the variable choice
return choice;
}
//Define the function AccWins()
int Player::AccWins()
{
//Return the value of the variable totWins
return totWins;
}
//Define the function IncWins()
void Player::IncWins()
{
//Increment the value of the variable totWins by 1
totWins++;
}
//Declare the function wins()
int wins(Player& user1, Player& user2);
//Define the function wins()
int wins(Player& user1, Player& user2 )
{
//Check, the expression
if( ( 'R' == user1.Ch() && 'S' == user2.Ch() )||
( 'P' == user1.Ch() && 'R' == user2.Ch() )||
( 'S' == user1.Ch() && 'P' == user2.Ch() ) )
{
//True, user 1 wins by calling the function IncWins()
user1.IncWins();
//Return 1
return 1;
}
//Check, the expression
else if( ( 'R' == user2.Ch() && 'S' == user1.Ch() )
|| ( 'P' == user2.Ch() && 'R' == user1.Ch() )
|| ( 'S' == user2.Ch() && 'P' == user1.Ch() ) )
{
//True, user 2 wins by calling the function IncWins()
user2.IncWins();
//Return 1
return 2;
}
//Otherwise
else
//Return zero, no winner
return 0;
}
//Define the main() function
int main()
{
//Create objects for the class Player
Player player1;
Player player2;
//Initialize the variable answer as Y
char answer = 'Y';
//Check, Y is equal to answer
while ('Y' == answer)
{
//True, the objects call the function play()
player1.play();
player2.play();
//Swich case
switch( wins(player1, player2) )
{
//Case 0 for no winner
case 0:
//Print the result
cout << "No winner. " << endl
<< "Totals to this move: " << endl
<< "Player 1: " << player1.AccWins()
<< endl
<< "Player 2: " << player2.AccWins()
<< endl
<< "Play again? Y/y continues other quits";
//Get the input from the user
cin >> answer;
//Print the statement
cout << "Thanks " << endl;
//Break the statement
break;
//Case 1 for player 1 wins
case 1:
//Pint the result
cout << "Player 1 wins." << endl
<< "Totals to this move: " << endl
<< "Player 1: " << player1.AccWins()
<< endl
<< "Player 2: " << player2.AccWins()
<< endl
<< "Play Again? Y/y continues, other quits. ";
//Get the input from the user
cin >> answer;
//Print the statement
cout << "Thanks " << endl;
//Break the statement
break;
//Case 2 for player 2 wins
case 2:
//Pint the result
cout << "Player 2 wins." << endl
<< "Totals to this move: " << endl
<< "Player 1: " << player1.AccWins()
<< endl
<< "Player 2: " << player2.AccWins()
<< endl
<< "Play Again? Y/y continues, other quits.";
//Get the input from the user
cin >> answer;
//Print the statement
cout << "Thanks " << endl;
//Break the statement
break;
}
/*Call the function toupper() and assign the result in the variable answer*/
answer = toupper(answer);
}
//Return zero
return 0;
}
Output:
Please enter either R)Rock, P)Paper, or S)Scissor.
R
Please enter either R)Rock, P)Paper, or S)Scissor.
S
Player 1 wins.
Total to this move:
Player 1: 1
Player 2: 0
Play Again? Y/y continues, other quits. Y
Thanks
Please enter either R)Rock, P)Paper, or S)Scissor.
P
Please enter either R)Rock, P)Paper, or S)Scissor.
S
Player 2 wins.
Total to this move:
Player 1: 1
Player 2: 1
Play Again? Y/y continues, other quits. Y
Thanks
Please enter either R)Rock, P)Paper, or S)Scissor.
S
Please enter either R)Rock, P)Paper, or S)Scissor.
R
Player 2 wins.
Total to this move:
Player 1: 1
Player 2: 2
Play Again? Y/y continues, other quits. N
Thanks
Want to see more full solutions like this?
Chapter 3 Solutions
Problem Solving with C++ (9th Edition)
Additional Engineering Textbook Solutions
Introduction To Programming Using Visual Basic (11th Edition)
Digital Fundamentals (11th Edition)
Starting Out with Programming Logic and Design (4th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
- Programming is Javaarrow_forwardText Rectangle. Create a program that draws a rectangle in text mode given the dimensions provided by the user. Attached is a sample of the execution of the program. Hint: Use a loop and the repetition operator to create the rectangle.arrow_forwardUsing C# in Microsoft Visual Studio create an application that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows:1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Do not display the computer’s choice yet.)2. The user selects his or her choice of rock, paper, or scissors. To get this input you can use Button controls, or clickable PictureBox controls displaying some of the artwork that you will find in the student sample files.3. The computer’s choice is displayed.4. A winner is selected according to the following rules:• If one player chooses rock and the other player chooses scissors, then rock wins. (Rock smashes scissors.)• If one player chooses scissors and the other player chooses paper, then scissors wins. (Scissors…arrow_forward
- Running on a particular treadmill you burn 3.9 calories per minute. Write a program that uses a loop to display the number of calories burned after 10, 15, 20, 25, and 30 minutes.arrow_forwardMUST BE IN PYTHON PROGRAMMING CODE!!!!! NO C++ or Java!! Directions: Design a program with a loop that lets the user enter a series of names (in no particular order). After the final person’s name has been entered, the program should display the name that is first alphabetically and the name that is last alphabetically. For example, if the user enters the names Kristin, Joel, Adam, Beth, Zeb, and Chris, the program would display Adam and Zeb. Additional requirements: Note that the possible set of input values can include negative, 0, or positive values, but not the sentinel value of -99. DESIGN HINT: • When determining the largest (or smallest) value in a set of numbers, remember to use variables to keep track of the current largest (or smallest) number found thus far. These variables can then be used as a comparison to each subsequent input number to see if the new number is larger (or smaller) than the current largest (or smallest) number and updated appropriately. At the end of all…arrow_forwardC++ Program plsarrow_forward
- Write a program that generates a random number in the range from 1 through 100 After generating the random number, the user is to guess the number. If the user’s guess is higher than the random number, the program should display “Too high, try again” If user’s guess is lower than the random number, the program should display “Too low, try again”. If the user guesses the number, the program should congratulate the user and then generate a NEW random number so the game can start over. Make the game menu driven with these options Main Menu ______________ 1)Play Game 2)Exit written in pythonarrow_forwardplease write a code for thatarrow_forwardQ1: The local Driver's License Office has asked you to write a program that grades the writ- ten portion of the driver's license exam. The exam has 20 multiple choice questions. Here are the correct answers: 1. A 2. D 3. B 4. B 5. C 6. B 7. A 8. B 9. C 10. D 11. A 12. C 13. D 14. B 15. D 16. C 17. C 18. A 19. D 20. B Your program should store the correct answers shown above in an array. It should ask the user to enter the student's answers for each of the 20 questions, and the answers should be stored in another array. After the student's answers have been entered, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions. Input Validation: Only accept the letters A, B, C, or…arrow_forward
- Not sure how to solve this in Pesudocode.arrow_forwardUsing a Sentinel Value to Control a while Loop Summary In this lab, you write a while loop that uses a sentinel value to control a loop in a Java program that has been provided. You also write the statements that make up the body of the loop. The source code file already contains the necessary variable declarations and output statements. Each theater patron enters a value from 0 to 4 indicating the number of stars the patron awards to the Guide’s featured movie of the week. The program executes continuously until the theater manager enters a negative number to quit. At the end of the program, you should display the average star rating for the movie. Instructions Ensure the file named MovieGuide.java is open. Write the while loop using a sentinel value to control the loop, and write the statements that make up the body of the loop to calculate the average star rating. Execute the program by clicking Run. Input the following: 0, 3, 4, 4, 1, 1, 2, -1 Check that the average…arrow_forwardProblem: Feed Nibble Monster As Long As It Is Happy Write a program in which the Nibble Monster happily keeps asking for nibbles until the user gives it something that is not a nibble. At that point the monster becomes unhappy and the loop ends. Use do-while loop. HINT: You can control the loop with a boolean variable which indicates the state of Monster's feelings: it is true initially but becomes false when the Monster receives a non-nibble. Sample Runs: weron@DESKTOP-218KDUL MINGW64 /c/courses/CS2011/code $ java NibbleMonsterWithLoopwhileHappy Monster hungry :E Feed monster nibble :0 c yum! Feed monster nibble :0 F YUM! Feed monster nibble :0 8 m04r f00d! Feed monster nibble :0 $ Ewww! :0=$ You tried to poison me. Go away! weron@DESKTOP-218KDUL MINGW64 /c/courses/CS2011/code $ java NibbleMonsterWithLoopwhileHappy Monster hungry :E Feed monster nibble :0 # 4 Ewww! :o=# You tried to poison me. Go away!arrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage