Finished Number Wizard with guess counter

I added in the random starting number from the end of the NumberWizard section. I then decided to have it count how many attempts it has made before getting the correct answer, and then print that out to the player.
Feedback is appreciated (Whether it’s good feedback or bad, as long as I learn something from it I don’t mind.) Please note that this is my first time learning code, and I started the course about 24 hours ago, so if my commenting is OTT, or the structure is bad then I wan’t to know now before it becomes a bad habit that I can’t get out of! :smiley:

using UnityEngine;

using System.Collections;

public class NumberWizard : MonoBehaviour {

// Use this for initialization
int max;
int min;
int guess;
int Attempts;

void Start () {
	StartGame ();
}

void StartGame () {

	max = 10000; //Set the Max, min and guess
	min = 1;
	guess = Random.Range(min, max); //Start with a random number between min and max.
	Attempts = 1; // First "Is the number high or lower than" counts as attempt #1.
	
	print ("========================================");
	print ("Welcome to Number Wizard");
	print ("Pick a number in your head, but don't tell me");
	
	print ("The highest number you can pick is " + max);
	print ("The lowest number you can pick is " + min);
	
	print ("Is the number higher or lower than " + guess);
	print ("Up arrow = higher, Down arrow = lower, Return = Equal");
	
	max = max + 1; //To make 10000 available due to code rounding down.
}

// Update is called once per frame
void Update () {
	if (Input.GetKeyDown(KeyCode.UpArrow)) {
		min = guess;
		NextGuess ();
	} else if (Input.GetKeyDown(KeyCode.DownArrow)) {
		max = guess;
		NextGuess ();
	} else if (Input.GetKeyDown(KeyCode.Return)) {
		print("I won in " +Attempts +" guesses!"); //would print "I won in X guesses!"
		StartGame ();
	}
}
void NextGuess () {
	guess = (max + min) /2;
	Attempts = Attempts +1; //After every guess, increase the int Attempts by 1.
	print ("Higher or lower than " + guess);
	print ("Up arrow = higher, Down arrow = lower, Return = Equal");
}

}

The only thing I might do is put the max+1 line in an if statement to only
do it if needed, as every time you run StartGame it’ll add 1 otherwise.

That or just set the max to 10001 straight off and just -1 whenever it
displays the, “highest number you can pick,” line.

Good idea, I will do that and test it out :slight_smile: Thank you for your feedback, much appreciated :smiley:

Privacy & Terms