02-Number-Wizard-Console icon indicating copy to clipboard operation
02-Number-Wizard-Console copied to clipboard

The bug in the video

Open kristi-swat opened this issue 6 years ago • 1 comments

Hi,

This is not really an issue, but I didn't know where to ask this question, for some reason can't join the discord chat.

I am trying to understand why setting the max to max+1 within Start fixed the bug? That value is supposed to be only within Start method right?

kristi-swat avatar Nov 19 '19 20:11 kristi-swat

Hi @kristi-swat,

The problem here is how the program convert into integer a non integer number.

When you reach the top margin you have max = 1000 and min = 999.

As the user tell the program that your number is higher, the program calculate the next guess with (1000+999) / 2 = 999,5, rounding it to 999 and setting it again as the min.

I would have fixed this problem using another approach.

When you ask the user if a numer is higher or lower than the suggested, there's no reason to put this number as a min or max in the new guess.

I didn't run any unit tests to tests the limits of this apporach, but I think is clearer.

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            min = guess + 1;
            NextGuess();
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            max = guess - 1;
            NextGuess();
        }

kabeocho avatar Sep 22 '21 13:09 kabeocho