Who Am I? —Creating a Simple Guessing Game

What Number am I Thinking of?

Everyday, people guess. They estimate or guess how long it’s going to take to get to work or how the cup of coffee will taste like, or even guess the numbers to the winning lottery. Why do people guess? It’s because there is probability to everything in life — even if an event is certain or uncertain to happen, there is some kind of randomness involved.

Today, we’re going to create “Who am I?”, a simple guessing game example provided by Sketchware. In this project, the computer generates a random number between 0 and 9, and the user has to guess the correct number.

What you’ll need

  1. Sketchware Application from Google Play

What you’ll learn

  1. Concept of Random and Comparison

Step 1: Starting the project

Start the project “Who am I?” from the example list.

Step 2: Designing the Application

We’ll leave this part up to you. If you have completed the previous tutorials, now you should be able to design the View without an issue. Here is the Hint View to help you get going. Remember — you can edit the View however you like!

Step 3: Setting up the logic

The computer generates a random number between 0 and 9, and the user has to guess the correct number.

That is the core of the logic we have to create for this application. By observing the demo above, we can see how the application is working:

  1. The computer generates a random number between 0 and 9 when START is clicked, and it’s shown as “?”
  2. The user can only guess when START button is pressed. Until the START button is pressed, GUESS and STOP buttons are disabled.
  3. The user can GUESS or click on STOP to reveal the number.
  4. When the user makes a guess, a message is toasted, guiding the user to either guess higher or lower.
  5. When the correct number is guessed, the number is revealed and you can start over.

From the observation above, let’s divide the logic into four events: onCreate, Guess, Stop, and Start.

onCreate

By looking at the demo above, we can see that the GUESS, STOP buttons and the input are disabled before START button is clicked. onCreate is a good place to disable those events from happening.

Button: start onClick

Let’s think what is going to happen when start is clicked. We first want GUESS and STOP buttons to be enabled, as well as the text input. Also, we want to hide the answer by setting it to “?”. Then, we want to generate a random number by using the pick random block.

Button: stop onClick

Before we do guess onClick event, let’s finish STOP. When stop is pressed, we want to reveal the answer, as well as disabling the guess button and input. We also want to empty the input to reset to how the application was like when it started.

Button: guess onClick

Now this is the fun part! Let’s recap what we have to achieve.

First, there must be an input before there can be a guess — the user can’t really just press guess when there is no nothing in the input right? Error handling is just as important as programming the application. Without handling or preventing potential errors, the application may crash.

Next, since all text inputs are considered as String values, we have to turn the String into a Number before we can actually make comparisons. After turning the String into a Number, we look at three cases:

1. The guess equals the number. Hooray! We can reveal the correct number, show the message that the guess was correct, and reset the application by disabling the guess, stop, and input.
2. If the guess is higher the number, we simply show the message that the guess must be lower, and vice versa — which makes it three cases.

By following these rules, we can create a logic like this:

Click on RUN, and see how your application goes!

Conclusion and Challenges

We completed the “Who am I?” application, and learned programming concepts like Random and Comparison. These can come in handy when you want to create an application that involves a random chance. There are some challenges you can add to this application:

  1. Create a More Block that disables guess, stop, and input, so the code looks more clean.
  2. Let the user know how many guesses it took to guess the right number!

That’s it! Hope you guys enjoyed and happy coding! :-)