In groups, think of some of your favourite tabletop or video games, and complete a table like the one below by answering these questions:
The first few rows are completed as examples.
Francois Philipp/flickr, Creative Commons BY 2.0
|Game||Random element(s)||Mechanism to generate randomness|
|Monopoly||Which square the player lands on||Dice roll|
|Chance cards||Shuffled cards|
|Chess||No random element (except for starting player)|
|Matching puzzle phone game||Order of arrival of pieces to match||Game code chooses piece types|
|First person shooter game||Spawn positions of players||Game code chooses locations|
|Locations and types of upgrades||Game code chooses locations and types|
|AI of enemies||Game code chooses behaviours with a mix of strategy and randomness|
You may want to take this one step further in a class discussion:
Q. A computer often makes thousands of ‘random’ choices when you play an electronic game. How does it choose a random puzzle piece, for example?
A. When a program seems to choose a random puzzle piece, it’s because each piece type has been assigned a number. This is like rolling a die to select which piece type will appear next.
Q. But there’s no dice inside a computer. Can it really choose random numbers?
A. For a computer to come up with a truly random number, it must gather complex data from the world outside, such as fan noise. Computers can also make ‘pseudorandom’ numbers, which can appear to be random but are generated in sequence by an algorithm and a seed value. How random numbers are generated is quite a controversial topic, because it applies to encryption for security.
In this lesson, students will:
Begin by watching the overview video below:
As a class, or in teams, discuss what the Heads or tails program will do, then design the program as a flowchart. Effective designs may vary, for example, the program might toss the coin first, then ask the user for their guess.
Here is one solution:
Image: Flowchart for Heads or tails game
Once the flowchart is complete, write the program in pseudocode (structured English).
BEGIN Display “Pick heads or tails:” guess ← input from user computerPick ← random choice between ‘heads’ or ‘tails’ If guess = computerPick then Display “You guessed it.” Else Display “Better luck next time.” End if END
For more on setting up and choosing a language, see Setting Up.
Now that you can toss a coin, the video below challenges you to code a 6-sided dice roll.
Try it yourself before finishing the video or checking the solution code below. You may want to design your program with a flowchart or pseudocode first.
These challenges use the skills covered so far. By writing or modifying their own programs, students have an opportunity to demonstrate Application and Creation.
How about a program to help you pick who gets to go first when playing a board game? The program should randomly pick one name out of five names.
Note: You don’t need to ask the user to enter all the names. They can be built into the program (or ‘hard-coded’).
Challenge early finishers to improve the program. Now it will ask the user to enter the five names at the start, store each one in a variable, then randomly choose one of them to display.
Enter your name: Bill Enter your favourite place: Paris Enter your favourite sport: soccer Enter an exclamation: Ouch! Enter an adjective: hairy Bill was playing soccer in Paris when a hairy bicycle crashed into him. ‘Ouch!’ said Bill.
By asking for more than one place, or more than one sport, you can make the story more random by choosing just one response.
(Optional) Armed with the skills to use branching (from Lesson 2), you can write a choose your own adventure story. By adding variables, you’re already making a smarter story than is possible using slide presentation software. Maybe you can even add a random element to your game.