21. The HiLow game is a one-person game that challenges a player to determine th
ID: 3869819 • Letter: 2
Question
21. The HiLow game is a one-person game that challenges a player to determine the value of a random number (target) between 1 and 1000 within the span of n s 10 guesses. The player inputs a number, and the program informs the player whether the target mat the number "PLAYER WINS IN GUESSE S"), the target is less than the number "LOWER"), or the target is greater than the number ("HIGHER"). After 10 unsuc- cessful guesses, the game terminates ("PLAYER LOSES-TARGET IS ") (a) Declare and implement with inline code the class play HiLow, which has the integer, target, and the randomNumber object, rnd, as data members. The constructor uses rnd to initialize the target to a value in the range from 1 to 1000. The member func- tion makeGuess takes an integer argument and returns an integer value. The argu- ment represents a guess that the function compares with target. The return value is negative (-1) if the target is less than the guess, zero if the target and the guess area match, and positive (+1) if the target is greater than the guess. The function write- Target) outputs the message Target = (b) Write a code segment that declares a playHiLow object called player and uses a loop to control the playing of the game. For each iteration, input a guess by calling makeGuess0, and use the return value to indicate whether the player wins or the tar-Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
//class randomNumber definition
class randomNumber
{
public:
//Function to generate and return random number
int generateRandomNumber()
{
//A seed value of 1 is the default setting yielding the same sequence of values as if srand(x) were not used.
//Any other value for the seed produces a different sequence.
srand((unsigned)time(0));
//Generates and returns a random number between 1 and 1000
return (1 + (rand() % 1000));
}//End of function
};//End of class
//class playHighLow definition
class playHighLow
{
//To store target number
int target;
//randomNumber class object declared as a data member (This mechanism is called deligation)
randomNumber rnd;
public:
//Default Constructor
playHighLow()
{
//Calls the method generateRandomNumber() to generate random number and stored it in target variable
target = rnd.generateRandomNumber();
}//End of constructor
//Function takes the guess number entered by the user and return the value for high or low or same
int makeGuess(int guessNo)
{
//Checks if target is less than the guess number return -1
if(target < guessNo)
return -1;
//Checks if target is equal to the guess number return 0
else if(target == guessNo)
return 0;
//Otherwise target is greater than the guess number return 1
else
return 1;
}//End of function
//Displays the target number
void writeTarget()
{
cout<<"Target = "<<target;
}//End of function
};//End of class
//Main method definition
int main()
{
//Creates an object of playHighLow class
playHighLow player;
//To store guess number, to store the return result of makeGuess function, counter to loop 10 times, flag to know the status
int guessNo, result, counter, flag = 0;
//Loops 10 times
for (counter = 1; counter <= 10; counter++)
{
//Accept a number from the user
cout<<" Guess a number: ";
cin>>guessNo;
//Calls the method and stores the return result
result = player.makeGuess(guessNo);
//If the result is zero set the flat to 1 for winner
if(result == 0)
{
//Set the flag value to one
flag = 1;
//Come out of the loop
break;
}//End of if
//If the result is negative display the higher message
else if(result < 0)
cout<<" HIGHER";
//Otherwise the result is positive display the lower message
else
cout<<" LOWER";
}//End of for loop
//If flag value is one then winner
if(flag == 1)
{
//Display the message
cout<<" Player Wins in "<<counter<<" guesses.";
//Display the target
player.writeTarget();
}//End of if
//Otherwise looser
else
{
//Display the message
cout<<" Player Loses - ";
//Display the target
player.writeTarget();
}//End of else
}//End of main function
Sample Run 1:
Guess a number: 500
LOWER
Guess a number: 600
LOWER
Guess a number: 700
LOWER
Guess a number: 800
LOWER
Guess a number: 900
HIGHER
Guess a number: 850
LOWER
Guess a number: 860
LOWER
Guess a number: 870
LOWER
Guess a number: 880
LOWER
Guess a number: 890
HIGHER
Player Loses - Target = 881
Sample Run 2:
Guess a number: 100
LOWER
Guess a number: 200
LOWER
Guess a number: 300
HIGHER
Guess a number: 350
HIGHER
Guess a number: 250
LOWER
Guess a number: 270
LOWER
Guess a number: 272
Player Wins in 7 guesses.Target = 272
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.