Why guess=min; and min=guess; arent same?

why guess=min; and min=guess; arent same ?

Hi,

Can you expand on your question a bit please, do you mean, why the values aren’t the same?

min = guess;

guess = (min + max)/ 2 ;

(when i changed places like that )

guess = min;

guess = (min + max)/ 2 ;

game always says 500 when u press up i just didnt understand whats the difference.

So, you’re saying that the max value always remains at 500, when you press the button to state your are thinking of a number which is higher, is that correct?

if (Input.GetKeyDown(KeyCode.UpArrow)){
			
	min = guess;

	guess = (max + min) / 2 ;

	print ("is that higher or lower than?" + guess);

in here when i change places guess=min; it stuck at 500 yeap

guess should only be being set via the calculation for the next guess, e.g. (min + max) / 2;

min and max are used as boundary variables. Each time the player indicates higher or lower, one of them will be updated to a new value and then used within the calculation again to create a new guess…

Let’s work through a few steps of a possible game;

  • New Game

    min = 1
    max = 500
    guess = (500 + 1) / 2 = 251
    
  • Player states their number is higher

    min = guess (251)
    max = 500 (unchanged)
    guess = (251 + 500) / 2 = 376
    
  • Player states their number is higher

    min = guess (376)
    max = 500 (unchanged)
    guess = (376 + 500) / 2 =  438
    
  • Player states their number is lower

    min = 376 (unchanged from last turn)
    max = 438 (now set as the highest possible number)
    guess = (376 + 438) / 2 = 407
    

…and so on, until the range is reduced and reduced until there can be only one possibility.

Does this help highlight what the code is doing?


See also;

1 Like

so pc is smart :slight_smile: thank u so much i guess i get it.

1 Like

You’re welcome, if there is anything else just post and someone will aim to help you.

It helps if you can give details though, and examples, this makes it easier to understand :slight_smile:

1 Like

Rob it helped a lot to me but if u are still here i didin’t get one point;

min =guess;

		guess = (max + min) / 2 ;
		
		print ("is that higher or lower than?" + guess);

this code works fine but

guess =min;

		guess = (max + min) / 2 ;
		
		print ("is that higher or lower than?" + guess);

this one doesnt work and the only difference between them guess=min or min =guess why they arent doing same job 2+2=4 and 4=2+2 isnt the same thing ?

Hi,

guess is variable which is storing a calculated value, which uses both the min and max variables.

Each turn of the game we want to update either min or max in order to use them again in the calculation with the new set of values so that we can calculate a new guess.

In your example above, if min was equal to 2, then guess would not equal 2 + 2, it would simply equal 2, e.g. the value of min.

Let’s put a more in context example together. At the start of the game;

min = 1
max = 500

We are using a very simple calculation for the game to create a new guess, simply taking these two values and finding the mid-point by adding them together and dividing them by two.

So on the first turn, the guess is going to be in the middle of the range of values from 1 to 500 (rounded because we are using integers, whole numbers). So, using our calculation we get 251.

If the player indicates that their number is higher/lower we need to reset our boundary variables accordingly. So, if they choose lower, we now know that 251 is the maximum possible value, because they have said its lower, thus we can throw away the 500, and replace it with 251. Thus, max = 251.

The same would have been true if they had chosen higher, we would then know that 251 was our minimum possible value, thus we can throw away the 1, and replace it with 251, min = 251.

guess is then used to store the next calculated value, using both min and max.

If you state guess = min you are overwriting the value stored in guess with whatever min is set to, even if you use the calculation first, it would then be replaced, for example;

guess = (max + min) / 2;  // let's assume guess now = 251
guess = min; // guess now equals whatever value min was, in this example, it would have been 1

Hope this helps.


See also;

1 Like

thank you so much again , that was really helpful for me

1 Like

Happy to help :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms