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 15, Problem 10PP

Solution to Programming Project 15.10

Listed below is code 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. The rand() function requires you include cstdlib (see Appendix 4):

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);

}

}

The play function takes as input two Player objects. Define the Player class with a virtual function named getGuess(). The implementation of Player::getGuess() can simply return 0. Next, define a class named HumanPlayer derived from Player. The implementation of HumanPlayer::getGuess() should prompt the user to enter a number and return the value entered from the keyboard. Next, define a class named ComputerPlayer derived from Player. The implementation of ComputerPlayer::getGuess() should randomly select a number between 0and 99 (see Appendix 4 for information on random number generation).Finally, construct a main function that invokes play(Player &player1, Player &player2) with two instances of a HumanPlayer (human versus human), an instance of a HumanPlayer and Computer Player (human versus computer),and two instances of ComputerPlayer (computer versus computer).

Blurred answer
Students have asked these similar questions
Write a JAVA program Write a function inside ProblemSolution class whose return type is void, accepts an array and the length of the array as input parameters. The function should call a static method display of MyArray class by passing an array and length value.    Input     5     1 5 8  2 0      Where,  First line of input represents the size of an array.  Second line represents array elements.    Output     1 5 8  2 0    Assume that,  N is an integer within the range [0 to 10000].  Array elements are integers within the range [-2147483648 to 2147483647].
In C++, Define a “Invalidanalyze” function that accepts an array of “Course” objects. It will return the following information to the caller: -  The number of courses with empty or blank description -  The number of courses with invalid negative units -  The number of courses with invalid day number of the week -  The total number of units for all invalid courses in the array Show how this method is being called and return proper information.
Rock-paper-scissors is a game for two players. Each player chooses an action without knowledge ofthe other’s choice. If the players choose the same action then there is a draw. Otherwise the winneris determined by the following rules: paper beats rock, rock beats scissors, and scissors beats paper. you are going to make a playable Rock-Paper-Scissors game and consider multiplayer tournaments. • Please make it a single Haskell file • Please put comments in your code to show which question you are answering with each piece ofcode. • You may create auxiliary functions if you like. You may use library functions from Haskell’sstandard library.• Please limit your line lengths to 100 characters max.Please use the following two data types which you can copy-and-paste into your code.data Action = Rock | Paper | Scissors deriving (Eq, Show)data Outcome = Player1Win | Player2Win | Draw deriving ShowAction represents a player’s chosen action and Outcome represents the outcome of playing a game

Chapter 15 Solutions

Problem Solving with C++ (9th 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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Call By Value & Call By Reference in C; Author: Neso Academy;https://www.youtube.com/watch?v=HEiPxjVR8CU;License: Standard YouTube License, CC-BY