NumberWhizzz

Hey all (:

Just finished with the first part of this course, and wanted to share my code for the Number Wizard task.
I tried to match instructions best I could, but just added an extra function for “restarting” the game, as I didn’t want to display the same exact text as the Initial startup.

int maxNumber;
int minNumber;
int guess;

void SetValues()
{
    maxNumber = 1000;
    minNumber = 1;
    guess = 500;
}

void StartGame ()
{
    SetValues();

    Debug.Log("Hello Player,");
    Debug.Log("Please choose a random number between " + minNumber + " and " + maxNumber);
    Debug.Log("To begin, please tell us whether your number is higher or lower than " + guess);
    Debug.Log("Make your selection using the Up or Down Key. If the guess is correct, hit Enter");
    maxNumber += 1;
}

void NextGuess()
{
    Debug.Log("Aw damn...");
    guess = (maxNumber + minNumber) / 2;
    Debug.Log("Is it higher or lower than " + guess + "?");
}

void GameRestart()
{
    SetValues();

    Debug.Log("I can read your mind. It's truly spectacular isn't it?");
    Debug.Log("If you want to play again - my first guess is " + guess + "! You Know what to do!");
    maxNumber += 1;
}

void Start()
{
    StartGame();
}

void Update()
{
    if(Input.GetKeyDown(KeyCode.UpArrow))
    {
        minNumber = guess;
        NextGuess();
    }
    else if(Input.GetKeyDown(KeyCode.DownArrow))
    {
        maxNumber = guess;
        NextGuess();
    }
    else if(Input.GetKeyDown(KeyCode.Return))
    {
        Debug.Log("WOOHOO!");
        GameRestart();
    }
}

}

Privacy & Terms