Continuing to Press Higher and Lower

with your recent code, if you keep pressing lower, the max and min will be smaller than what it should be. And if you keep pressing higher button, the min will be greater than max. So I’ve changed something here in NextGuess():

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class NumberWizard : MonoBehaviour
{

    [SerializeField] int max;
    [SerializeField] int min;
    [SerializeField] TextMeshProUGUI guessText;
    int guess;
    // Use this for initialization
    void Start()
    {
        StartGame();
    }

    void StartGame()
    {
        NextGuess();
        }

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

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

    // Update is called once per frame

    void NextGuess()
    {
        if (min > max)
        {
            min = max;
        }

        if (max <= min)
        {
            min = guess;
        }

        guess = Random.Range(min, max + 1);
        guessText.text = guess.ToString();
    }
}

i just found out that the result would be incorrect

if you can solve the problem, please comment on this post. Thanks very much.

I solved this by putting my logic in each of the higher/lower methods, checking that the current guess was not already at max or min.

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

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

private void NextGuess()
{
    guess = Random.Range(min, max + 1);
    guessText.text = guess.ToString();
}
2 Likes

i have never seen != before, thank you for reply to me.

I’ve thought that before too but i didn’t know what is not equal to in c#. it’s kinda stupid that i didn’t look up to search for it on the internet.

Thanks for the solution ottopia. I was having the same problem with the edge cases.

Hey there, guys.

I solved it this way:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class NumberWizard : MonoBehaviour
{
    [SerializeField] int max;
    [SerializeField] int min;
    [SerializeField] TextMeshProUGUI guessText;

    int guess;

    // Start is called before the first frame update
    void Start()
    {
        NextGuess();
    }

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

    public void OnPressLower()
    {
        max = guess;
        NextGuess();
    }

    void NextGuess()
    {
        guess = Random.Range(min, max);
        guessText.text = guess.ToString();
    }
}

When pressing the HigherButton, it will just return early and do nothing else if min and max are already equal.
:grinning:
Thought it would be nice to also share this approach :smile:

I solved it this way, using Mathf:

    void NextGuess ()
    {
        guess = Random.Range(min, max + 1);
        guessText.text = guess.ToString();
    }

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

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

Hi

Just wanted to say this works nicely, but with one caveat: you can only guess 1000 after you have guessed 999. That’s because of the exclusion of the highest number in

guess = Random.Range(min, max);

But if you use the

guess = Random.Range(min, max + 1);

like in the course this check breaks.
For that you’d write

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

it will work as intended while giving you the full guessing range. :slightly_smiling_face:

Hey there.
I simply coded like this;

void NextGuess()
    {
        if (maxGuess > minGuess)
        {
            guess = Random.Range(minGuess, maxGuess + 1);
            guessText.text = guess.ToString();
        }
        else
        {
            minGuess = maxGuess;
        }
    }

This way the min value can’t pass 1000 while you hit higher, and also the min value can’t go lower than 0 while you hit lower.

The display will show you max 1000 and min 1.

I just noticed sometimes the guess cant’ go below 2 or over 999. I am gonna add the missing part.

I updated the else statement like this;

else if (maxGuess == minGuess)
        {
            maxGuess = minGuess;
            guess = maxGuess;
            guessText.text = guess.ToString();
        }

I monitored that the guess display can now hit 1 and 1000, however the min can be 2 while the max is 0 or the min can be 1001 while the max 999.

Fantastic solution!

int guess;
    private const string error = "Error";

    // Use this for initialization
    void Start()
    {
        StartGame();
    }
    void StartGame()
    {
        NextGuess();
    }
    public void OnPressHigher()
    {
        minVar = guess + 1;
        NextGuess();
    }
    public void OnPressLower()
    {
        maxVar = guess - 1;
        NextGuess();
    }
    public void NextGuess()
    {
        guess = Random.Range(minVar, maxVar + 1);
        guessText.text = guess.ToString();
        if (guess > 1000)
        {
            guessText.text = error;
        }
    }
}

Dat is my solution for it guys, I declared a constant variable on top, and then if guess variable is higher than 1000 it shows “error”!:hugs:

Hi all, this is my solution:

 void StartGame()
    {

        NextGuess();

    }

    public void OnPressHigher()
    {

        min = guess;
        NextGuess();

    }

    public void OnPressLower()
    {

        max = guess;
        NextGuess();
               
    }
  public void NextGuess()
    {
        guess = Random.Range(min, max + 1);
        guessText.text = guess.ToString();
    }

Glad to see I wasn’t the only one who noticed if the player keeps pushing buttons they can break the system good quick fix with !=.

Privacy & Terms