Number Wizard Extra Credit - Tom

I’ve done some of the extra credit. I actually worked as a CSharp (My keyboard doesn’t have a hash key) developer for a while within a company so I had some confusion with how the console within unity won’t read CSharp console commands (Like Console.Read()) and therefore opted out of letting the user choose. There may be a way around this but it is early on.
On the other hand, I did input the random guess at the start as well as upping the range to 10,000. Pretty basic, but was fun. I haven’t added any magic numbers like another fellow, but I did appreciate that it was cool!

Anyway, here’s my code:

public class NumberWizards : MonoBehaviour {

int max;
int min;
int guess;
// Use this for initialization
void Start () {
	StartGame ();
}

void StartGame () {
	max = 10000;
	min = 1;
	guess = Random.Range(1,10000);
	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 + "!");
	max = max + 1;
	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, enter = equal");
}
	
// Update is called once per frame
void Update(){
	if (Input.GetKeyDown(KeyCode.DownArrow)) {
		max = guess;
		NextGuess ();
	} else if (Input.GetKeyDown(KeyCode.UpArrow)) {
		min = guess;
		NextGuess ();
	} else if (Input.GetKeyDown(KeyCode.Return)) {
		print ("I won!");
		StartGame ();
	}
}

void NextGuess() {
	guess = (max + min) / 2;
	print ("Higher or lower than " + guess + "?");
	print ("Up arrow = higher, down arrow = lower, enter = equal");
}

}
2 Likes

Thats pretty much the same as what I did regarding random.range, just made it dynamic as apposed to static.

using UnityEngine;
using System.Collections;

public class NumberWizard : MonoBehaviour {

/*
	Numbers don't reset due to the fact 
	max and min are now at the last guessed numbers 
	ie: you complete the game on 761, that will be your max guess.
	as you set max and min in every update it will keep max / min at that 
*/
int max;
int min;
int guess;

// Use this for initialization
void Start () {
	StartGame();
}

void StartGame () {
	max = 1000;
	min = 1;
	guess = Random.Range(min, max);
	
	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 lower or higher than " + guess + "?");
	print("Up = higher, Down = lower, Return = equal.");
	
	max = max + 1;
}

// Update is called once per frame
void Update () {
	/*
		Using else if instead of multiple if statements
		to only log one key press at a time.
	*/
	if(Input.GetKeyDown(KeyCode.UpArrow)){
		//print("Up Arrow Pressed");
		min = guess;
		NextGuess();
	}else if(Input.GetKeyDown(KeyCode.DownArrow)){
		//print("Down arrow pressed");
		max = guess;
		NextGuess();
	}else if(Input.GetKeyDown(KeyCode.Return)){
		print("I Won!");
		StartGame();
	}
}

void NextGuess(){
	guess = (max + min) / 2;
	print("Higher or lower than " + guess);
	print("Up = higher, Down = lower, Return = equal.");
    }
}

have been looking into user input, but as you stated, it seems rather impossible at this stage, and adding in a GUI would defeat the point of it being purely console based.

I thought about making it min, max but in all honesty it just didn’t seem any more viable in this specific situation. Obviously using the variables may have been better looking code - but technically the two inputs would’ve been static regardless!
That’s me being nitpicky though. We were set in our ways when we were in the office.

Also, yeah, without implementing a GUI there is no way to connect standard c# commands to a console. I think the reasoning behind this is that if you want to make a console game, go make it in a different c# IDE which is designed for console applications instead of a game engine.

You could take it a step further and have your number range that you can pick a number for being dynamic too.

i.e.

   void StartGame()
{
    // Define variables for Minimum and Maximum guess values
    minGuess = 1;
    maxGuess = Random.Range(2,1000);

    // AI guessing methods
    currentGuess = Random.Range(minGuess,maxGuess);

This would make the game unique each play for what number range you can work with. Really not something you would do in a number wizard game played with real people but hey, its a computer game :slight_smile:

Privacy & Terms