I made a basic number guessing game in Unity as part of an ongoing course in Unity game development. You pick a number between 1 and 1000 and the computer will always guess it in 10 tries or less. This is because every time it guesses it discards half of the available options. After ten times it has gone over 2 to the 10th power options or 1024 options. This type of search algorithm is called a binary search algorithm (or binary chop).

You can see the game in action here:

You can find the code below:

**Console Version:
**This was the basic assignment. It does what it’s supposed to do, runs without UI in Unity’s console.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | using UnityEngine; using System.Collections; public class NumberWizards : MonoBehaviour { // Use this for initialization int max; int min; int guess; int counter; void Start () { StartGame (); } void StartGame () { max = 1000; min = 1; guess = 500; counter = 1; max = max+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-1)); print("The lowest number you can pick is " +min); print("Instructions:"); print("I will try to guess your number."); print(" > Press up if it is higher than my guess."); print(" > Press down if it is lower than my guess."); print(" > Press enter if it is equal to my guess."); print("Is the number " +guess +"?" +" I have guessed " +counter +" times so far."); } void NextGuess (){ guess = (max+min)/2; counter = counter+1; print("Is the number " +guess +"?" +" I have guessed " +counter +" times so far."); } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.UpArrow)) { print("The number is higher than " +guess +"."); min = guess; NextGuess (); } else if (Input.GetKeyDown(KeyCode.DownArrow)) { print("The number is lower than " +guess +"."); max = guess; NextGuess (); } else if (Input.GetKeyDown(KeyCode.Return)) { print("Your number is " +guess +"!"); print ("I found your number in " +counter +" guesses."); } else if (Input.GetKeyDown(KeyCode.KeypadEnter)) { print("Your number is " +guess +"!"); print ("I found your number in " +counter +" guesses."); } } } |

**UI Version:
**This is the Number Wizard featured in the video above. I’m trying some things which haven’t been covered in the course yet, so if you spot some inelegant code (there’s bound to be plenty), feel free to let me know

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | using UnityEngine; using UnityEngine.UI; using System.Collections; public class TextController : MonoBehaviour { public Text text; // Use this for initialization int max; int min; int guess; int counter; void Start () { StartCoroutine(Intro()); } IEnumerator Intro() { max = 1000; min = 1; guess = 500; counter = 1; max = max+1; text.text = "Welcome."; yield return new WaitForSeconds(3); text.text = "Welcome. \n Pick a number in your head, \n but don't tell me what it is."; yield return new WaitForSeconds(3); text.text = "Welcome. \n Pick a number in your head, \n but don't tell me what it is. \n The highest number you can pick is " +(max-1) +"."; yield return new WaitForSeconds(3); text.text = "Welcome. \n Pick a number in your head, \n but don't tell me what it is. \n The highest number you can pick is " +(max-1) +"." +"\n The lowest number you can pick is " +min +"."; yield return new WaitForSeconds(3); text.text = "Welcome. \n Pick a number in your head, \n but don't tell me what it is. \n The highest number you can pick is " +(max-1) +"." +"\n The lowest number you can pick is " +min +"." +"\n You will now get 10 seconds \n to think of a number."; yield return new WaitForSeconds(3); text.text = "\n \n \n 10"; yield return new WaitForSeconds(1); text.text = "\n \n \n 9"; yield return new WaitForSeconds(1); text.text = "\n \n \n 8"; yield return new WaitForSeconds(1); text.text = "\n \n \n 7"; yield return new WaitForSeconds(1); text.text = "\n \n \n 6"; yield return new WaitForSeconds(1); text.text = "\n \n \n 5"; yield return new WaitForSeconds(1); text.text = "\n \n \n 4"; yield return new WaitForSeconds(1); text.text = "\n \n \n 3"; yield return new WaitForSeconds(1); text.text = "\n \n \n 2"; yield return new WaitForSeconds(1); text.text = "\n \n \n 1"; yield return new WaitForSeconds(1); text.text = "\n \n \n Ready?"; yield return new WaitForSeconds(2); text.text = "\n \n \n I will now try to guess your number."; yield return new WaitForSeconds(3); text.text = "Is the number " +guess +"?" +"\n(I have guessed " +counter +" time so far.)\n\n > Press the up arrow if \n it is higher than my guess. \n > Press the down arrow if \n it is lower than my guess. \n > Press enter if I guessed correctly."; } void NextGuess (){ guess = (max+min)/2; counter = counter+1; text.text = "\n \n \n Is the number " +guess +"?" +"\n(I have guessed " +counter +" times so far.)"; } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.UpArrow)) { text.text = "The number is higher than " +guess +"."; min = guess; NextGuess (); } else if (Input.GetKeyDown(KeyCode.DownArrow)) { text.text = "The number is lower than " +guess +"."; max = guess; NextGuess (); } else if (Input.GetKeyDown(KeyCode.Return)) { text.text = "\n \n \n Your number is " +guess +"! \n (I found your number in " +counter +" guesses.)"; } else if (Input.GetKeyDown(KeyCode.KeypadEnter)) { text.text = "\n \n \n Your number is " +guess +"! \n (I found your number in " +counter +" guesses.)"; } } } |