Due: 11:00pm, Monday, May 5. Value: 40 pts.
Stupid wizard. Driving around the countryside, you stopped to enjoy the scenery outside. But you locked your wand in the car, so you couldn't get back in. The only thing to do was to bushwhack through the forest in hope of reaching a locksmith. Along the way, you fell into a cave, with no way to climb out. And you can't cast your levitation spell without a wand. You have to find some other way out of this cave.
In each room of this cave, there's an obstacle that prevents you from leaving that room easily. Luckily, you happened to be carrying four potions in your robe's pockets, which might be helpful for getting past the obstacles. Of course, you have only one dose of each potion, and that dose will only last long enough for you to exit the current room.
Let's see if there's a way out….
The code available for download sets up this scenario. When you have it working, the program sohuld work as shown in the attached transcript.
Your job is to complete one class — the
Wizard
class — which
is used by the run
function. You should not modify the any class but
Wizard
, though you'll need to examine
the methods provided by the Room
and Monster
classes. The
Wizard
class requires a constructor
and three methods.
Wizard(startloc, potions)
(Constructor) Constructs a wizard whose initial location is
startloc
(a Room
object) and who is carrying all the
potions in potions
(a list of strings).
I recommend that the object be initialized to have three
instance variables (that is, variables within self
).
One will refer to the Room
where the wizard currently is; another will be a list of the
names of potions the wizard is still carrying; and the last will be
True
or False
depending on whether the wizard has drunk a potion that defeats the
monster in the current room — it will be set to True
when the wizard drinks a working potion, and it will be reset to
False
when the wizard moves to a different room.
is_game_over()
Returns True
if this wizard has gotten out of
the cave. You can detect this condition by checking whether the
wizard is currently in a room whose
“monster” is None
.
print_description()
Displays a description of this wizard's current situation, including the description of the room and the list of potions currently being carried.
handle_command(command)
Handles a command typed by the user.
The user may enter a potion, which would remove it from the list
of potions the wizard holds.
Or the user may type a direction
(n/s/e/w), in which case the wizard may
change rooms — if there is a passage in that direction,
and if while in the current room the wizard
drank a potion that gets by the monster.
(The user may also type quit or exit to exit
the program, but you don't need to worry about this since it is handled
by the run
function before
invoking handle_command
.)
There are three error conditions: The given command could not be interpreted, the monster blocks the exit, or there is no exit in the given direction. Another possible error condition is that the user types the name of a potion that has already been drunk, but you can treat that the same as a command that cannot be interpreted. In any of these cases, the method should display a message to that effect, and the command has no further effect.
To test to see whether the game continues to work at its end, you
could of course solve the puzzle. But an easier way is to modify the
next-to-last line of the create_world
function so that
the initial room is rooms[5]
rather than rooms[0]
.
From there, you have only to go south to reach the exit.