Paper scissors stone
Introductory tasks
How do you play 'Paper scissors stone'. Discuss the rules then students should play the game for a minute or two with a neighbour, to ensure everyone understands the rules.
The problem
Explain that the students have to make design and program the game in Python. The game will be played by one player against the computer. The first to three victories wins the game. After each individual game, the computer should display the computer's selection, the person's selection and who won. The person should then automatically start the next game, until either the person or the computer reaches three victories. The program should keep the following statistics about the games: the total number of games played, how many games were drawn, how many were won by the computer and how many were won by the person. These should be displayed at the end of the game.
Overview of our solution
This is a nice problem to solve because weaker students can simply use lots of if statements to test the different permutations to see if there is a draw or a winner. Better students can use a dictionary data structure. This will store the fact that a rock beats scissors, scissors beats paper and paper beats rock. By using a dictionary, we can reduce the number of if clauses to just three. The solution we will use uses global variables, a while loop and splits up the problem into functions.
We need to import the random module.
Set some global variables.
Have a welcome screen with some outline rules.
Randomly pick a selection for the computer.
Enter a selection for the person.
Play the game, updating the statisitcs and returning the result.
Keep playing until either the computer or person reaches three victories.
Print out the final statisitics and overall result.
Pseudo-code
import random
initialise global variables
function for computer's selection:
return a random value from a list
function for person:
return the person's selection
function play the game:
make variables global
increment games played
define the dictionary showing what beats what
compare the computer's choice to the person's, update the stats and return the result
function welcome:
display a welcome screen
function main:
declare variables as global
while (there isn't a winner):
get a computer selection
get a person's selection
play the game
Display the results
Call the welcome screen
Call main()
Solution
Extension tasks
There are some easily implemented extensions that can be set for students when they finish. The first one is to rewrite the code using a dictionary, if they haven't already. They could then ask the the user to input their name at the beginning of a set of games and then use and display that at all times. Dealing with users not entering in their selection correctly should be looked into e.g. what happens if the user uses all little letters or all capital letters, and how could they deal with that? What about entering in non-string values? Some students could use Try and build exceptions into their code. Really good students could write the results to a file, and then build up a file over time. They could then provide the user with a function to print out the statistics for matches played over time.