-~Guessing Game~-

I included some comments for clarification. Let me know what you all think!

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

public class NumberWizard : MonoBehaviour
{
    // Declaring variables in the class so that both start and update functions can see them
    // The Guess is 500, because the computer's guess will always be half of the max and minimum
    int max = 1000;
    int min = 1;
    int guess = 500;

    // Start is called before the first frame update
    void Start()
    {
        // Using int which does not include decimals 
        Debug.Log("Are You Ready to Play -~Guessing Game~-?");
        Debug.Log("Urgh, I've got to think of a better title. Alright, now think of a Number...");
        Debug.Log("No More than " + max);
        Debug.Log("...and no Less than " + min);
        Debug.Log("You'll want to use your Up and Down Arrow Keys if your Number is Higher or Lower. If my Guess is correct, Press Enter!");
        Debug.Log("Is Your Number Higher or Lower than " + guess + "?");

        max = max + 1;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            // We established guess = 500, and min = 1. If the player is thinking of a higher number, replace the min value with 500
            min = guess;
            // Our new guess will be (1000+500)/2 or 750 (half-way between 1000 and 500).
            guess = (max + min) / 2;
            Debug.Log("Is Your Number Higher or Lower than " + guess + "?");
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            max = guess;
            guess = (max + min) / 2;
            Debug.Log("Is Your Number Higher or Lower than " + guess + "?");
        }
        else if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("I guessed your Number! Thank you for playing!");
        }
    }
}

1 Like

Great looking code! Awesome job :fire:

Privacy & Terms