Text Based Games

1 NIM

Learn It

  • Nim was one of the earliest Computer Games ever created.
  • In fact, a special computer called the NIMROD was created for the sole purpose of playing the game.
  • There are numerous variants of the game, so we'll start off with one of the simplest - The 100 game
  • Have a read of the rules, so you understand the game

Code It

  • Let's start with some basics. Create a new Python file called The-100-Game.py
  • If we're going to have a two player game, we'd best let our players state their names and save these values using variables.
playerOne = input('What is the name of Player One? ')
playerTwo = input('What is the name of Player Two? ')

Run It

  • Run your code (Ctrl+s then F5)
  • Once you've typed in some test names, make sure it's all working fine by getting the interpreter to print the names.
print(playerOne)
print(playerTwo)

Code It

  • Now let's give the players some instructions.
print('Welcome to The 100 Game.')
print('You will each take turns to choose a number between 1 and 10.')
print('The first person to reach 100 is the winner')
  • It would be nice if we could personalise this output a little, by using the players names.
  • In python, you can string formatting syntax to print variable values within your strings.
  • Try replacing the first print line with the following
print('Welcome to The 100 Game %s and %s'%(playerOne,playerTwo))
  • The %s within the print statements are placeholders, where the variable values will be placed. The s indicates that the value will be a string.
  • At the end of the print statement (but inside the brackets), we place the variable names, whose values we want in the string.
  • Notice that the variable names have brackets around them as well - in Python, when we place items in brackets like this, it is called a tuple.
  • e.g (1,2,3,4) or ('abc','def','ghi') or (foo,bar,baz)

Learn It

  • For the game to work, we're going to need to keep asking each player for a number, and keep track of the total, until it reaches 100, at which point the game should end.
  • This looks like a job for a while loop

Code It

  • Save your original The-100-Game.py file.
  • Create a new file called whileLoops.py
  • A while loop will keep running until a given condition is met.
  • The basic structure is…
while something:
    do something

Badge It - Silver

  • Try each of the following while loops - One at a time
  • Write down what each of the loops does using comments in the code..
##This loop ....
while True:
    print('Computer Science is the best')
##This loop ....
foo = 0
while foo < 10:
    print(foo)
    foo += 1
##This loop ....
answer = 'yes'
while answer == 'yes':
    answer = input('Shall I continue? ')
print('I have stopped')
##This loop ....
answer = 'yes'
while answer != 'no':
    answer = input('Shall I continue? ')
print('I have stopped')
##This loop ....
x = 1
while x < 1000:
    print(x)
    x += x
##This loop ....
x = 0
y = 1
while x < 100:
    x = x + y
    y = x - y
    print(x)

Badge It - Gold

  • Create a while loop that asks for user input and then prints out that user input until the user types exit.
  • Create a while loop that prints out all the triangle numbers up to 100
  • Create a while loop that asks the user to enter a number, and tells them to keep guessing until they choose the number 7.
  • Take this self-assessment quiz after you have uploaded your code of the three while loops.

Code It

  • Open up your The-100-Game.py file again.
  • Our while loop will need to keep going until the players have a number totaling 100, so we'll need a variable to store the total number in.
total = 0
while total < 100:
  • Let's start off simply and just ask for a number between 1 and 10, within the loop, and then add the answer onto total.
total = 0
while total < 100:
    answer = input('Give me a number between 1 and 10')
    total = total + answer

Run It

  • Run your code and note any errors you get.
  • What do you think the problems might be?

Code It

  • You assigned the variable total a value of 0
  • Then, within your while loop, you're trying to add on the user input.
  • Let's see what the problem might be.
  • Type this into your interpreter.
total = 0
type(total)
  • Now try this
answer = input('Give me a number')
  • Now give the interpreter a number, and then type:
type(answer)

Learn It

  • input() always returns a string.
  • Even when a number is typed in, it is interpreted as a string.
  • You can't add strings to integers (in Python), so we need to convert the string into an integer before we add it on.

Code It

total = 0
while total < 100:
    answer = input('Give me a number between 1 and 10')
    answer = int(answer)
    total = total + answer
  • While this code works well, and is easy to understand, it can easily be condensed.
total = 0
while total < 100:
    total += int( input('Give me a number between 1 and 10'))
  • If you completely understand the second version, then use it. Otherwise, stick with the first.

Run It

  • Run your game.
  • It should allow you to type in numbers, and then stop when the total reaches 100, but there's very little feedback from the computer.

Code It

  • Add in a print statement so that you can see what the total is each time.

Badge It - Platinum

  • To get your platinum badge, you're going to need to give a little more feedback to the user.
  • You'll need to use all the following lines of code, and put them in the correct place (and order) in your script, to get it working
playerOneTurn=True
print('%s WINS'%(playerTwo))
print('%s WINS'%(playerOne))
print('%s - pick a number from one to ten'%(playerTwo))    
print('%s - pick a number from one to ten'%(playerOne))
if playerOneTurn == True:
if playerOneTurn == False:
else:
else:
playerOneTurn = not playerOneTurn
  • The game should keep track of who is the current player, prompt each player by name to make their turn, and congratulate the winner of the game at the end.
  • Take this self-assessment quiz after you have uploaded your code of your finished 100 game.