Code Challenge for Number Wizard

Did a small change to how the guess is initialized, thanks for the easy to understand tutorial :slight_smile:

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

public class NumberWizard : MonoBehaviour
{
    int max = 1000, min = 1;
    int guess = 0;
    int result;
    // Start is called before the first frame update
    void Start()
    {
        guess = (min + max) / 2;
        Debug.Log("Namaste, Welcome to Number Wizard ^_^");
        Debug.Log("Think of a number, and be as secretive as you can get ^-*");
        Debug.Log("Pick between " + min);
        Debug.Log("and " + max);
        Debug.Log("Tell me if your number is higher or lower than " + guess);
        Debug.Log("Push the up arrow if your number is higher than our guess\nPush the down arrow is your number is lower than our guess\nPush Enter if we guessed it right :D");
        max += 1;
    }

    // Update is called once per frame
    void Update()
    {
        //Detect when the up arrow key is pressed down
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            Debug.Log("Ah! So it's bigger number!!");
            min = guess;
            guess = (max + min) / 2;
            Debug.Log(guess);
        }
        
        //Detect when the down arrow key is pressed down
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            Debug.Log("Lower huh....");
            max = guess;
            guess = (max + min) / 2;
            Debug.Log(guess);
        }

        //Detect when the Return key is pressed down
        else if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("Gotcha :3");
            result = guess;
            Debug.Log("Your number is: " + result);
        }
    }
}

2 Likes

Congrats on making small changes! Awesome job! :fire:

Privacy & Terms