Text Based Games

1 Rock, Paper, Scissors

Learn It

  • Rock, Paper, Scissors is a simple enough game to create in Python, although it's a little difficult to tell if the computer is cheating or not, as the player always goes first.
  • In the real game, each player (on the count of three) calls out either Rock, Paper or Scissors (with the appropriate hand gesture).
  • The rules are:
    1. Rock crushes Scissors.
    2. Scissors cuts Paper
    3. Paper wraps Rock

Learn It

  • We're going to need to use a couple of data structures to accomplish this game.
  • The first are lists.
  • In Python a list is an ordered collection of elements. They can be integers, floats, strings, variables or even other lists.
  • We create a list, by giving it a name and then placing our elements inside of square brackets [ ] with commas separating each element.

Code It

  • Let's create a list and see what we can do with it.
  • Write the following into your INTERPRETER
people = ['Niall','Zayn','Liam','Harry','Louis']
  • We can access any element of the list, using it's index.
  • Try typing the following.
people[0]
people[3]
people[-1]
people[5]
  • Pay careful attention to the error you received in the last example. This is one you're sure to meet again, and means you've tried to access an element that is beyond the range of the list.

Learn It

  • If we want the computer to pick a random name from our group of people, we're going to need an additional library.
  • The random module allows us to use random number generators.

Code It

  • Type the following into your INTERPRETER
import random
  • Now try typing the following (you might have to put them in more than once, to see what is happening)
random.random()
random.randrange(0,10)
random.randrange(0,1)
random.randrange(0,2)
random.randrange(0,10,2)

Code It

  • So using what we have now learned, we could write some code to choose a random person from our list.
people = ['Niall','Zayn','Liam','Harry','Louis']
randomNumber = random.randrange(0,5)
randomPerson = people[randomNumber]
print(randomPerson)
  • We could condense this down a little though.
people = ['Niall','Zayn','Liam','Harry','Louis']
randomPerson = people[random.randrange(0,5)]
print(randomPerson)
  • And there's an even easier way of choosing a random element from a list, if you'd like to go and try and discover it on the web.

Badge It - Silver

  • Using what you have learned so far.
    1. Create a list called choices that contains the words - rock, paper and scissors.
    2. Set a variable called computerChoice that gets set to a random element from the list.
    3. Set a variable called playerChoice that lets the user choose from rock, paper or scissors, by asking for some input
    4. Print out the computer and player's choice on a single line (remember string formatting from last lesson (%s))

Learn It

  • Next we need a way for the computer to understand the rules of Rock, Paper, Scissors.
  • For this we'll need a knew data structure called a dictionary.
  • A dictionary is a collection of key and value pairs.
  • Dictionaries are surrounded by curly brackets { }
  • The easiest way for you to understand dictionaries, is to create one.

Code It

  • Let's use the people from the last section and try to classify them.
people = {'Harry':'Cute','Zayne':'Mysterious','Liam':'Sensible','Harry':'Charming','Louis':'Funny'}
  • Each of the names in the dictionary is known as a key, while each of the descriptions are known as values.
  • We can query the dictionary and get returned the value of any given key.
people['Liam']
people['Louis']
people['Zayne']=='Mysterious'
people['Harry']=='Funny'

Code It

  • We can now use the dictionary to create a little guessing game.
  • Copy and paste the code below into a new file and then run it.
import random
people = ['Niall','Zayn','Liam','Harry','Louis']
descriptions = {'Harry':'Cute','Zayn':'Mysterious','Liam':'Sensible','Niall':'Charming','Louis':'Funny'}
person = random.choice(people)
guess = input('Guess what %s is described as '%(person))
if descriptions[person] == guess:
    print('You are correct')
else:
    print('You are wrong')
  • Once you've played a couple of times, make sure you have a good look at the code and that you understand what is going on.

Code It

  • Let's use a dictionary to build up the rules of Rock, Paper, Scissors.
  • Create a dictionary where the keys are 'rock', 'paper' and 'scissors', and the values are the item that each one beats.
  • For instance:
'rock':'scissors'

Badge It - Gold

  • We can now add a few more lines to the code, to decide who wins.
  • You should know enough about conditional selection to do this yourself (if, elif and else)
    1. if the playerChoice and computerChoice are equal, then the program should print 'Draw'
    2. if the playerChoice is looked up in the dictionary, and its value is equal to the computerChoice, then the program should print 'You win'
    3. Otherwise the program should print 'I win'

Code It

  • Dictionaries can contain more than just strings.
  • Here we have a dictionary that contains lists.
compounds = {'Water':['H','O'],'Carbon dioxide':['C','O'],'Ammonia':['N','H'],'Salt':['Na','Cl']}
  • We can use this to query which elements are in any given compound.
compounds['Water']
'C' in compounds['Carbon dioxide']
'Na' in compounds['Ammonia']

Badge It - Platinum

  • Watch this video carefully.
  • It might be useful to have a pen and paper ready - to write down the rules
  • Using what you know about dictionaries of lists, alter your code so that you can play Rock, Paper, Scissors, Lizard, Spock with the computer.