My improved Number Wizard

Hi! I tried to improve the number wizard a bit and ended up with this one, hope you like it and have some feedback :smiley:

Final Version posted Below
http://gamebucket.io/game/45a45e6f-61be-4f60-9ae7-20a081f5f7c3

Added functions:
Keyboard/mouse support (not numpad).
Can make the computer guess any chosen number up to 10 digits.
Auto-adjusted max-guesses to try to make it fair.

Known bugs:
The ā€œQuitā€ button does not work in WebGL (not really a bug, quit never work in web builds) (Quit button removed)
There is a strange error where if ā€œReturnā€ is pressed to confirm ā€œcorrect numberā€, the push carries over to ā€œwinā€ and also activate the ā€œstart new gameā€ function. It is not replicable in editor and should be impossible given the ā€œGetKeyUpā€ condition, but it still does it in Web Build. If anyone have any tips I would welcome it!
(Problem fixed by switch and boolean)

Relevant code snippet:

void Update () {
	if (Input.GetKeyUp (KeyCode.Return) && Application.loadedLevelName == "Start") {
		Application.LoadLevel ("Game");	
	}
	else if (Input.GetKeyUp (KeyCode.Return) && Application.loadedLevelName == "Lose") {
		Application.LoadLevel ("Start");	
	}
	else if (Input.GetKeyUp (KeyCode.Return) && Application.loadedLevelName == "Win") {
		Application.LoadLevel ("Start");	
	}

	if (Input.GetKeyUp (KeyCode.Escape)) {
		Application.Quit();
	}

}

Hi @Goldragon,

Any reason for the use of the Input.GetKeyUp() rather than Input.GetKeyDown() method?

When I tested your game I noticed that I had to hold the Return key down for a little while for it to seemingly detect anything, I did see the Win screen, but only for a split second, then it takes you back to the main Start scene.

I would suggest what is happening is that the Input.GetKeyUp() is still returning true when the Win scene loads, and thus triggers your next else if statement, causing the Start scene to load.

Incidentally, I am using a laptop so my instinctive behaviour was to use the keypad, these numbers were detected when I tried typing in a value :slight_smile:

I changed from GetKeyDown to GetKeyUp specifically to avoid this problem. On ā€œgetKeyDownā€ I had the same problem we are experiencing here, but in the editor. Now that I have changed it, I do not get it in editor or pc version of the game, but I do experience it on the web build. I am a bit surprized really. If I had learned coroutines yet I could of course just put in a 0.5 second delay after each, but in that case I double back and do it later in the course when I have learned what I need :smiley:

Iā€™m not sure adding a delay is really the best way forward on this. As a player I would expect a response to be instant, regardless of input method, if nothing happens I may be tempted to press the key again, which may cause me to say an answer was higher/lower than it actually was.

I think getting to the bottom of the issue would be better, Iā€™ve not come across anyone else having this same issue.

It may make a difference if the logic were changed slightly, removing the logical && operator, e.g.

