Page 29
3: Guessing Game
This lesson will introduce
Breaking down a problem into sub- problems
Building your own blocks (procedures)
Nesting (decision statements)
Random numbers
Input validation
Introduction
You are going to create a simple game where the user guesses a number chosen at random by the computer. The user keeps guessing until they get the correct answer.
Task 1: Designing the solution
Let’s look at the main things we need to code in our game: 1. choose the random number
2. get the user’s guess 3. check if correct
4. steps 2 and 3 should repeat until the user guesses correctly An algorithm for doing this is shown below.
when the flag is clicked
choose random number (1-10)
repeat until user’s guess = number
take in user’s guess check if guess is correct
Just like the previous lesson, the algorithm above gives a clear idea of what our code will have to do. However, we don’t have simple commands in BYOB to “choose random number”, “take in user’s guess” or “check if guess is correct” (shown in bold).
Page 30
Our program also needs to hold information while it is running:
the random number (number)
the user’s guess (guess)
so we will need to use variables for these.
Algorithm Code
Choose random number
set number variable to a random number between 1 and 10
set guess variable to 0
give user message about the program
Code this one yourself!
Take in user’s guess
ask user to guess the number (1-10) and wait for answer
set guess variable to user’s answer
Code this one yourself! Check user’s guess
Write this one yourself! Code this one yourself!
Task 2
Watch screencast GuessingGame to learn how to create the game.
After you have watched the screencast, create the game using your own code.
Itching for More 3: Guessing Game
Page 31
Procedures: Building Your Own Blocks (BYOB)
In this program, we saw how lines of code can be grouped together into a single block (also called a procedure). Creating a procedure is like creating a new command in your programming language.
Procedures let us:
● break down a problem into smaller problems and solve each of those separately. We can then concentrate on just one small “sub-program” at a time.
● create a single piece of code that we can use (or call) as often as we need to within a program. This saves us “reinventing the wheel” by entering the same code lots of times.
As a general rule, whenever you have a clear “sub-task” in your program, you should create a procedure to do this. It will make your life easier!
Task 3: Count guesses
Adapt your program so that every time you take in the user’s guess, it counts the user’s guesses.
Hint: Store this in a variable called attempts and display it on the screen (by ticking the name of the variable in the Variables section).
Page 32
Task 4: Hint, hint
When checking the user’s guess, tell them if their guess was too low or too high.
Hint: adapt your CheckGuess procedure so that it works in the following way: If guess = number
Say “Well done! You guessed it”
Else ... so guess is not equal to number
If guess < number ... notice the nested If (one If inside another) Say “Too low!”
Else ... so guess must be greater than number Say “Too high”
Now try to enter a guess that is below 1 or above 10. What does your program do? _______________________________________________________________________ _______________________________________________________________________ What should your program do? ______________________________________________ _______________________________________________________________________
Itching for More 3: Guessing Game
Page 33
Validating Input
Whenever we get input in a program we should always check that it is valid – allowable or reasonable – before we process it.
If an input is invalid, we should:
● tell the user they have entered an invalid value ● tell them what the valid values are
● ask them to re-enter their input
Extension 1: Validate
The program should not progress until the user enters a valid value for their guess. Which procedure will you need to change? ____________________________________ Check your answer with your teacher then make the change.
Hint: when taking in the user’s guess: repeat until guess>0 and guess <11
Extension 2: A fair attempt
Your program should add to the user’s attempts only if they enter a valid guess.
Which procedure will you need to change? ____________________________________ Check your answer with your teacher then make the change.
Page 34
Did you understand (part 1)?
3.1 A programmer creates a guessing game like the one in this lesson, but uses variables for the lower and upper limits of the range of numbers.
So, instead of the Choose Number procedure starting with:
it starts with: and the values of lower
and upper are set at the beginning of the program:
Although this seems like extra work, why is it a good idea?
__________________________________________________________________ __________________________________________________________________
Now make this change to your own program.
Extension 3: (Not) much harder
Now adapt your program so that the user has to guess a number between 1 and 1000!
Hint: Your program should also validate the user’s input to ensure it is in the correct range before continuing. Remember to check for this when testing, too!
How does using variables for the lower and upper limits of the range make this easier? _______________________________________________________________________ _______________________________________________________________________
Itching for More 3: Guessing Game
Page 35
Did you understand (part 2)?
3.2 Write down the range of valid values for the following:
a) someone’s age _________________________________________________ b) the current day of the month _____________________________________ c) gender _______________________________________________________ d) someone’s title (or salutation) ____________________________________ e) The answer to the question:
“Do you want to continue? (Yes/No)” _______________________________
3.3 What is the maximum number of guesses a skilled user should take to guess the correct answer when guessing a number between:
a) 1 and 10 ______________________________________________________ b) 1 and 100 _____________________________________________________ c) 1 and 1000 ____________________________________________________ d) Explain how you got the answers above.
_____________________________________________________________ _____________________________________________________________ _____________________________________________________________
Page 36
3.4 Discuss the following examples from real life. What “procedures” could they be broken down into?
a) Cooking a meal _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ b) Building a house _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________ _____________________________________________________________
3.5 Research a classic video game (e.g. Pong, Breakout or even a newer one like Angry Birds).
Discuss with your partner some examples of “sub-tasks” within your chosen game that could be coded as procedures.
Write down the “sub tasks” in the space below.
__________________________________________________________________ __________________________________________________________________ __________________________________________________________________ __________________________________________________________________
Itching for More 3: Guessing Game
Page 37
3.6 In this lesson, we validated input from the user – that is, made sure that it was an allowable value for the input.
Look at the code below that takes in a percentage as a whole number from the user. If the value entered is not valid, it displays an error and asks for the percentage again. This is repeated until the input is valid.
Which of the following repeat until blocks would ensure the user entered a valid percentage? Write the letters of the correct statements below the table.
A B
C
D
Correct pieces of code (letters): ________________________________________
Will the correct code work if the user enters a valid percentage the first time? Explain your answer.
__________________________________________________________________ __________________________________________________________________
Page 38