My Number Wizard Console

Not as in depth as Derek’s masterpiece, but I did implement a check for cases where you’ve lead the wizard to an invalid state, also threw in an OR when handling the Enter key so we can support the numpad enter key too, and I encapsulated some parts slightly differently than the way shown in the course so I thought this might be worth sharing.

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

public class NumberWizard : MonoBehaviour
{
    int max;
    int min;
    int guess;

    void MakeGuess()
    {
        if (!(min < max))
        {
            Debug.Log("That doesn't make any sense.");
            Debug.Log("From what you've told me it has to be");
            Debug.Log("At least " + min);
            Debug.Log("And no more than " + max);
        }
        guess = (max + min) / 2;
        Debug.Log("Is it " + guess + "?");
    }

    // Use this for initialization
    void Start()
    {
        StartGame();
    }

    void StartGame()
    {
        min = 1;
        max = 1000;
        Debug.Log("Welcome to the Console of the Number Wizard!");
        Debug.Log("Think of a number within the range I specify.");
        Debug.Log("The maximum is " + max);
        Debug.Log("The minimum is " + min);
        Debug.Log("Respond to my guesses with:");
        Debug.Log("Up arrow to tell me to try higher");
        Debug.Log("Down arrow to tell me to try lower");
        Debug.Log("Enter when I get it right, which I definitely will...");
        max += 1;
        MakeGuess();
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            Debug.Log("Ah, it's higher is it?");
            min = guess;
            MakeGuess();
        }

        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            Debug.Log("Oh, lower then?");
            max = guess;
            MakeGuess();
        }

        else if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
        {
            Debug.Log("Splendid! I told you I'd get it!");
            StartGame();
        }
    }
}

Privacy & Terms