Improving Upon Number Wizard

So, while working on my own projects at my own pace, I am also learning through a course on Udemy about the ends and outs of using Unity to develop games.  I recently just finished the section where we took a previous game we made in console only and gave it a User Interface.  That game was Number Wizard.

Number Wizard is a simple game.  You think of a number between 1 and 1000 and the computer gets 12 tries to guess it.  If it doesn’t guess it you win.

While the tutorial taught me a few things, like how to connect buttons to scripts and how to build scenes.  There was one thing I felt like they neglected on what would be a ‘final product’ of the game.  So I changed something and added something to the game and learned a lot in the process.

One of the major “bugs” of the game is that the way it was set up.  If your number was ‘500’ the computer would sometimes get stuck between 499 and 501.

“So your number is 499? No? 501 then? No? 499! No? Bah I give up, it’s impossible”.

Why did it do this?  Well it’s last guess becomes either you new maximum or minimum, and since it is picking a random number, including the max and min, it could randomly just go back and forth between the two.  So it wouldn’t always get stuck, but it would take it a few times bouncing around before it found your number, or sometimes it just wouldn’t find it at all.

So how did I fix it?  Well, before the old guess would become the new max or min, but we know we can rule out the old guess since the player did not say it was their number.  So if their number is higher, the min now becomes the old guess +1 and if the number is lower the max becomes the old guess -1. So now when the random number goes, it doesn’t include the old guess, it’s not an option.

This then lead to what I added to the game!  A tiny bit of AI (that’s artificial intelligence).  AI doesn’t have to be super complicated, and in fact this bit of AI is so simple, I only just realized that what I created was indeed AI.  Under certain circumstances the computer can tell you it’s got your number already or if you’ve made a mistake.

When I started thinking about using  the + and – 1 to the guess in order to rule out old numbers, I thought “What if min and max were equal, that would mean that number would have to be the correct number, the computer would automatically win.”  So I set that up.  If when you press “higher” or “lower” the max and min are equal.  You get taken to a different screen where the computer tells you that it figured it out already.

However, what if the user made a mistake.  They pressed up when they meant to press down.  What if they cheat and change their number in the middle of the game or they simply forgot their number.  Number Wizard can take care of that too.  If you tell Number Wizard to go higher or lower when you cant he will tell you that you made a mistake and claim victory for himself.

Making the “alternate computer win” screen was a challenge on it’s own.  Apparently in Unity your scripts and things get destroyed when you change scenes.  So I had to learn how to persist the Number Wizard script into the next scene to say that “this number is indeed your number”.  Also then using that script to display the number in a new scene other than the one it came from took some time to learn as well.

Overall I am happy with this game (mechanically speaking).  I took what they gave me for the lesson, refined it and added some stuff of my own.  I also learned a few new things that will help in my other projects for sure! When I get a site to put my games up on I will link it here.  Until next time!

Buttons Simulator 2016

As you may or may not know from my WIXOSS blog entry, currently the only thing working about that game is the menu (and even then really just one button does anything, although it does play music!).  However, I recently discovered that while my ‘settings’ button was taking me to the ‘settings scene’.  It was more complicated than it should have been.  Thanks to “Learn How to Code By Making Games – The Complete Unity Developer” course on Udemy.  I was able to learn a much easier process of doing it.  In other words, Button Simulator got a major update!!!!!

In Unity each level is called a scene.  So if I want to go from my start menu scene to my setting menu scene I have to tell the button which scene or ‘level’ I want it to go to.

Originally I wrote a script that handled the transitions, it looked something like this.

public void start_menu(){

Application.LoadLevel(1);

}

public void settings_menu(){

Application.LoadLevel(4);

}

So in that example using start_menu() will load the scene labeled as 1 in the build, which is the start scene.  Now there are a lot of things wrong with this.  For one, I would have to write a line of code for each single scene, as you see with my settings_menu().  Then I have to keep track of which number it is in the build.  What if I change a number of something?  Then everything breaks. Bad!  Bad programming!

So how did I learn to do it the right way (or atleast in a better way)?  Well, first off I created a new game object called LevelManager.  Then I gave that object my script as a component.  Then I got rid of all the previous code I had written and wrote this one line.

public void loadLevel(string name){

Application.LoadLevel(name);

}

That one line does the work of ALL the other lines I had and would have had in the future for changing levels.  It’s so simple!  The LoadLevel() function apparently looks in the build for either the number or the name.  So when you connect your button to the LevelManager object we created and specify loadLevel(string name) as the function to run onClick() it will give us a place to input a string.  That is where we put the name of the scene we want to go to!

loadlevel

So on the settings button on the start menu we put settings_menu as the name and ta-da! It goes to that scene.  This is great because if we know what button we are working on, it is easy to keep track of which scene we want to go to. If we need to change the build order, it will just search for the names, so it doesn’t matter.

So WIXOSS got an update today.  Buttons should now respond better, or at the very least, more efficiently.  (Really it’s just good design, probably didn’t have an effect on game speed, but I like to think it’s a big change!)  Just a really good trick, I am glad I learned!