Guessing doesn't stop at 1000 (solution)

make this change in your function

public void OnPressHigher() 
{ 
    min = guess + 1;
    if (min <= 1000) { 
    NextGuess();
    }
9 Likes

thank you! this helped a lot

Better to change the code to something like this:

   public void OnPressHigher()
    {
        min = guess + 1;
        if ( min >= max)
        {
            min = max;
        }
        NextGuess();
    }

Reasons: do not depend on fixed numbers but use the variables (because range may change), and second to keep the code flow the same for both cases.

8 Likes

This worked! Thanks. I came on here to see if there was a solution. I tried a few things that didn’t quite work.

3 Likes

Thanks for this solution. I too noticed it was going higher than 1000. I’m glad this solution has prevented that.

1 Like

Thank you guys, this work)))

1 Like

I found a (in my opinion) more elegant solution (but probably also more complex to understand for complete beginners):

  1. Add the using Unity.Mathematics to the top of the file

  2. Change your OnPressHigher() and OnPressLower() to this:

public void OnPressHigher()
    {
        min = math.min(guess + 1, max);
        NextGuess();
    }

    public void OnPressLower()
    {
        max = math.max(guess - 1, min);
        NextGuess();
    }

math.min() takes two numbers and returns the lowest. This means that min will now never be higher than max

math.max() does the opposite. It takes two numbers and returns the highest. This means that max will now never be lower than min

2 Likes

This can also be a good solution:

if (guess < max)
{
     min = guess + 1;
     NextGuess();
}

Thank you for adding this! I might be the only one retaking this course, so I doubt there will be an update to the video, but I appreciate that other people had the same problem and posted solutions.

Privacy & Terms