Already guess number bug

Here’s the problem I ran in to.

As we introduce some code so that a guess number can’t be repeated, we allow the game to decrease the max value or increase the min. But by doing that, we create a way for the game to stat guessing numbers superior to our initial max (in this case, 1000). This is because as we add +1 to the min, it can eventually get to values greater than 1000.

Does this makes sense or is it something that is happening just to me?

I think it can be fixed using conditionals, but I chose to remove this additional coding because I like that, eventually, the game reaches a point where it stops guessing numbers.

Yes, I have noticed that it will eventually go above 1000 as well.

EDIT: I saw that Rick has addressed this in another thread, saying we could “clamp” the values but he wasn’t wanting to introduce that concept yet. - About ‘Use Random.Range()’!

Here’s how I fixed it. That bug gets introduced in the code which ensures that you never repeat a guess twice by setting min to guess + 1, or max to guess - 1. The problem in that logic is that you’re never checking for the edge case of having min and max arrive at the same value. I added some extra if checks to check for those edge cases like this. The code in my else is kind of redundant, since I’m assigning a value I’ve already proved it has but it makes the logic of what’s happening, and why, a little clearer I think:

public void OnPressHigher()
{
    if (guess != max)
    {
        min = guess + 1;
    }
    else
    {
        min = guess;
    }
    NextGuess();
}
public void OnPressLower()
{
    if (guess != min)
    {
        max = guess - 1;
    }
    else
    {
        max = guess;
    }
    NextGuess();
}

Privacy & Terms