Question: Option 1: Guessing Game Write a program to play a guessing game. In the game, two players attempt to guess a number. Your task is
Option 1: Guessing Game
Write a program to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player.
Step 1: Define three classes called Player, HumanPlayer and ComputerPlayer.
Define the class Player with a virtual function named getGuess(). The implementation of Player::getGuess can simply return 0.
Define a class named HumanPlayer derived from Player.
HumanPlayer::getGuess() should prompt the user to enter a number and return the value entered from the keyboard.
Define a class named ComputerPlayer derived from Player.
ComputerPlayer::getGuess() should randomly select a number between 0 and 99. and return the value entered from the keyboard.
bool checkForWin(int guess, int answer)
{
cout << "You guessed " << guess << ". ";
if (answer == guess)
{
cout << "You're right! You win!" << endl;
return true;
}
else if (answer < guess)
cout << "Your guess is too high." << endl;
else
cout << "Your guess is too low." << endl;
return false;
}
void play(Player &player1, Player &player2)
{
int answer = 0, guess = 0;
answer = rand() % 100;
bool win = false;
while (!win)
{
cout << "Player 1's turn to guess." << endl;
guess = player1.getGuess();
win = checkForWin(guess, answer);
if (win) return;
cout << "Player 2's turn to guess." << endl;
guess = player2.getGuess();
win = checkForWin(guess, answer);
}
}
Write a main function that creates two instances of HumanPlayer and two instances of ComputerPlayer . It invokes two functions play( ) and CheckForWin( ).
Step 2: Modify the program so that the computer plays a more informed game.
The specific strategy is up to you, but you must add function(s) to the Player and ComputerPlayer classes so that the play(Player &player1, Player &player2) function can send the results of a guess back to the computer player. In other words, the computer must be told if its last guess was too high or too low, and it must also be told if its opponents last guess was too high or too low. The computer can then use this information to revise its next guess.
Write a main function that creates two instances of HumanPlayer and two instances of ComputerPlayer. (Also rewrite the two functions play() and CheckForWin( ).)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
