My Final Console Code For Number Wizard

This code could probably be simplified by someone more advanced, but I wanted to share my final code for the console version of Number Wizard…

Some of the features included:

  1. There are different text scripts to give the player some variety when pressing up and down as the computer makes guesses (probably the main reason the code is so long)
  2. There’s also a guess count to automatically end the game if the computer cannot guess the number after a certain amount of guesses
  3. The guesses themselves are randomly assigned between the min and max
  4. The text displays differently if the computer guesses the same value as the min (it won’t ask if it is lower) or the max (it won’t ask if it is higher)
  5. If the min (or max) equals the last guess, the computer will give an error if the player tries to press down (or up, respectively)
  6. There are line-breaks in the text so the console never shows the words “UnityEngine.Debug:Log(Object)”
  7. A few more little bits

Let me know what you guys think, and definitely let me know if I could have saved a whole bunch of lines somewhere! Also, if others have suggested additions or improvements, I’m all ears, or eyes, as this is a chat forum, but you get it…

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

public class NumberWizard : MonoBehaviour {

	int max;
	int min;
	int guess;
	int guessCount;
	int runTextScript;

	// Use this for initialization
	void Start () {
		runTextScript = Random.Range(1,4);
		max = 1000;
		min = 1;
		guessCount = 0;
		guess = Random.Range(min,max+1);
		guessCount++;
		Debug.Log("Welcome To Number Wizard" +
			"\nPick A Number, Don't Tell Me What It Is!");
		Debug.Log("The Highest Number You Can Pick Is " + max +
			"\nAnd The Lowest Number You Can Pick Is " + min);
		Debug.Log("Push Up To Tell Me It's Higher, " +
			"Push Down To Tell Me It's Lower," +
			"\nPush Return To Tell Me I'm Right," +
			" Press Space At Any Time To Have Me Start Again");
		if (guess >= max){
			Debug.Log("For Guess Number " + guessCount + ", Tell Me, Is Your Number" +
				"\nEqual To Or Lower Than " + guess + "?");
		} else if (guess <= min){
			Debug.Log("For Guess Number " + guessCount + ", Tell Me, Is Your Number" +
				"\nEqual To Or Higher Than " + guess + "?");
		} else {
			Debug.Log("For Guess Number " + guessCount + ", Tell Me, Is Your Number" +
				"\nEqual To, Higher Or Lower Than " + guess + "?");
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (guessCount <= 10){
			if (Input.GetKeyDown(KeyCode.Return)){
				guessCount = guessCount + 11;
				if (runTextScript == 1){
					Debug.Log("Woo-Hoo, I Win, I'm Just Too Good!" +
						"\nPress Backspace To Play Again!");
				} else if (runTextScript == 2){
					Debug.Log("I Told You I Was Gonna Guess Right!" +
						"\nPress Backspace To Play Again!");
				} else if (runTextScript == 3){
					Debug.Log("That Was Way Too Easy! Give Me A Challenge Next Time!" +
						"\nPress Backspace To Play Again!");
				}
			}
			if (Input.GetKeyDown(KeyCode.Space)){
				guessCount = guessCount + 11;
				if (runTextScript == 1){
					Debug.Log("I Give Up! You're Just Too Good For Me!" +
						"\nPress Backspace To Play Again!");
				} else if (runTextScript == 2){
					Debug.Log("I'm Never Gonna Get This, I Quit!" +
						"\nPress Backspace To Play Again!");
				} else if (runTextScript == 3){
					Debug.Log("This Is Way Too Hard! Are You Sure Your Number Is Between " + min + " & " + max + "?" +
						"\nPress Backspace To Play Again!");
				}
			}
		}
		if (guessCount >= 11){
			GameOver();
		}
		if (guessCount == 10){
			if (Input.anyKeyDown){
				guessCount++;
				if(runTextScript == 1){
					Debug.Log("Oh No, I Lost! How Did This Happen?!" +
						"\nPress Backspace To Play Again!");
				} else if (runTextScript == 2){
					Debug.Log("You've Bested Me This Time! But I Will Win The Next One!" +
						"\nPress Backspace To Play Again!");
				} else if (runTextScript == 3){
					Debug.Log("Something Doesn't Quite Add Up Here, How Could I Have Lost?!" +
						"\nPress Backspace To Play Again!");
				}
			}
		}
		if(runTextScript == 1){
			if (guessCount == 9){
				if (Input.GetKeyDown(KeyCode.DownArrow)){
					runTextScript = Random.Range(1,4);
					if(min == guess){
						Debug.Log("Oh No, There Seems To Be An Error" +
							"\nI Cannot Guess Lower Than The Minimum!");
					} else {
						GuessLower();
						if(((guess == min && guess != max)||guess != max && guess != min)||guess == max && guess != min){
								Debug.Log("For Guess Number " + guessCount + ", My Final Guess! Tell Me, Is Your Number" +
								"\nEqual To " + guess + "?");
						}
					}
				} else if (Input.GetKeyDown(KeyCode.UpArrow)){
					runTextScript = Random.Range(1,4);
					if(max == guess){
						Debug.Log("Oh No, There Seems To Be An Error" +
							"\nI Cannot Guess Higher Than The Maximum!");
					} else {
						GuessHigher();
						if(((guess == min && guess != max)||guess != max && guess != min)||guess == max && guess != min){	
							Debug.Log("For Guess Number " + guessCount +", My Final Guess! Tell Me, Is Your Number" +
								"\nEqual To " + guess + "?");
						}
					}
				} if (guess == min && guess == max){
					Debug.Log("Hang On A Minute!" +
						"\nThe Answer Is " + guess + "!");
					Debug.Log("Woo-Hoo, I Win!" +
						"\nPress Backspace To Play Again!");
					guessCount = 11;
				}
			}
			if (guessCount <= 8){
				if (Input.GetKeyDown(KeyCode.DownArrow)){
					runTextScript = Random.Range(1,4);
					if(min == guess){
						Debug.Log("Oh No, There Seems To Be An Error" +
							"\nI Cannot Guess Lower Than The Minimum!");
					} else {
						GuessLower();
						if(guess == min && guess != max){
							Debug.Log("For Guess Number " + guessCount + ", Tell Me, Is Your Number" +
								"\nEqual To Or Higher Than " + guess + "?");
					} else if (guess != max && guess != min){
						Debug.Log("For Guess Number " + guessCount + ", Tell Me, Is Your Number" +
							"\nEqual To, Higher Or Lower Than " + guess + "?");
					} else if (guess == max && guess != min){
						Debug.Log("For Guess Number " + guessCount + ", Tell Me, Is Your Number" +
							"\nEqual To Or Lower Than " + guess + "?");
						}
					}
				} else if (Input.GetKeyDown(KeyCode.UpArrow)){
					runTextScript = Random.Range(1,4);
					if(max == guess){
						Debug.Log("Oh No, There Seems To Be An Error" +
							"\nI Cannot Guess Higher Than The Maximum!");
					} else {
						GuessHigher();
						if(guess == max && guess != min){
							Debug.Log("For Guess Number " + guessCount +", Tell Me, Is Your Number" +
								"\nEqual To Or Lower Than " + guess + "?");
					} else if (guess != max && guess != min){
						Debug.Log("For Guess Number " + guessCount + ", Tell Me, Is Your Number" +
							"\nEqual To, Higher Or Lower Than " + guess + "?");
					} else if (guess != max && guess == min){
						Debug.Log("For Guess Number " + guessCount + ", Tell Me, Is Your Number" +
							"\nEqual To Or Higher Than " + guess + "?");
						}
					}
				} if (guess == min && guess == max){
					Debug.Log("Hang On A Minute!" +
						"\nThe Answer Is " + guess + "!");
					Debug.Log("Woo-Hoo, I Win!" +
						"\nPress Backspace To Play Again!");
					guessCount = 11;
				}
			}
		}
		else if(runTextScript == 2){
			if (guessCount == 9){
				if (Input.GetKeyDown(KeyCode.DownArrow)){
					runTextScript = Random.Range(1,4);
					if(min == guess){
						Debug.Log("Something Seems To Have Gone Wrong" +
							"\nYou Cannot Tell Me The Answer Is Lower Than The Minimum!");
					} else {
						GuessLower();
						if(((guess == min && guess != max)||guess != max && guess != min)||guess == max && guess != min){
							Debug.Log("Okay Then, Since Guess Number " + guessCount + " Is My Last Guess," + guess + 
								"\nIs Your Number " + guess + "?");
						}
					}
				} else if (Input.GetKeyDown(KeyCode.UpArrow)){
					runTextScript = Random.Range(1,4);
					if(max == guess){
						Debug.Log("Something Seems To Have Gone Wrong" +
							"\nYou Cannot Tell Me The Answer Is Higher Than The Maximum!");
					} else {
						GuessHigher();
						if(((guess == min && guess != max)||guess != max && guess != min)||guess == max && guess != min){
							Debug.Log("Okay Then, Since Guess Number " + guessCount + " Is My Last Guess" + guess +
								"\nIs Your Number " + guess + "?");
						}
					}
				} if (guess == min && guess == max){
					Debug.Log("Oooh, I Know What It Is!" +
						"\nThe Answer Has To Be " + guess + "!");
					Debug.Log("I Told You I Was Gonna Guess Right!" +
						"\nPress Backspace To Play Again!");
					guessCount = 11;
				}
			}
			if (guessCount <= 8){
				if (Input.GetKeyDown(KeyCode.DownArrow)){
					runTextScript = Random.Range(1,4);
					if(min == guess){
						Debug.Log("Something Seems To Have Gone Wrong" +
							"\nYou Cannot Tell Me The Answer Is Lower Than The Minimum!");
					} else {
						GuessLower();
						if(guess == min && guess != max){
							Debug.Log("Okay Then, Guess Number " + guessCount + " Is " + guess + "!" +
								"\nIs Your Number Equal To Or Higher Than " + guess + "?");
						} else if (guess != max && guess != min){
							Debug.Log("Okay Then, Guess Number " + guessCount + " Is " + guess + "!" +
								"\nIs Your Number Equal To, Higher Or Lower Than " + guess + "?");
						} else if (guess == max && guess != min){
							Debug.Log("Okay Then, Guess Number " + guessCount + " Is " + guess + "!" +
								"\nIs Your Number Equal To Or Lower Than " + guess + "?");
						}
					}
				} else if (Input.GetKeyDown(KeyCode.UpArrow)){
					runTextScript = Random.Range(1,4);
					if(max == guess){
						Debug.Log("Something Seems To Have Gone Wrong" +
							"\nYou Cannot Tell Me The Answer Is Higher Than The Maximum!");
					} else {
						GuessHigher();
						if(guess == min && guess != max){
							Debug.Log("Okay Then, Guess Number " + guessCount + " Is " + guess + "!" +
								"\nIs Your Number Equal To Or Higher Than " + guess + "?");
						} else if (guess != max && guess != min){
							Debug.Log("Okay Then, Guess Number " + guessCount + " Is " + guess + "!" +
								"\nIs Your Number Equal To, Higher Or Lower Than " + guess + "?");
						} else if (guess == max && guess != min){
							Debug.Log("Okay Then, Guess Number " + guessCount + " Is " + guess + "!" +
								"\nIs Your Number Equal To Or Lower Than " + guess + "?");
						}
					}
				} if (guess == min && guess == max){
					Debug.Log("Oooh, I Know What It Is!" +
						"\nThe Answer Has To Be " + guess + "!");
					Debug.Log("I Told You I Was Gonna Guess Right!" +
						"\nPress Backspace To Play Again!");
					guessCount = 11;
				}
			}
		}
		else if(runTextScript == 3){
			if (guessCount == 9){
				if (Input.GetKeyDown(KeyCode.DownArrow)){
					runTextScript = Random.Range(1,4);
					if(min == guess){
						Debug.Log("This Doesn't Make Any Sense" +
							"\nThe Minimum Guess Available To Me Is " + min + "! I Can't Guess Any Lower!");
					} else {
						GuessLower();
						if(((guess == min && guess != max)||guess != max && guess != min)||guess == max && guess != min){
								Debug.Log("Hmmm, I'm On Guess Number " + guessCount + " Now, So I Can't Make Another Mistake!" +
								"\nIs Your Number " + guess + "?");
						}
					}
				} else if (Input.GetKeyDown(KeyCode.UpArrow)){
					runTextScript = Random.Range(1,4);
					if(max == guess){
						Debug.Log("This Doesn't Make Any Sense" +
							"\nThe Maximum Guess Available To Me Is " + max + "! I Can't Guess Any Higher!");
					} else {
						GuessHigher();
						if(((guess == min && guess != max)||guess != max && guess != min)||guess == max && guess != min){
							Debug.Log("Hmmm, I'm On Guess Number " + guessCount + " Now, So I Can't Make Another Mistake!" +
							"\nIs Your Number " + guess + "?");
						}
					}
				} if (guess == min && guess == max){
					Debug.Log("I Definitely Know What It Is Now!" +
						"\nIt Can't Be Anything But " + guess + "!");
					Debug.Log("That Was Way Too Easy! Give Me A Challenge Next Time!" +
						"\nPress Backspace To Play Again!");
					guessCount = 11;
				}
			}
			if (guessCount <= 8){
				if (Input.GetKeyDown(KeyCode.DownArrow)){
					runTextScript = Random.Range(1,4);
					if(min == guess){
						Debug.Log("This Doesn't Make Any Sense" +
							"\nThe Minimum Guess Available To Me Is " + min + "! I Can't Guess Any Lower!");
					} else {
						GuessLower();
						if(guess == min && guess != max){
							Debug.Log("Thinking Carefully About Guess Number " + guessCount + ", I Wonder, Is Your Number" +
								"\nEqual To Or Higher Than " + guess + "?");
						} else if (guess != max && guess != min){
							Debug.Log("Thinking Carefully About Guess Number " + guessCount + ", I Wonder, Is Your Number" +
								"\nEqual To, Higher Or Lower Than " + guess + "?");
						} else if (guess == max && guess != min){
							Debug.Log("Thinking Carefully About Guess Number " + guessCount + ", I Wonder, Is Your Number" +
								"\nEqual To Or Lower Than " + guess + "?");
						}
					}
				} else if (Input.GetKeyDown(KeyCode.UpArrow)){
					runTextScript = Random.Range(1,4);
					if(max == guess){
						Debug.Log("This Doesn't Make Any Sense" +
							"\nThe Maximum Guess Available To Me Is " + max + "! I Can't Guess Any Higher!");
					} else {
						GuessHigher();
						if(guess == max && guess != min){
							Debug.Log("Thinking Carefully About Guess Number " + guessCount +", I Wonder, Is Your Number" +
								"\nEqual To Or Lower Than " + guess + "?");
						} else if (guess != max && guess != min){
							Debug.Log("Thinking Carefully About Guess Number " + guessCount + ", I Wonder, Is Your Number" +
								"\nEqual To, Higher Or Lower Than " + guess + "?");
						} else if (guess != max && guess == min){
							Debug.Log("Thinking Carefully About Guess Number " + guessCount + ", I Wonder, Is Your Number" +
								"\nEqual To Or Higher Than " + guess + "?");
						}
					}
				} if (guess == min && guess == max){
					Debug.Log("I Definitely Know What It Is Now!" +
						"\nIt Can't Be Anything But " + guess + "!");
					Debug.Log("That Was Way Too Easy! Give Me A Challenge Next Time!" +
						"\nPress Backspace To Play Again!");
					guessCount = 11;
				}
			}
		}
	}
	void GameOver (){
		if (Input.GetKeyDown(KeyCode.Backspace)){
			Debug.Log("==================================" +
				"\n==================================");
			Start();
		}
	}
	void GuessLower(){
		guessCount++;
		max = guess-1;
		guess = Random.Range(min,max+1);
	}
	void GuessHigher(){
		guessCount++;
		min = guess+1;
		guess = Random.Range(min,max+1);
	}
}

1 Like

Hi Derek,

Very nice, and well done for taking some extra steps with this section of the course and expanding on what was given - that is a fantastic way to learn!

Regarding your question, its not been covered yet, so it is a little bit unfair for me to perhaps mention it, but you could store the values for the alternating text in an array. You could then create a method to get the string to return in the Debug.Log statement, this method could either generate the random number and then use that to get the corresponding element from the array, or, you could pass the value into the method and create the random number outside of the method.

You could do something similar for the if statement logic where you effectively have the three different outcomes based on the value of guess compared to min and max. Even if that method was effectively generating the text, it would enable you to remove repetition in your code. To give you an example of how that might be handy, if you decided you wanted to change "Thinking Carefully About Guess Number " to "Thinking Cautiously About Guess Number " you could then change that text in one method rather than in multiple lines of code. (I fully appreciate you could use a Find & Replace within Visual Studio etc).

The above refinements are things you will learn as you progress through the course so perhaps not something to worry about now, but as you asked for some feedback I thought I’d suggest it.

Again, well done for taking the time to extend this version of the course game :slight_smile:

Thanks Rob, much appreciated!

Yeah, if we’re gonna cover it later I’ll wait until I get there and maybe refine the code then, I feel I might have went far enough ahead already this early in the course…

Cheers!

1 Like

In my opinion, you’ll get far more from the course and gain a much better understanding by doing exactly what you’ve started to do here. e.g. take the content from each section and then make a point of doing your own thing with it also. Some of the upcoming sections will make this even more straight forward as there will be some obvious features that could be added.

I look forward to seeing what you create next! :slight_smile:

I enjoyed your version of the number wizard program. :slight_smile:

1 Like

Privacy & Terms