My Version of Number Wizard

Hey all! Just started taking the course, this is my first time posting on these forums. Included is my version of the sample code (with a randomly generated max value, random guesses, and cheat detection):

/*
* Copyright (c) Ben Schiffler
* www.benschiffler.com
*/

using UnityEngine;

public class NumberWizard : MonoBehaviour {

    #region Variables
    public int max;
    public int min;
    private int guess;

    #endregion

    #region Unity Methods

    void Start () {
        StartGame();
    }

    void StartGame()
    {
        min = 1;
        max = Random.Range(1, 10) * 1000;

        print("=========================\n");
        print("Welcome to Number Wizard.\n");

        print("Choose a number between " + min + " and " + max + ", but don't tell me!\n");
        //initialize first guess
        guess = Random.Range(max,  min);

        print("Is the number higher or lower than " + guess + "? \n");
        print("Up = higher, down = lower, enter = equals.\n");
    }

    void Update () {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            //print("Up arrow pressed.");
            min = guess + 1;
            NextGuess();
        } else  if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            //print("Down arrow pressed.");
            max = guess;
            NextGuess();
        } else if (Input.GetKeyDown(KeyCode.Return))
        {
            print("I won!\n");
            StartGame();
        } 
    }

    void NextGuess()
    {
        guess = Random.Range(min, max);
        if (max == min)
        {
            print("No, it has to be " + guess + ", or you cheated. I won!\n");
            StartGame();

        }
        print("Higher or lower than " + guess + "\n");
        print("Up = higher, down = lower, enter = equals.\n");
    }


    #endregion
}

Any comments or feedback would be super appreciated, thanks!

3 Likes

Your code is well formatted, well commented, and easy to read. Nice job dude!

1 Like

Thanks so much!

1 Like

I like seeing the cheat detection feature. That was something I’d noticed as a design concern early on in the course.

2 Likes