Narrowing down numbers for a guessing game


#1

Hi! I have a project in which I need to make a number guessing game. I’ve already figured how to the code for asking the user to guess lower or higher but another component was that the game should be able to narrow down the numbers to help the user guess the number. After the first and second guess the program must give a message saying “You’ve already narrowed it down to guess a and guess b”. How do I do this exactly?


#2

You’ll first need some more variables to keep track of how close the user has guessed.

You will need a variable to remember the largest number the user has guess that is still smaller than the target number.

You will need a variable to remember the smallest number the user has guess that is still larger than the target number.

You will also need two boolean variables to record if the values stored in the previous two variables are even meaningful.

Let’s give these variables names, specify their types, and initialize their values:

float lower_bound = 0;
float upper_bound = 0;
boolean lower_bound_meaningful = false;
boolean upper_bound_meaningful = false;

Now if it easy and useful to display the information we want to see:

if( lower_bound_meaningful ){
  text( "The target is more than " + lower_bound, 20, 20);
} else {
  text( "You haven't determined a lower bound yet!", 20, 20);
}
// Do the same for upper bound too...

Now you just need to make sure the values stored in these variables remain correct. Consider:

  • When should a new lower bound be set?
  • What about if no lower bound exists?
  • What about if a better lower bound exists?
  • What about the upper bound?

I’ll leave it up to you to get working…

// When user guesses:
if( guess < ??? && ???????? ){
  lower_bound = ???;
  lower_bound_useful = ???;
}

#3

Would inserting it inside the if else statement for determining if it is higher or lower be easier or would a separate if statement be better.

 else if (guess < posInt)
     {
      println("Too low. Please guess again.");
      lowerBound = guess;
      if (guess > lowerBound)
       lowerBoundSet = false;
       println ("You've narrowed down to " + lowerBound + " and " + upperBound);
     }
     else if (guess > posInt)
     {
      println("Too high, Please guess again.");
      upperBound = guess;
      if (guess > upperBound)
      upperBoundSet = false;
      println ("You've narrowed down to " + lowerBound + " and " + upperBound);
     }

#4

-a- it does not matter , just about writing style, but

if ( a < b ) ...
else if ( a > b ) ...

is the same as

if ( a < b ) ...
else  ...

oh NO!
what is if
a == b
??
//__________________________________________
-b- but the later part has problems:

c = a
if ( a > c ) 

will never be the case!

//__________________________________________

-c- are you aware of the 2 writing styles:

if ( a > c ) dothis
dothat

is same as:

if ( a > c ) { 
  dothis
} 
  dothat

so pls format your code with
[ctrl][t]
and you will never get confused by your

if ( a > c ) 
dothis
dothat