Min Max variable setup

Hi @Samuel_Chavles

“min = guess” work? Isn’t that also saying 500 = 1?

Other way around actually, thus min = 500 it’s preparing the variables ready for the next guess.

At the top we’ve set “guess = 500” , but what in the code is allowing guess to change without conflicting with the original value of 500?

After the first guess the code replaces the value of 500 with a new value, based on whether the player indicated higher or lower, so the second guess may be 750 for example, therefore guess = 750

Just confused as to how the program is defining what it’s next minimum/maximum is going to be.

Lets run through an example, in words rather than code as such (and ignore rounding for now!)…

Game Initialisation

  • min = 1
  • max = 1000
  • guess = 500

Player told to choose a number, player chooses 326
Game displays guess of 500 to screen
Player indicates number is lower

  • max = 500
  • min = 1
  • guess = (max + min) / 2, thus, guess = 250

Game displays guess of 250 to screen
Player indicates number is higher

  • max = 500
  • min = 250
  • guess = (max + min) / 2, thus, guess = 375

I won’t keep going, but as you can see, depending on the indication of the player, either the max or min variable is set to which ever threshold is true. As the turns continue the game continues to guess the midway point.

Hope this helps.

2 Likes

Thanks so much @Rob !

This clears it up, my confusion was that I didn’t know that the program could assign guess a new value without conflicting with our original guess = 500, unless some special code was designated to allow it to do so based on the user inputs.

Just spent the last two hours writing this program from scratch myself (albeit not exactly the same, but it works!) to get myself situated before moving on to Section 3.

Appreciate the help!

1 Like

Hi @Samuel_Chavles,

You are more than welcome.

Just spent the last two hours writing this program from scratch myself (albeit not exactly the same, but it works!) to get myself situated before moving on to Section 3.

That’s great to hear, well done for pushing onward and especially for giving it a go by yourself, that’s a great way to learn.

Keep us all updated with your progress and I will look forward to playing any of the games you share in the Showcase forum :slight_smile:

Just a little insight in why exactly the original code works like it does:

If you pick 1000, you will keep pressing UP as the guess is always lower than your number. The MAX stays at 1000 but the MIN will change every time you press the UP key. You will end up having 999 as MIN and 1000 as MAX, resulting into this calculation: (999+1000)/2 = 1999/2 = 999,5 being rounded down as an INT resulting into 999 every time.

Changing the MAX to 1001 will result into the following calculation: (1000+1001)/2 = 2001/2 = 1000,5

This will be rounded down as well, resulting into a maximum int of 1000.

I havent continued after this lecture yet, so my apologies if this next solution is addressed later on:
Instead of starting off with max = max + 1;, put that line of code AFTER telling the player what the max number is. This way, the player will be informed that 1000 is the max number to guess instead of 1001.

Hope this helps a little to anyone new to this course!

About the bounds check, I think this also works :
if (max < 1)
{
max = max + 1;
}

Privacy & Terms