Hi everyone,
So this lessons challenge was to share our text/code so I figured I’d do that.
I have added some comments to make things easy to understand.
To make things a bit more interesting I added a counter to count how many times it took to guess the correct number.
I’m looking forward to learning more about programming and making games.
Phoenix Lucror
--------------------------------------- Code Below -----------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NumberWizard : MonoBehaviour
{
// Instance variable. These are acceptable within the methods in this class.
int max = 1000;
int min = 1;
int guess = 500;
int tries = 0;
// Use this for initialization
void Start ()
{
/* Print and Debug works similary. The difference is that using debug
gives us more flexability and control later in the course.
*/
// Instructions to the player.
Debug.Log("Comtrya! Welcome to Number Wizard."); // This is the first statement
Debug.Log("This is a number guessing game where I guess the number that you pick.");
Debug.Log("The highest number you can pick is: " + max);
Debug.Log("The lowest number you can pick is: " + min);
Debug.Log("To tell me if your number is higher or lower");
Debug.Log("Push Up = Higher, Push Down = Lower, Push Enter = Correct guess");
Debug.Log("Are you thinking on " + guess + "?");
tries = tries + 1;
// Fix the bug where the up key doesn't go to 1000.
max = max + 1;
}
// Update is called once per frame
void Update ()
{
// Get the input from the player
//Detect when the up arrow key is pressed down
if (Input.GetKeyDown(KeyCode.UpArrow))
{
min = guess;
guess = (max + min) / 2;
tries = tries + 1;
Debug.Log("Is the number you are thinking on higher or lower then " + guess + "?");
}
//Detect when the down arrow key is pressed down
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
max = guess;
guess = (max + min) / 2;
tries = tries + 1;
Debug.Log("Is the number you are thinking on higher or lower then " + guess + "?");
}
//Detect when the enter arrow key is pressed down
else if(Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("Excellent! The number you were thinking about was " +guess + "!");
Debug.Log("And it only took me " + tries + " tries to guess it! I must be a mastermind genius!");
}
}
}