Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. rooms_array = []
  2.  
  3. def get_room(index):
  4.     return rooms_array[index]
  5.  
  6. # class to store the room description and direction indexes
  7. # the indexes are positions in the room array of the room in that direction
  8. # for example, if there is a room to the east, and the east index is 8, then that
  9. # room is rooms_array[8]
  10. # however if the index is -1, then there is no room there -- a check needs to be performed
  11. # before moving to this room to make sure it isn't -1
  12. class Room:
  13.     def __init__(self, description, north_index = -1, east_index = -1, south_index = -1, west_index = -1):
  14.         self.north = north_index
  15.         self.east = east_index
  16.         self.south = south_index
  17.         self.west = west_index
  18.         self.description = description
  19.        
  20.     def list_options(self):
  21.         direction_str = ""
  22.         if self.north != -1:
  23.             direction_str += "North, "
  24.         if self.east != -1:
  25.             direction_str += "East, "
  26.         if self.south != -1:
  27.             direction_str += "South, "
  28.         if self.west != -1:
  29.             direction_str += "West"
  30.         print direction_str
  31.        
  32. # the main program
  33. if __name__ == "__main__":
  34.     current_room = None
  35.  
  36.     # set up our rooms and add them to the array
  37.     Room leftRoom = Room("This is the left room... It's dark", east_index=1)
  38.     Room rightRoom = Room("This is the right room... It's light", west_index=0)
  39.     rooms_array.append(leftRoom)
  40.     rooms_array.append(rightRoom)
  41.    
  42.     # we start in the left room
  43.     current_room = leftRoom
  44.    
  45.     # to transition
  46.     # player enters "GO EAST"
  47.     # it gets it from the room array
  48.     current_room = get_room(current_room.east)
  49.     # 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