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).
Want to see the full answer?
Check out a sample textbook solutionChapter 15 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
C++ How to Program (10th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Starting Out with C++: Early Objects (9th Edition)
Starting Out with C++: Early Objects
Artificial Intelligence: A Modern Approach
- In c++arrow_forwardWhat does the function f do? struct Point2D { double x; double y; struct Triangle { Point2D v1; Point2D v2; Point2D v3; }; void f(Triangle&t) { } int temp = 12.5; temp = t.v1.x; t.v1.x = t.v1.y; t.v1.y = temp; } int main () { Triangle mytri; mytri.v1.x = 1.0; mytri.v1.y = 22.5; f (mytri); Swaps values of x and y in vertex 1 of an argument of type Triangle Initializes value of x in vertex 1 of an argument of type Triangle Sets all x,y values in all vertices of an argument of type Triangle Swaps value of x in vertex 1 with value of x in vertex 2, for an argument of typearrow_forwardHello I need some help with this. I keep getting this one wrong. The language program is Python.arrow_forward
- 3: The code on the right is supposed to be the ARM code for procA; however, there are problems with the ARM code. C code: int procA(int x, int y) { int perimeter = (x + y) * 2; I return perimeter; } ARM code: I procA: add rø, r1, r2 mov r1, rø lsl #1 bx lr push {lr} Give the corrected version of the ARM code for procA:arrow_forwardjavascript only: This quarter, your team manager is awarding commission to anyone who closes a sale worth $15,000 or more! The commission amount will be 10% of the value of their largest sale. Anyone who does not make this large of a sale will receive the base commission of $500. Write a function `salesCommission(arr)` that takes an array of objects containing salesperson names and their largest sale, and returns the total amount of commission money that needs to be paid out to the team. Example: salespeople = [ { name: "Susan", largestSale: 3000 }, { name: "Brianna", largestSale: 20000 }, { name: "Dwayne", largestSale: 13000 }, { name: "Delilah", largestSale: 26000 }, { name: "Fernando", largestSale: 8000 }, ]; console.log(salesCommission(salespeople)); // 6100arrow_forwardnbm.m//,//arrow_forward
- Code in python: A certain company has encoded the accounts of its customers and requires that you provide an algorithm that, given an account code, informs if it is valid according to the following description: The account codes are made up of 4 digits counted from right to left, plus the verification digit. The verifying digit is obtained by adding the digits of the account number of the even positions and multiplying the digits of the odd positions, from the new result the residue of the division is extracted for 10, which represents the verifying digit. Develop the code in python as a check digit class that returns 1 if it is correct or 0 if not Heading: Extract digits, verifier calculation, Account verification, Integral algorithmarrow_forwardWrite a Python function def getBonus(salary, bonusPercent) The function you write should be named getBonus, and it should accept an employee’s salary and the bonus they have been granted, and then return the bonus amount. The salary, bonusPercent, and bonusAmount should all be float type. Here are some examples of typical usage: salary = readFloat(“Please enter your salary:”) xmasBonus = getBonus(50000, 0.04)arrow_forwardC++ 1. Write a function named "acceptKey" that accepts an array of valid keysand its size. The method will read in a key from the userand check whether the user has entered one of the valid keys in the given array or not. If not, it will allow the user to try up to 3 times. If the user was able to enter the expectedkey number within 3 tries, it will return true. Otherwise, it returns false and key value of 0.In addition, it also returned the key to the caller. For example, assuming the given valid key array contains {111, 222, 333, 444} here are 2 different sets of runs: Please enter your key: 123Please enter your key: 456Please enter your key: 789Return false and the returned key of 0 Please enter your key: 123Please enter your key: 456Please enter your key: 444Return true and the returned key of 444 Requirement: please use only pointer notation. No array notation.The use of pass-by-reference parameters is also not allowed. Use pointer parameters instead.arrow_forward
- *68. What is the return value of f (p, p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value. int f(int &x, int c) { c = c 1; if X = X + 1; return f(x, c) } (a) 3024 (c==0) return 1; * X; (b) 6561 (c) 55440 (d) 161051arrow_forwardMindTap: In C#, Write a program named Averages that includes a method named Average that accepts any number of numeric parameters, displays them, and displays their average. Test your function in your Main(). Tests will be run against Average() to determine that it works correctly when passed one, two, or three numbers, or an array of numbers.arrow_forward3. A chess board is an 8x8 matrix of squares. Each square can be empty or occupied by a chess piece. Write a class for a chess board. In the class, write a function that puts a chess piece into the position given as a parameter. The position can be given as numbers 0..7, 0.7. Note: I need an answer on C++ code.. please do it fastarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education