Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 3, Problem 1P

Write a program to score the paper-rock-scissor game. Each of two users types in either P, R, or S. The program then announces the winner as well as the basis for determining the winner: Paper covers rock. Rock breaks scissors. Scissors cut paper, or Nobody wins. Be sure to allow the users to use lowercase as well as uppercase letters. Your program should include a loop that lets the user play again until the user says she or he is done.

Expert Solution & Answer
Check Mark
Program Plan Intro

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.
  • 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 Description Answer

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;

}

Sample Output

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Question#2. The distance a vehicle travels can be calculated as fallout: Distance = Speed * Time For example, if a train travels 60 miles-per-hour for three hours, the distance [raveled is 120 miles. Write a program that asks for the speed of a vehicle (in miles-per-hour) and the number of hours it has traveled. It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. For example, if a vehicle is traveling at 40 mph far a three-hour time period, it should display a report similar to the one chat follows: Hour Distance Traveled 1 90 2 100 3 150 Input Validation: Do not accept a negative number for speed and do not accept any value les than 1 for time traveled.
1. first ask the user to enter the number of students in the range of 1 to 10. 2. then the program asks the user to enter the number of tests (in the range of 1 to  5) 3. Then use a loop to collect the scores for those many tests for each student. The outer loop will iterate once for each student. The inner loop will iterate several times, once for each test. Each iteration of the inner loop will ask the user to enter the score for a test of a student.
Part 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 only

Chapter 3 Solutions

Problem Solving with C++ (9th Edition)

Ch. 3.2 - What output will be produced by the following...Ch. 3.2 - Write a multiway if-else statement that classifies...Ch. 3.2 - Given the following declaration and output...Ch. 3.2 - Given the following declaration and output...Ch. 3.2 - What output will be produced by the following...Ch. 3.2 - What would be the output in Self-Test Exercise 15...Ch. 3.2 - What would be the output in Self-Test Exercise 15...Ch. 3.2 - What would be the output in Self-Test Exercise 15...Ch. 3.2 - Prob. 19STECh. 3.2 - Though we urge you not to program using this...Ch. 3.3 - Prob. 21STECh. 3.3 - Prob. 22STECh. 3.3 - What is the output of the following (when embedded...Ch. 3.3 - What is the output of the following (when embedded...Ch. 3.3 - Prob. 25STECh. 3.3 - What is the output of the following (when embedded...Ch. 3.3 - Prob. 27STECh. 3.3 - For each of the following situations, tell which...Ch. 3.3 - Rewrite the following loops as for loops. a.int i...Ch. 3.3 - What is the output of this loop? Identify the...Ch. 3.3 - What is the output of this loop? Comment on the...Ch. 3.3 - What is the output of this loop? Comment on the...Ch. 3.3 - What is the output of the following (when embedded...Ch. 3.3 - What is the output of the following (when embedded...Ch. 3.3 - What does a break statement do? Where is it legal...Ch. 3.4 - Write a loop that will write the word Hello to the...Ch. 3.4 - Write a loop that will read in a list of even...Ch. 3.4 - Prob. 38STECh. 3.4 - Prob. 39STECh. 3.4 - What is an off-by-one loop error?Ch. 3.4 - You have a fence that is to be 100 meters long....Ch. 3 - Write a program to score the paper-rock-scissor...Ch. 3 - Write a program to compute the interest due, total...Ch. 3 - Write an astrology program. The user types in a...Ch. 3 - Horoscope Signs of the same Element are most...Ch. 3 - Write a program that finds and prints all of the...Ch. 3 - Buoyancy is the ability of an object to float....Ch. 3 - Write a program that finds the temperature that is...Ch. 3 - Write a program that computes the cost of a...Ch. 3 - (This Project requires that you know some basic...Ch. 3 - Write a program that accepts a year written as a...Ch. 3 - Write a program that scores a blackjack hand. In...Ch. 3 - Interest on a loan is paid on a declining balance,...Ch. 3 - The Fibonacci numbers F are defined as follows. F...Ch. 3 - The value ex can be approximated by the sum 1 + x...Ch. 3 - Prob. 8PPCh. 3 - Prob. 9PPCh. 3 - Repeat Programming Project 13 from Chapter 2 but...Ch. 3 - The keypad on your oven is used to enter the...Ch. 3 - The game of 23 is a two-player game that begins...Ch. 3 - Holy digits Batman! The Riddler is planning his...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
In Exercises 41 through 46, identify the errors.

Introduction To Programming Using Visual Basic (11th Edition)

Why is the CPU the most important component in a computer?

Starting Out with Programming Logic and Design (4th Edition)

The following program will not compile because the lines have been mixed up. System.out.print(Success\n); } pub...

Starting Out with Java: From Control Structures through Data Structures (3rd Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Java random numbers; Author: Bro code;https://www.youtube.com/watch?v=VMZLPl16P5c;License: Standard YouTube License, CC-BY