void Update() {

    if(Input.GetKeyUp(KeyCode.Return) {        // I would really suggest using GetKeyDown() instead here

        switch (Application.loadedLevelName.ToUpper()) {

            case "START":
                Application.LoadLevel("Game");
                break;

            case "LOSE":
                Application.LoadLevel("Start");
                break;

            case "WIN":
                Application.LoadLevel("Start");
                break;
        }

        default:
            Debug.Log("Scene: " + Application.loadedLevelName + " is not supported by method : Update()";
            break;
    }

    if(Input.GetKeyUp(KeyCode.Escape)) {        // again, I'd opt for GetKeyDown() here also
	Application.Quit();
}

}

Worked like clockwork in editor, will repost when I am done compiling so we can see if it works :slight_smile:

Thank you for help and feedback!

1 Like

No worriesā€¦ I think the && maybe your culpritā€¦ I can explain why if this is the case :slight_smile:

An unexpected result. Now it no longer skips from game to start. But it DOES jump over the ā€œenter numberā€ part of game (meaning it still presses two enters, just not the same place). Only on web version. I did replace GetKeyUp with GetKeyDown so that is not the culprit.

New Version of the game edited in to original post.

New Version of levelchanger Update:

void Update () {

	if(Input.GetKeyDown(KeyCode.Return)) {        // I would really suggest using GetKeyDown() instead here

	switch (Application.loadedLevelName.ToUpper()) {

	case "START":
		Application.LoadLevel("Game");
		break;

	case "LOSE":
		Application.LoadLevel("Start");
		break;

	case "WIN":
		Application.LoadLevel("Start");
		break;
	}

//	default:
//		Debug.Log("Scene: " + Application.loadedLevelName + " is not supported by method : Update()");
//		break;
		}

		if(Input.GetKeyDown(KeyCode.Escape)) {        // again, I'd opt for GetKeyDown() here also
			Application.Quit();
		}

	}

I could not edit the post because it was ā€œspamā€ (why? Are we not supposed to post projects here?) and I could not post a link to the new gamebucket page here because ā€œCannot post links in commentā€, so I dropped the new gamebucket link in headline of original post.

Itā€™s because you are a fairly new userā€¦ the forum has a trust level for users which grows over time, as such there are some thresholds which limit what new users can do to start with, specifically the posting of the same/similar links and images - I unflagged it as spam, the original post is back, and Iā€™ve popped the link out of the title as its in the post againā€¦ :slight_smile:

I think Iā€™ll need to see the rest of the code to really help, as the code that is posted doesnā€™t really cover the number entry system.

Updated Wed Nov 30 2016 11:11

On the very first screen where it says welcome and offers you the Start button, it didnā€™t matter how many times I pressed return, nothing happened. I clicked the mouse on the background and the button then lit up and I was taken into the game. I believe Ben mentions this issue in the lecture, with regards to the colour of the button text getting a bit ā€œstuckā€, I think this is a focusing issue within Unity. Iā€™m guessing you are running Unity 4.6.x as per the course instructions, later you move on to Unity 5, I suspect this issue may remove itself at that point. So, for now Iā€™m going to assume thatā€™s the case and just ā€œclickā€ on Start on the first screen and then see what happens after that.

Updated Wed Nov 30 2016 11:13

  • Clicked Start with the mouse
  • Typed in 300 with the main numeric character keys (I chose 225 in my head)
  • Pressed Enter
  • Game updated, told me there were 6 guessing remaining and guessed at 90
  • Pressed the Up arrow
  • Game updated, told me there were 5 guesses remaining and guessed at 219
  • Pressed the Up arrow
  • Game updated, told me there were 4 guesses remaining and guessed at 250
  • Pressed the Down arrow
  • Game updated, told me there were 3 guesses remaining and guessed at 238
  • Pressed the Down arrow
  • Game updated, told me there were 2 guesses remaining and guessed at 227
  • Pressed the Down arrow
  • Game updated, told me there was 1 guess remaining and guessed at 225 (the number I chose in my head)
  • Pressed Enter
  • Game updated, took me to the Win screen (e.g. it had won) and offered me the chance to try again
  • Pressed Enter
  • Game updated, took me to the Start screen
  • Pressed Enter
  • Game updated - BUG: It stated there were 2 guesses remaining and itā€™s guess was 1.
  • Pressed Enter
  • Game updated, took me to the Win screen (e.g. it had won) and offered me the chance to try again
  • Pressed Enter
  • Game updated, took me to the Start screen
  • Pressed Enter

It cycles after this point back to the line indicated BUG

You will need to ensure that you reset your number of allowed guesses and any other variables in use, Iā€™m guessing perhaps that they are set as Static?

Other than that, allowing for the potential Unity issue with the first button press, Iā€™d suggest this is fairly much working.

Updated Wed Nov 30 2016 11:26

This may be a potential fix for the issue you are experiencing with the button at the beginning, but Iā€™ve not tested it and it is somewhat out of the scope of the course material at this point in the course. Could be worth a few minutes to try it though.

http://answers.unity3d.com/questions/820599/simulate-button-presses-through-code-unity-46-gui.html

What you are wanting to do is effective trigger the Text button to think the mouse is over it, e.g. highlighted, then, when the user presses the Enter key, it will activate - I believe.

Alternatively, I think the move to Unity5 will resolve this anyway in due course.

Thank you for valuable feedback!

I uploaded a version now that seems to be working fine.

The bug, as far as I understand it, was that when ā€œReturnā€ was pressed once it triggered two stages of accepts. First it triggered (ā€œYes I want to start the gameā€) which changed a bool in my NumberWizard script to ā€œgameStarted = trueā€, then it also activated a (if (gameStarted) ā€œif you receive ā€œReturnā€ accept the number listed as the maximum range and start guessingā€).

I added two bools to make sure that the maximum range is not zero and that at least one guess has been done and now it works fine. (I realised as I write this that it is now impossible to use keyboard to confirm correct guess if the computer guesses the number on the first attempt so it was sort of a bad solution, but in that case people can use the mouse :P)

If you added a little code so that the range that people could enter had to be at least so big, you would reduce the probability of being given the number that they had chosen on the first guess, it would also prevent people from entering silly things like ā€œ1ā€ or ā€œ2ā€ā€¦ bit self defeating and pointlessā€¦ a minimum might be 50, or a 100 for example - but this is used as a minimum for the maximum if that makes any senseā€¦ e.g. you still run from 0 - valuePlayerEntered

Sounds like youā€™ve got it sorted to at least move forwards anyway, so well done - and youā€™re more than welcome for the feedback :slight_smile:

Privacy & Terms