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

Videos

Question
Book Icon
Chapter 3, Problem 1P
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
Python - Need help! How do I have an input in turtle to display my name below the circle it draws and another input to display my age written below that? Code: import turtlebackground = "#FFFFFF" def draw_circle(radius, line_color, fill_color): my_turtle.color(line_color) my_turtle.fillcolor(fill_color) my_turtle.begin_fill() my_turtle.circle(radius) my_turtle.end_fill() def move_turtle(x, y): my_turtle.penup() my_turtle.goto(x, y) my_turtle.pendown() turtle.done()
Need help fixing my python code! Images attached on the required modficications I dont know how to do. Simpler the better.Code: (in images)
Answer all of the questions with steps by step explanation to every question.

Chapter 3 Solutions

Problem Solving with C++ (9th Edition)

Chapter 3.2, Problem 11STE Chapter 3.2, Problem 12STE Chapter 3.2, Problem 13STE Chapter 3.2, Problem 14STE Chapter 3.2, Problem 15STE Chapter 3.2, Problem 16STE Chapter 3.2, Problem 17STE Chapter 3.2, Problem 18STE Chapter 3.2, Problem 19STE Chapter 3.2, Problem 20STE Chapter 3.3, Problem 21STE Chapter 3.3, Problem 22STE Chapter 3.3, Problem 23STE Chapter 3.3, Problem 24STE Chapter 3.3, Problem 25STE Chapter 3.3, Problem 26STE Chapter 3.3, Problem 27STE Chapter 3.3, Problem 28STE Chapter 3.3, Problem 29STE Chapter 3.3, Problem 30STE Chapter 3.3, Problem 31STE Chapter 3.3, Problem 32STE Chapter 3.3, Problem 33STE Chapter 3.3, Problem 34STE Chapter 3.3, Problem 35STE Chapter 3.4, Problem 36STE Chapter 3.4, Problem 37STE Chapter 3.4, Problem 38STE Chapter 3.4, Problem 39STE Chapter 3.4, Problem 40STE Chapter 3.4, Problem 41STE Chapter 3, Problem 1P Chapter 3, Problem 2P Chapter 3, Problem 3P Chapter 3, Problem 4P Chapter 3, Problem 5P Chapter 3, Problem 6P Chapter 3, Problem 7P Chapter 3, Problem 1PP Chapter 3, Problem 2PP Chapter 3, Problem 3PP Chapter 3, Problem 4PP Chapter 3, Problem 5PP Chapter 3, Problem 6PP Chapter 3, Problem 7PP Chapter 3, Problem 8PP Chapter 3, Problem 9PP Chapter 3, Problem 10PP Chapter 3, Problem 11PP Chapter 3, Problem 12PP Chapter 3, Problem 13PP

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
The momentreactions at the supports A and B.

Mechanics of Materials (10th Edition)

Enum method in java returns the position of an enum constant that is known as the ordinal value. Hence, the cor...

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

Loop structure is a control flow statement; the basic purpose is to execute the block of statements repeatedly ...

Computer Science: An Overview (13th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
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
    Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    Text book image
    C++ for Engineers and Scientists
    Computer Science
    ISBN:9781133187844
    Author:Bronson, Gary J.
    Publisher:Course Technology Ptr
    Text book image
    Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
    Text book image
    C++ Programming: From Problem Analysis to Program...
    Computer Science
    ISBN:9781337102087
    Author:D. S. Malik
    Publisher:Cengage Learning
    Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781305480537
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    Java random numbers; Author: Bro code;https://www.youtube.com/watch?v=VMZLPl16P5c; License: Standard YouTube License, CC-BY