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++ (10th 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)
- Create a program where a user guesses whether the value of the sum of two dice will be even or odd. The game will stop when the number of turns has reached 5. To guess odd, the user enters 1. To guess even, the user enters 2. After each roll, the program should display the two numbers rolled,the sum of the numbers, the user's guess as "Even" or "Odd" and whether they won or lost. Once the player ends the game, the program should display the number of times played and the number of turns won and lost and the number of times the dice were even/odd. Technical Requirements: Must use the random function for each die Use a loop to play the game for 5 turns Use an array to track each roll as to whether it was even or oddarrow_forwardPart A: While Loop Program - using Java. Write a program that detects Fibonacci numbers. Prompt the user to input a positive integer. Upon input, the program will determine if the number is either a Fibonacci number or not. If a Fibonacci number, then the order of the number in the sequence must be output. If not a Fibonacci number, then the Fibonacci numbers above and below it (including their order in the sequence) must be output. Once it finishes, the program will prompt the user for a new number. The program will exit if the user enters a non-integer number or string (such as “quit”) instead of an integer. Use the sample output file, fib-seq-det.txt, to view a sample session Additionally: For both the above problems, the first four numbers of the Fibonacci sequence are: 0, 1, 1, and 2. Part A must use While loops onlyarrow_forwardIn the Raptor program Write a program 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 is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the computer’s choice yet.)2. The user enters his or her choice of “rock,” “paper,” or “scissors” at the keyboard.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 cuts paper.)• If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.)• If both players make the same choice, the…arrow_forward
- In the Raptor program Write a program 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 is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the computer’s choice yet.)2. The user enters his or her choice of “rock,” “paper,” or “scissors” at the keyboard.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 cuts paper.)• If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.)• If both players make the same choice, the…arrow_forwardIn the Raptor program Write a program 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 is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the computer’s choice yet.)2. The user enters his or her choice of “rock,” “paper,” or “scissors” at the keyboard.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 cuts paper.)• If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.)• If both players make the same choice, the…arrow_forwardWrite a program that displays a weekly payroll report. A loop in the program should ask the user for the employee number, gross pay, state tax, federal tax, and FICA withholdings. The loop will terminate when 0 is entered for the employee number. After the date is entered, the program should display totals for gross pay, state tax, federal tax, FICA withholdings, and net pay.arrow_forward
- Use C++ programming language.. Random Number Guessing Game Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number. Then the program should display “Congratulations. You figured out my number.”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_forwardPYTHON PROGRAMMING ONLY. Please provide explanation as well. Thank you. Write a while loop that lets a user enter a number less than 10. If the number inputted exceeds 10, print an error and ask for the number again, in which a user may enter the same or a different number. When the user inputs a valid number, the number should be incremented by 5, and the result assigned to a variable named sum. This loop should continue to iterate as long as the sum totals less than 50. When sum exceeds 30, it should print "Almost there..." until it reaches or exceeds 50. Once it does, print out "your final sum is (replace with final sum)". For example, if the sum is currently 45 and the user inputs 4, the program will not enter the loop again since the new sum will be 54 and should then print "your final sum is 45".arrow_forward
- n is an integer variable.Write a while loop that prints integer numbers starting with n and dividing the number to 4 each time, while the new number is greater than 0.(Example: If n is 2077, it will print 2077 519 129 ...)Write only the statements to perform the above explained task, nothing else.Use while loop, don't use for loop.arrow_forward1. Write a program that lets the user guess whether a randomly generated integer would be even or odd. The program randomly generates an integer and divides it by 2. The integer is even if the remainder is 0, otherwise odd. The program prompts the user to enter a guess and reports whether the guess is correct or incorrect.arrow_forwardPlease don't copy from other website Complete the program below, so it works. User enters amount to invest, interest percentage and number of years. Enter amount to invest: 200 Enter interest percentage: 0.10 Enter number of years: 2 Year: 1 Interest: 20 Year: 2 Interest: 20 Total Amount thus far: 220 Total Amount thus far: 240 The while loop runs according to the user input. Interest is calculated by multipling the user amount by the interest percentage. double amount, interestPerc, interest; int years; int i; Console.Write("Enter amount to invest: "); amount = double.Parse(Console.ReadLine(); Console.Write("Enter interest percentage: "); %3D interestPerc = double.Parse(Console.ReadLine()); Console.Write("Enter number of years: "); %3D years = int.Parse(Console.ReadLine()); Console.WriteLine(); %3D interest = [A] %3D [B]arrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage