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:
- 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)
- There’s also a guess count to automatically end the game if the computer cannot guess the number after a certain amount of guesses
- The guesses themselves are randomly assigned between the min and max
- 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)
- 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)
- There are line-breaks in the text so the console never shows the words “UnityEngine.Debug:Log(Object)”
- 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);
}
}