“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.