Let’s Learn Unity – Number Wizard UI

Let’s Learn Unity – Number Wizard UI

For information and navigation, check out my post that introduces this Unity series here.

TL;DR? Skip to the game.


 

This Week

After the first introductory section of the Udemy Course, Ben Tristem and the GameDev.tv team take us through a basic scripting algorithm. Later on in the course, this script is then reused to show how to add a User Interface (UI). As I have already done a fair bit of coding, and to make sure that I pack all the juicy learning  I can into each and every post, I’ve decided to cover both of these sections simultaneously.

Now, it’s time to meet the Number Wizard!

After you pick a number between 1 and 1000, the mighty Number Wizard will attempt to find your number in as few guesses as possible. In fact, the wizard is able to eliminate half the remaining numbers with each guess. This is achieved by using a Binary Search.

 

Binary Search

But just what is a binary search? Well, I’m glad you asked.

Imagine you are in a library and you really want the latest book by an author called Angela Appleson. You would probably start at AA and work your way along until you get to the book. That might work rather well in this case, but what if the author’s name was Wilson Wheelbarrow (I’m not the best at coming up with names). Now you would have to go through almost the entire library. You could instead start at ZZ and work your way up, but there is a way to ensure you do as little searching as possible, regardless of the author’s name. This is where the binary search comes in.

A binary search begins in the middle, around M perhaps. If the book you are after is before M, then you can immediately eliminate the lower section of the library, if it is after then you do the opposite. You then repeat this process, eliminating half the remaining books each time. By doing this, you could search 1,024 books in a maximum of 11 guesses (see below).

  1. 1,024
  2. 512
  3. 256
  4. 128
  5. 64
  6. 32
  7. 16
  8. 8
  9. 4
  10. 2
  11. 1

While you might not be able to tell exactly where the middle is, you should be able to make a fairly accurate guess.

 

Demonstrations

I have made a video of a Binary Search using a pack of cards. I also do a bit of my own numerical wizardry at the end. You can see it below.

 

Hackerrank has also made a video on Binary Searches (not just for this blog post though). It goes into more detail than my video and shows you how to code one in java. It also goes over the Big O notation that I talk about in the next section.

I’m including both videos so you can choose the one you prefer or watch them both. Go nuts!

 

Let’s get technical!

Finally, as you need to double the number of elements you are searching to add another step in the search, we can say that the complexity of a Binary Search is log(n). Or, O(log n) in Big O Notation. There is a graph below to demonstrate what this looks like.

In the graph, the y-axis represents the maximum number of steps needed to perform the search and the x-axis shows the number of items to be searched.

It's an exciting graph of log(n).
The efficiency of log(n).

 

 

My Game

With most of the games in this series, I will be doing my best to add a little bit of panache. However, the simplicity of this first game didn’t leave much room for that. Still, I did my best and I hope you enjoy it.

You can see the Number Wizard’s awe-inspiring usage of the binary search by clicking here.

 

Challenges

Easy: If you’d like to get a better idea of just how efficient a Binary Search is, why not work out the maximum number of steps are required to search each of the following things:

  1. A pile of 3 books.
  2. A pile of 4 books.
  3. A classroom of 16 children.
  4. A classroom of 15 children.
  5. A 235-page journal.

 

Hard: While the Number Wizard works wizardly magic with numbers, I’ve thought of an even more impressive trick! I’ve set up the start of a program (in C#) for searching through a pack of cards. Anyone who would like a challenge can try to finish it off.

Download Zipped Unity Version Here

Download Zipped Text Files Here

If you manage to complete the card search or have any challenges of your own that you would like to suggest, then please let me know either in the comments below or via Twitter @TheLearnJourn.

3 thoughts on “Let’s Learn Unity – Number Wizard UI

  1. I have noticed you don’t monetize your page, don’t waste your traffic, you can earn additional cash every month
    because you’ve got hi quality content. If you want to know how to make extra bucks, search
    for: Boorfe’s tips best adsense alternative

  2. Have yoս eveг thought about including a little bіt more than just
    your articles? I mean, what you say is fundamental and all.
    However think of if you аdⅾed ѕome great graphics or
    ѵideos tο give your pοsts more, “pop”!
    Your cοntent is excellent but with images and videos, this website coսld undeniably be one of the very best in its
    field. Awesome blog!

    1. Hello, I’m sorry for not replying sooner, your comment got caught in my spam filter for some reason.
      Thank you very much for the encouragement, it’s always nice to get some feedback, especially in the early days.

      I did my best to add some more interesting elements but the Number Wizard section was a bit bare bones, so I struggled a bit. I have now added a bit more razzmatazz as I’d come to the same conclusion about the post.

      I hope you continue to enjoy my blog.

Leave a Reply

Your email address will not be published. Required fields are marked *