Enhancing User Experience - Random Range +

Hello!

I was thinking about enhancing experience of “getting to the right number” in Number Wizard UI. Using Random() it may sometimes take too long for user to get to the right number, while using our previous method of (higher_guess + lower_guess)/2 seems too rigid, though it is faster.

So I have an idea to make it like this: first 3 guesses (or so) are random, and then we get the rigid method till the end. So first it feels like true guessing and then it gets straight to right answer in shortest (logically speaking ;)) way.

So my question is, how do I program that? Some hints? Should I be able to do it knowing as much as far (I just have finished Number Wizard UI part) or it will be clearer to do something like that after going deeper into the course?

Let’s see code as far:

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()
    {
        StartGame();
    }

    void StartGame()
    {

        NextGuess();

    }

    // Update is called once per frame

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

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

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


}

Hi,

You need a variable to keep track of the number of guesses. Then you can use an if/else statement to determine which code block is supposed to get called.

Thank you for your advise! Works quite well. I’ve created a method BetterUserExperience() which uses variable “randomy” to determine number of guesses needed to change from random to robust guessing. Code I’ve created:

public class NumberWizard : MonoBehaviour
{

    [SerializeField] int max;
    [SerializeField] int min;
    [SerializeField] TextMeshProUGUI guessText;
    int guess;
    int randomy;

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

    void StartGame()
    {

        NextGuess();

    }

    // Update is called once per frame

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

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

    void NextGuess()
    {
        randomy++;
        BetterUserExperience();
        guessText.text = guess.ToString();

    }


    void BetterUserExperience()
    {

            if (randomy >= 5)
            {
                guess = (min + max) / 2;
            }
            else
            {
                guess = (Random.Range(min, max + 1));
            }
        
    }

Well done. :slight_smile:

Does your solution work as expected?

Not as good as I expected, but it is fair enough, it is just guessing anyway :wink:

If it’s better than the original algorithm, that’s definitely something. :slight_smile:


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms