Finished This With All The Extra Credit Things

Hi all. Let me know if there’s any way to reduce my code. I implemented picking a number range. Q picks 1 to 100, E picks 1 to 1000, etc. There is a guess counter. It specifies between “Iv’e guessed 1 time.” and “I’ve guessed to times.” (the ‘s’ gets added to ‘time’ after the first guess). I also have the computer picking a random number within the current range. I’m gonna post my code, but try to use your own logic to set things up. The only thing the lesson didn’t teach us is Random.Range(1stNumber , 2ndNumber). I think it just calls a random number between 1stNumber and 2ndNumber, then sets the new min/max. I’m not quite sure what bool does yet. The coding is attached. Lemme know what you think?

Unity Version: 5.6.3f1

Number Wizard Coding

Hi Brady,
There is one major reduction you can do.

if (tries == 1) {
           print("I've guessed 1 time.");
       }
       else if (tries <= 10) {
           print("Iv'e guessed " + tries + " times.");
       }
       else if (tries >= 10) {
           print("What's the stupid number?");
           print("Iv'e guessed " + tries + " times.");
       }

Instead of this if-else loop, you could just simply display your number of tries after you have guessed the number. Like here,

 else if (Input.GetKeyUp(KeyCode.Return)){
                print("I won!");
                print ("Guesses: " + (tries+1) + "!");
                StartGame();

This would save you from using the if-else code above with the same desired result.

Also, you are right about the Random.Range(min,max) keyword. It specifies the range from min to max and chooses a random number from that range.

Bool only has two conditions (true and false or 1/0), so it works just like a switch. For example, (in your code) it was used like a switch to detect whether the range was chosen by the end user.

For more info about these keywords and functions, browse
https://docs.unity3d.com/ScriptReference/index.html

Cheers & Good Luck,
YA

The bool is boolean variable type where you can assign either true or false to that variable.

Privacy & Terms