Question: C++ Problem B) (20 Points) Creating a Guessing Game Program. Part of the code to play a guessing game is listed below. In the game,
C++
Problem B) (20 Points) Creating a Guessing Game Program. Part of the code to play a guessing game is listed below. In the game, two players are playing the game rock, paper, scissors. The game works as follows Two players simultaneously state one of three possible words: Rock, Paper, or Scissors. If they both say the same word, neither one wins the turn. Otherwise, a winner is determined as follows: - Rock beats Paper - Paper beats Rock, and - Rock beats Scissors. (See https://en.wikipedia.org/wiki/Rock%E2%80%93paper%E2%80%93scissors for a complete specification). Your task is to use the C++ code provided and construct a program to play the game Rock-Paper-Scissors using objects that represent either a human player or a computer player. Recall, the rand() function requires you to include the cstdlib of your textbook for information on random number generation)
The C++ code is as follows: void takeTurn (Player & player1, Player & player2) { string p1 = player1.name; string p2 = player2.name; string p1turn = player1.getGuess(); string p2turn = player2.getGuess(); if (p1turn == p2turn) cout << "Both Guessed: " << p1turn << " No winner! "; else if (p1turn == "rock" && p2turn == "scissors" ) { cout << p1 << " wins! "; cout << "rock beats scissors! "; } else if (p1turn == "scissors" && p2turn == "paper"){ cout << p1 << " wins! "; cout << "scissors beats paper! "; } else if (p1turn == "paper" && p2turn == "rock"){ cout << p1 << " wins! "; cout << "paper beats rock! "; } else if (p1turn == "scissors" && p2turn == "rock"){ cout << p2 << " wins! "; cout << "rock beats scissors! "; } else if (p1turn == "paper" && p2turn == "scissors"){ cout << p2 << " wins! "; cout << "scissors beats paper! "; } else { cout << p2 << " wins! "; cout << "paper beats rock! "; } return; } // end takeTurn
Constraints: The takeTurn function takes two Player objects. You are required to follow steps 1 through 7 (inclusive) below when you implement your solution. 9 1. Define the Player class with a virtual function named getGuess(), and an attribute named name that stores a string. The implementation of Player::getGuess can simply return the string paper. 2. Next, define a class named HumanPlayer derived from Player. The implementation of HumanPlayer::getGuess should prompt the user to enter a number from the keyboard 0 for rock, 1 for paper, and 2 for scissors. The function should return the string corresponding to the number entered by the user (rock if 0 is entered, paper if 1 is entered, and scissors if 2 is entered). 3. The HumanPlayer class should also declare and define a constructor that accepts a string as a parameter, and sets the attribute name to the parameter value (a string); 4. Next, define a class named ComputerPlayer derived from Player. ComputerPlayer::getGuess() should randomly generate a number between 0 and 2 (inclusive) and use that number determine which string value is returned (rock if 0 is randomly generated, paper if 1 is randomly generated, and scissors if 2 is randomly generated). 5. The ComputerPlayer class should also declare and define a constructor that accepts a string as a parameter, and sets the attribute name to the parameter value (a string); 6. Finally, construct a main program that does the following: a. Creates two instances of the HumanPlayer (with different names). b. Creates two instances of the ComputerPlayer(with different names). c. Seeds the random number generator with the value 125. d. calls the function takeTurn (Player &player1, Player &player2) with two different instances of HumanPlayer(human vs human), e. calls the function takeTurn (Player &player1, Player &player2) an instance of a HumanPlayer and ComputerPlayer (human vs computer), f. Finally, calls the function takeTurn (Player &player1, Player &player2) with two different instances of ComputerPlayer (computer vs computer). 7. Be sure to include a continuation loop in your program.
Below is an example of what your output should look like when you program is run(user input is in bold): Enter 0 for rock, 1 for paper, or 2 for scissors: 3 Error: Your entry was invalid, try again! Enter 0 for rock, 1 for paper, or 2 for scissors: 2 Enter 0 for rock, 1 for paper, or 2 for scissors: 1 human1 wins! scissors beats paper! Enter 0 for rock, 1 for paper, or 2 for scissors: 0 Both Guessed: rock No winner! Both Guessed: rock No winner! Play again (Enter 'y' or 'Y'): y (The example run is continued on the next page, this message is not part of it) 10 Enter 0 for rock, 1 for paper, or 2 for scissors: 1 Enter 0 for rock, 1 for paper, or 2 for scissors: 2 human2 wins! scissors beats paper! Enter 0 for rock, 1 for paper, or 2 for scissors: 2 human1 wins! scissors beats paper! computer2 wins! paper beats rock! Play again (Enter 'y' or 'Y'): n Test and revise your program until you are sure it is correct.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
