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++ Plus MyLab Programming with Pearson eText -- Access Card Package (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)
- Write a program in Python, in which the user can enter any number of positive and negative integer values, and then we display the total number of positive values entered and the total number of negative values entered. We are going to use a sentinel value (also known as a flag value) to control the loop. If the user wants to quit counting up positive and negative numbers, the user will enter a (q). The first line should print out a title that says something like “Practice 4: Counting the number of positive and negative values entered by the user”.arrow_forwardThis is the question - Write a program that inserts parentheses, a space, and a dash into a string of 10 user-entered numbers to format it as a phone number. For example, 5153458912 becomes (515) 345-8912. If the user does not enter exactly 10 digits, display an error message. Continue to accept user input until the user enters 999. This is the code I have - However it isn't seeming to accept my output. It doesn't like what I have for some reason - import java.util.*; public class PhoneNumberFormat { public static void main(String[] args) { // Write your code here //Create an instance of Scanner class Scanner keyboard=new Scanner(System.in); //declare string data type variables String phoneNumber= ""; String formattedPhoneNumber=""; /*Run the loop infinitely until user enters 999 to exit from program*/ while(true) { System.out.println("Enter phone number: "); phoneNumber=keyboard.nextLine(); //exit…arrow_forwardWrite a program that receives student's marks (8 marks) and evaluates the average: use conditions or loops If 50<=average<60 print "poor". If 60<=average<70 print "medium". If 70<=average<80 print "good". If 80<=average<90 print "very good". If 90<=average<=100 print "excellent". Otherwise, print "failarrow_forward
- CORAL HELP Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. End each line with a newline. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies. Ex: If the input is: 0 or less than 0, the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2 dimesarrow_forwardWrite a program that requests the user to enter the total purchase in dollars and cents ( for example, $23.65, $45.00, etc) and outputs the discount in dollars and cents. If the total purchase is less than 200 dollars, there is no discount. If the total purchase is more than 200 dollars but less than or equal to 1000 dollars, then the discount is 15%, and if the total purchase is more than 1000 dollars, then the discount is 30%. At this store, a student always gets 30% discount on all purchases. Include this information in your codes.arrow_forwardplz solve it asap ????????: Write a program for a game that consists of two players. The rule of the game is that player 1 inputs a random number from 1-100. Player 2 will input 5 numbers. If the summation of those numbers is equal to the number OR less than or greater than the number by 3 (+3 or -3), then player 2 wins. Otherwise, player 1 wins. Test Case 1 Input Player 1, enter a number: 50 Player 2, enter a number: 10 Player 2, enter a number: 10 Player 2, enter a number: 20 Player 2, enter a number: 5 Player 2, enter a number: 7 Output Player 2 wins Explanation: The summation of 10,10,20,5 and 7 is 52 which is greater than 50 by 2. According to the rule, player 2 wins. Test Case 2 Input Player 1, enter a number: 50 Player 2, enter a number: 10 Player 2, enter a number: 10 Player 2, enter a number: 20 Player 2, enter a number: 5 Player 2, enter a number: 1 Output Player 1 wins Explanation: The summation of 10,10,20,5 and 1 is 46 which is less than 50 and also doesn’t…arrow_forward
- In Coral Programing!! Write a program that takes in an integer in the range 20-98 as input. The output is a countdown starting from the integer, and stopping when both output digits are identical. Ex: If the input is 93, the output is: 93 92 91 90 89 88 Ex: If the input is 77, the output is: 77 Ex: If the input is not between 20 and 98 (inclusive), the output is: Input must be 20-98 For coding simplicity, follow each output number by a space, even the last one. Use a while loop. Compare the digits; do not write a large if-else for all possible same-digit numbers (11, 22, 33, ..., 88), as that approach would be cumbersome for large ranges.arrow_forwardWrite a program that allows the user to enter a series of exam scores. The number of scores the user can enter is not fixed; they can enter any number of scores they want. The exam scores can be either integers or floats. Then, once the user has entered all the scores they want, your program will calculate and print the average of those scores. After printing the average, the program should terminate. You need to use a while loop to allow the user to enter numbers, one at a time, until some numeric sentinel value is entered. I recommend having a sentinel like 9999, something unlikely to be confused with an exam score. If the user enters a score < 0 or > 100 that is not the sentinel value then that score is to be rejected. Each time a legit score is entered, however, it should be added (appended) to a list. Once the user has entered all the numbers they want, calculate and display the average of the scores rounded to 1 decimal place. I attached my solution. I can not fix two…arrow_forwardC++ Question Write a program that reads any 5 numbers as in the given sample of output below: Sample of output: Enter any 5 integer value. 1 2 -1 3 0 The number of positive value is 3 The numbers are: 1 2 3 The number of negative value is 1 The numbers are: -1 The 0 number is 1 The total is 5.0 The average is 1.25arrow_forward
- Write a program that shows you a menu offering you the choice of addition, subtraction, multiplication, or division. After getting your choice, the program asks for two numbers, then performs the requested operation, prints out the expression evaluated and its result. The program should accept only the offered menu choices. It should use type float or double for the numbers and allow the user to try again if he or she fails to enter a number. In the case of division, the program should prompt the user to enter a new value if 0 is entered as the value for the second number. I'm struggling to write this program without using the break statements. I have an idea to write this program by using getchar() statements and the get_first function, but my program is not running. Please, can you help me with any ideas?arrow_forwardWrite a program in Python that sums a series of (positive) integers entered by the user, excluding all numbers that are greater than 100. We are going to use a sentinel value (also known as a flag value) to control the loop. If the user wants to quit summing up numbers, the user will enter a (-1). The first line should print out a title that says something like “Practice 3: summing positive values entered by the user that are less than 100”. For this practice you only can use while loop as you don’t know how many numbers the user wants to enter. A sample output is below.arrow_forwardWrite a program that randomly chooses between three different colors for displaying text on the screen. Use a loop to display twenty lines of text, each with a randomly chosen color. The probabilities for each color are to be as follows: white = 30%, blue = 10%, green = 60%. Hint: generate a random integer between 0 and 9. If the resulting integer is in the range 0-2, choose white. If the integer equals 3, choose blue. If the integer is in the range 4-9, choose green.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning