CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Adventure starting code

[assignment description]

class Wizard:
    # Constructs a wizard who is initially in the given room holding
    # the potions given in the parameter list of strings
    def __init__(selfstartlocpotions):
        pass

    # Displays a description of the wizard's current state, including
    # the current room's description and a list of the current potions
    # (if any)
    def print_description(self):
        pass

    # Returns True if the wizard's current room is the winning location -
    # that is, if there is no monster in that location.
    def is_game_over(self):
        pass

    # Updates the wizard's state based on the outcome of the command,
    # displaying an error message if the command could not be handled.
    # (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.)
    def handle_command(selfcommand):
        pass

#
# You should not modify code below this point.
#

class Monster:
    # Constructs a monster with the given description, which can
    # be foiled by any potion in the given list of strings.
    def __init__(selfdescriptionpotions_defeating):
        self.description = description
        self.potions_defeating = potions_defeating

    # Returns a short description of this monster.
    def get_description(self):
        return self.description

    # Returns True if this monster is defeated by the named potion.
    def is_defeated_by(selfpotion):
        return potion in self.potions_defeating

class Room:
    # Creates a room with the given monster (which will be
    # None if the room is the final, winning room). The
    # description is as given in the second parameter.
    def __init__(selfmonsterdescription):
        self.description = description
        self.monster = monster
        self.adjacent = {}

    # Returns the description of this room.
    def get_description(self):
        return self.description

    # Returns the monster in this room, or None if this is the
    # final, winning room.
    def get_monster(self):
        return self.monster

    # Given a direction represented by a string that is 'n', 's', 'e', or
    # 'w', returns the room that is reached by going in that direction -
    # or None if no room lies in that direction.
    def get_adjacent(selfdirection):
        direction = direction.lower()
        if direction in self.adjacent:
            return self.adjacent[direction]
        else:
            return None

    # Adds an exit from this room into another, and from that room to this.
    def set_adjacent(selfdirectionadjacent_roomreverse_direction):
        self.adjacent[direction.lower()] = adjacent_room
        adjacent_room.adjacent[reverse_direction.lower()] = self

# Executes the main loop of the program
def run():
    wiz = create_world()
    wiz.print_description()
    while not wiz.is_game_over():
        command = input('Command: ')
        if command == 'quit' or command == 'exit':
            print('Good-bye')
            return
        wiz.handle_command(command)
        print('')
        wiz.print_description()

# Creates the cavern into which the wizard has fallen, returning the
# newly created wizard
def create_world():
    potions = 'invisibility shrink nightsight flame-retardant'.split()
    monsters = [
        Monster('giant',  [potions[0], potions[1]]),
        Monster('bat',    [potions[1], potions[2]]),
        Monster('darkness', [potions[2]]),
        Monster('fire',   [potions[3]]),
        Monster('dragon', [potions[0], potions[1], potions[3]]),
        Monster('bunny',  [potions[0]])
    ]
    rooms = [
        Room(monsters[0], 'You are at the pit\'s bottom. There is no way '
            + 'to climb out. Passages: S/E\nA giant confronts you.'),
        Room(monsters[1], 'Stalagmites fill the room. Passages: S/W\n'
            + 'A huge bat is flying around.'),
        Room(monsters[2], 'You are in deep darkness. Passages: S/E/W'),
        Room(monsters[3], 'A vast bonfire surrounds you! Passages: N/E'),
        Room(monsters[4], 'The ceiling of this huge room is 50 feet above. '
            + 'Passages: N/E/W\nA dragon fills the vast room.'),
        Room(monsters[5], 'A narrow passageway forks here, with light to '
            + 'the south. Passages: N/S/W\nYou see a bunny rabbit with '
            + 'nasty, big, pointy teeth!'),
        Room(None'You emerge into daylight on the road home. '
            + 'Congratulations!')
    ]

    rooms[0].set_adjacent('e'rooms[1], 'w')
    rooms[1].set_adjacent('e'rooms[2], 'w')
    rooms[3].set_adjacent('e'rooms[4], 'w')
    rooms[4].set_adjacent('e'rooms[5], 'w')
    rooms[0].set_adjacent('s'rooms[3], 'n')
    rooms[1].set_adjacent('s'rooms[4], 'n')
    rooms[2].set_adjacent('s'rooms[5], 'n')
    rooms[5].set_adjacent('s'rooms[6], 'n')

    init_room = rooms[0# change to rooms[5] to make the puzzle much easier
    return Wizard(init_roompotions)

run()