We'll now create a guessing game, which will allow us to practice further on conditions. In this game we're playing against the computer, and more particularly we're trying to guess a number that it is currently thinking of. Let's create it. The first thing we need is a variable, so as to store the number that the computer is currently thinking of. Let's define the variable secret number. Note that if you uncheck it, its value is not shown in the execution area. What is the algorithm for a guessing game? We first need to specify the number that the computer is currently thinking of. So we'll set our secret number variable to any number from one to ten with the use of a pick random block from the Operators palette Then , we'll ask the player to enter a number he or she thinks the computer is thinking of and we'll keep asking until the player guesses correctly. In other words, until the user's answer is equal to the secret number. Whenever the user guesses incorrectly, we'll say that the guess was incorrect, and we'll ask again. When the player successfully guesses the secret number, we'll congratulate them. Good, now let's test our program. Instead of actually guessing the number for the test, I want to see what the secret number is and test how our program behaves for correct and incorrect user inputs. So I'll tick the secret number variable in the Data palette to make it visible on the screen.
Let's try it. I'll first guess incorrectly. Good, the computer asked me to try again. Let's go for the correct number now. Brilliant! Our guessing game is ready. Now, let's see how we can further enrich it. Instead of keeping the secret number variable visible on the screen all the time, we can include, in our program, instructions for hiding it by introducing a hide variable block at the very beginning of our program.
We an also specify that whenever the up arrow key is pressed, the secret number variable appears. While whenever the down arrow key is pressed it disappears.
How about giving the player some clues to help him or her win? For instance, by stating whether the number entered is greater or smaller than the secret number. We can do this by incorporating a decision inside our repeat until block. So we'll introduce an if then else block, which will check whether the user's answer is greater than the secret number. In which case it will let the user know about this so we'll use a say block for this. Note that the else part of our decision block corresponds to the case where the user's answer is smaller than the secret number. The case where the user has guessed correctly has already been checked at the condition within repeat until. We could also keep track of how long it takes to guess correctly. How can we implement this? We can do this with a use of the timer block from the Sensing palette. We can reset the timer at the beginning of the game, and at the end of a game in addition to congratulating the player we can also show this time. To do this, we'll use a say block, a timer block, and a join block from the Operator palette, which allows to join several elements, in this case some text like, "congratulations your time is", with the value of the timer. Now let's extend our game, so that it states in the end whether the player was slow or fast. Just like in a race, we need to stop the timer at the finish point and then, compare this value to a time that we think is reasonable, say 10 seconds. So we'll define a new variable called time, and we'll set it to zero at the beginning of the program. When the player guesses the secret number, we'll set this variable to the value of the timer, thus keeping track of the exact time it took the player to win. Next, we'll compare this time to 10 seconds with the use of an if, then, else block. And we'll tell the player whether he or she was slow or fast. Let's test our modified program to see if it works as expected. I'll press the up-arrow key to make the secret number visible so as to have better control over the testing.
I'll first go for a larger number. Good, I was correctly notified about this. Now let's go for a smaller number. Again, the message I see on the screen is as expected. Now, let's try guessing correctly to see if the computer will congratulate me. Excellent! I was also given my time which is indeed a bit slow hence the message for being slow. The final test case involves guessing correctly but fast. Let’s try it. Good stuff, we've now got a guessing game with interesting functionality. You can also try adding some visual effects to make it look a bit more professional.