Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rooms_array = []
- def get_room(index):
- return rooms_array[index]
- # class to store the room description and direction indexes
- # the indexes are positions in the room array of the room in that direction
- # for example, if there is a room to the east, and the east index is 8, then that
- # room is rooms_array[8]
- # however if the index is -1, then there is no room there -- a check needs to be performed
- # before moving to this room to make sure it isn't -1
- class Room:
- def __init__(self, description, north_index = -1, east_index = -1, south_index = -1, west_index = -1):
- self.north = north_index
- self.east = east_index
- self.south = south_index
- self.west = west_index
- self.description = description
- def list_options(self):
- direction_str = ""
- if self.north != -1:
- direction_str += "North, "
- if self.east != -1:
- direction_str += "East, "
- if self.south != -1:
- direction_str += "South, "
- if self.west != -1:
- direction_str += "West"
- print direction_str
- # the main program
- if __name__ == "__main__":
- current_room = None
- # set up our rooms and add them to the array
- Room leftRoom = Room("This is the left room... It's dark", east_index=1)
- Room rightRoom = Room("This is the right room... It's light", west_index=0)
- rooms_array.append(leftRoom)
- rooms_array.append(rightRoom)
- # we start in the left room
- current_room = leftRoom
- # to transition
- # player enters "GO EAST"
- # it gets it from the room array
- current_room = get_room(current_room.east)
- # since get_room() doesn't check if the room exists (index is -1) this check needs to be done beforehand
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement