Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

ICT112 – Module 7 Workshop

Learning Objectives
In this Workshop you will:
1. use string methods to make a game more robust;
2. use the string format(...) method to get nice output;
3. use Lists to store flexible collections of data

Task 1
Each week (week 1 to 8) you will be required to submit into Canvas your solution. Each submission
will be marked out of 5. Task 1 will be the total of your best 6 submissions (30 marks possible).

Theme: Text Adventure Games


Back in the 1970s, computers were text only, and
graphics was far too expensive for ordinary people.
But that didn't stop programmers from inventing
some intruiging computer games - these days we
call them Text Adventure Games. They are like an
interactive short story, where you are faced with a
scenario and have to decide what to do or where to
go next. Depending upon what choice you make,
the story will evolve differently.

In today's lab you are going to design and


implement your own simple text-based Figure 1. http://blog.readytomanage.com/fact-based-decision-making/
Figure 2. Colossal Cave Adventure, from http://rickadams.org/adventure.
adventure game.

Before you do that, you might like to spend a few minutes playing one or two of the existing
games to see how they work:

 http://textadventures.co.uk (try "Hitchhikers Guide to the Galaxy"? or "Jacqueline, Jungle Queen!")


 http://rickadams.org/adventure The original Colossal Cave Adventure. (The Wikipedia page gives
some fascinating history about this 1970s game:
http://en.wikipedia.org/wiki/Colossal_Cave_Adventure).
ICT112 – Module 7 Workshop

1. Make a game robust


The game developers have built a partial text game, using command-line input and output.
The code for the game is supplied in text_game.py
Improve the main loop of the game so that:

1. All user input is converted to lowercase, so that it does not matter if the player has
the CAPS-LOCK key on or types a mixture of uppercase and lowercase letters.

2. Extra spaces ('whitespace') are allowed anywhere in the user input, and the game
still behaves correctly.
Here are some example input commands you should test. These should all behave the
same as "go north"
 "go North"
 "GO NORTH"
 " go north "

These should all behave the same as "take black key" (or any other multi-word object in
your game).
 "take Black Key"
 " take black key "

Hint: after the input("...") command in the main loop of your game, you should convert the
input result to lowercase, and then split it into words, so that all whitespace is removed.
Then for the commands that can take multi-word objects, you can either pass a list of
words, or turn the list of words back into a single string by using the " ".join(list) method.
ICT112 – Module 7 Workshop

2. Add movable objects


The goal of this section is for the player to be able to pick up an object in one location, and
then drop it in another location. It should stay at the new location, be visible in the
description of that location, and disappear from the description of the old location.
Here are several suggested steps you can follow to get this working:

1. You need to have a separate list of movable objects for each location. So up the top
of your program, we will create one global dictionary, called items_in, which maps
each room/location name to a list of items (strings). So, to get the list of items for a
particular room, like the "bedroom", you can just write items_in["bedroom"], or to get the
list of items in the current room, you can just write items_in[state]. And of course,
since this is a list, you can use the append and remove list methods to modify the
items in that room. Here is the code to create this dictionary for the original simple
game. Notice how the movable objects in each room at the beginning of the game
are included in the appropriate list for each room. Copy this code and add it to the
global variables of your program, and modify the rooms and items to be what you
want for your own game:
# global dictionary of the movable objects in each location
items_in = {"bedroom" : ["aquarium", "bats"],
"hallway" : [],
"lounge" : ["stereo"]
}
2. Extend your describe() function so that it displays all the movable objects in the
current location. Hint: loop through the list of items in the current room like this: for
item in items_in[state]: ... and print out each item with a message like "You can see: "+item
3. Update your take_cmd(obj) function so that it gets the list of objects in the current
location and checks that the requested object does really exist in that list, before it
puts the object into the carrying variable. It should also remove the object from the
room, because you have now picked it up. Hint: to update a list (e.g. called contents)
you will find the following methods helpful:
a. s in contents # True if s is already in this location
b. contents.append(s) # adds s to the end of the list
c. contents.remove(s) # removes s from the list
4. Add a 'drop object' command to your game so that the player can drop an object in
the current location. (You could just do a simple 'drop' command at this stage with
no object specified, since the player can only carry one thing. But shortly we will
extend 'carrying' to be a list, and then the drop command will need to specify which
object to drop.)
ICT112 – Module 7 Workshop

3. Carry multiple objects


Now let's allow your player to carry more than one object at a time!
Hint: change your carrying variable to be a list. Initially empty? Then update all the code at
the places where you use carrying, so that you append or remove objects in that carrying
list.
Add an inventory command too, so that the player can see all the things that s/he is
currently carrying!

4. Finish your game [optional]


Well done, if you got this far! Now use the rest of your time to finish your game and test it
thoroughly. Make it fun to play, inject some humour into the dialogue, and have a couple
of endings. A good ending and a bad one?
Get some friends or relatives to play your game and observe what they do. It's surprising
what people will misunderstand, or interpret differently...

Task 1 - Submission
Ensure you submit 3 files. You can continue to add features to your earlier files and submit in the
new file (ie text_game_movable.py can contain Q1 and 2).

1. Submit your answer to Question 1 Make a game robust as a python program


“text_game_robust.py” (2 marks)

2. Submit your answer to Question 2 Add movable objects as a python program


“text_game_movable.py” (2 marks)

3. Submit your answer to Question 3 Carry multiple objects as a python program


“text_game_multiple.py” (1 mark)

You might also like