Continuing to Press Higher

In reading the initial thread, there are many of us that realized the issue if players lie and continue to press higher. I am sharing my solution to that as it makes a couple of subtle changes to the suggested code. Two changes.

  1. Subtracting 1 from max during the OnPressLower() method and then adding 1 back in the NextGuess() method seemed redundant. Because the Max end of the Random is exclusive, I don’t think you have to do anything to ensure that the max is NOT guessed a second time. I removed those and it seems to be functioning correctly.

  2. I added an if statement that only increments the min in the event that min < max. This ensures that you cant have minimum that is greater than the max which creates the ability to go above the max number.

Note: I named my variables minNumber and maxNumber

Any thoughts?

    // Method to Process User Pressing Higher Button
    public void OnPresssHigher()
    {
        minNumber = guess;
        if (minNumber < maxNumber) minNumber = minNumber + 1;
        NextGuess();
    }

    // Method to Process User Pressing Lower Button
    public void OnPressLower()
    {
        maxNumber = guess;
        NextGuess();
    }

    // user this method to update the gues
    void NextGuess()
    {
        guess = Random.Range(minNumber, maxNumber);
        guessText.text = guess.ToString();
    }
2 Likes

I thought it was redundant as well, but it does solve one extreme edge case. It could never guess the max value (i.e. 1000) on the very first guess.

This is my solution and it looks work fine:

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

Thanks for this. Came here looking for this issue as well and this seems to do the trick.

Agree with #1. Just leave the part of

max = max + 1;

in start to fix the max-limit problem.

For #2 i prefer the following solution rather than a if-statement use Mathf.min which chooses the lower value of the two variables given to it.

min = Mathf.Min(guess + 1, max);

Privacy & Terms