Question

Part 1 Write a program that plays the other side of the game "I'm thinking of...

Part 1

Write a program that plays the other side of the game "I'm thinking of a number"

Accuse the player of cheating if the player doesn't say y even though your program is sure that it has the correct number .

PT[154]% ./a.out
Think of a number between 1 and 100.
I will guess the number, then tell me if my guess is
too high (enter 'h'), too low (enter 'l') or correct
(enter 'y' for 'yes').

Is it 50? [(h)igh, (l)ow, (y)es] h
Is it 25? [(h)igh, (l)ow, (y)es] l
Is it 37? [(h)igh, (l)ow, (y)es] l
Is it 43? [(h)igh, (l)ow, (y)es] h
Is it 40? [(h)igh, (l)ow, (y)es] h
Is it 38? [(h)igh, (l)ow, (y)es] y
Yay! I got it!
PT[155]% 

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE :

#include <iostream>
using namespace std;
int main(){
cout<<"Think of a number between 1 and 100.";
cout<<"I will guess the number, then tell me if my guess is too high (enter 'h'), too low (enter 'l') or correct";
cout<<"(enter 'y' for 'yes')."<<endl; //all the instructions for the game
int i = 50,l = 0,h = 101; //l and h are lower and higher limits
char ch = 'a'; //variable to store user input
while(ch!='y'){
cout<<"Is it "<<i<<"? [(h)igh, (l)ow, (y)es]"<<endl;
cin>>ch;
if(ch=='y')
cout<<"Yay! I got it!"<<endl;
else if(ch=='h'){ //as number is smaller, it changes the upper limit
h=i;
i=int((l+h)/2);
}
else if(ch=='l'){ //as number is bigger, it changes the lower limit
l=i;
i=int((l+h)/2);
}
else
cout<<"Enter a valid response."<<endl; //to make the game more interactive
if(h == (l+1)){ //if the user is cheating, there must be a difference of one between l and h
cout<<"You are cheating"<<endl;
break;
}
}
}

OUTPUT :

Run 1 :Think of a number between 1 and 100.I will guess the number, then tell me if my guess is too high (enter 'h'), too low (enter 'l') or correct(

enter 'y' for 'yes').   

Is it 50? [(h)igh, (l)ow, (y)es]                                                                                                              

h                                                                                                                                             

Is it 25? [(h)igh, (l)ow, (y)es]                                                                                                              

h                                                                                                                                             

Is it 12? [(h)igh, (l)ow, (y)es]                                                                                                              

h                                                                                                                                             

Is it 6? [(h)igh, (l)ow, (y)es]

l

Is it 9? [(h)igh, (l)ow, (y)es]                                                                                                               

h                                                                                                                                             

Is it 7? [(h)igh, (l)ow, (y)es]                                                                                                               

y                                                                                                                                             

Yay! I got it!  

Run 2 :

Think of a number between 1 and 100.I will guess the number, then tell me if my guess is too high (enter 'h'), too low (enter 'l') or correct(

enter 'y' for 'yes').                                                                                                                         

Is it 50? [(h)igh, (l)ow, (y)es]                                                                                                              

h                                                                                                                                             

Is it 25? [(h)igh, (l)ow, (y)es]                                                                                                              

l  

Is it 37? [(h)igh, (l)ow, (y)es]                                                                                                              

h                                                                                                                                             

Is it 31? [(h)igh, (l)ow, (y)es]                                                                                                              

l                                                                                                                                             

Is it 34? [(h)igh, (l)ow, (y)es]

h

Is it 32? [(h)igh, (l)ow, (y)es]                                                                                                              

l                                                                                                                                             

Is it 33? [(h)igh, (l)ow, (y)es]                                                                                                              

h                                                                                                                                             

You are cheating  

Add a comment
Know the answer?
Add Answer to:
Part 1 Write a program that plays the other side of the game "I'm thinking of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Has to be written in C++. Visual studio. Write a C++ program to realize the game...

    Has to be written in C++. Visual studio. Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started,...

  • I'm writing a program in game for a dice game. During the game you decide if...

    I'm writing a program in game for a dice game. During the game you decide if you want to roll or pass to the next player, mine so far will just keep rolling until you roll the same number again (besides six) and then move to the next player. How do I get it to manually change players by the player response "y" or "n"? This is what I have for this part of the game simulator and it doesn't...

  • C++ Visual Studios Your program will attempt to guess a number that the user is thinking...

    C++ Visual Studios Your program will attempt to guess a number that the user is thinking of within the range [1, 20) (1 inclusive 20 exclusive). Your program will have 5 tries and each time the user should be able to tell your program if it is either correct, high, or low. If your program attempts 5 times but is not told that it is correct on the 5th try you should output "You cheated...". Otherwise your program should always...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Write a java program that simulates the operation of a Halloween Vampire Hunt game. In the...

    Write a java program that simulates the operation of a Halloween Vampire Hunt game. In the game scenario, you are a vampire hunting a victim in a dark cave. The cave consists of 10 by 10 little squares, numbered 0-9 on each side; your victim is in the cave, with x-coordinate and y-coordinate 0-9 (both integers). On each turn, you guess where your victim is, and try to bite her/him. Your “health” is determined by the number of bloodpoints you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT