Depending on the number of rooms that your game will have, it may be easier to refine our link_room( ) method to add two rooms to each others linked_rooms attribute. For this functionality we need to:
create a complimentary direction dictionary
change the link_room( ) method
remove half of the link_room( ) instructions from main.py
1. Create a complimentary direction dictionary
The goal is to have the primary room add the destination room to the linked_rooms attribute, and then add the primary room to the to the destination room's linked_rooms attribute. But to do that, we need a way of working with complimentary directions. That is if we say 'north', the complimentary is 'south', 'left' is 'right', 'up' is 'down'.
In room.py
.
.
.
def describe(self):
print( self.description )
def link_room(self, room_to_link, direction):
# add a dictionary of possible directions an their compliment
compliment = {
'north': 'south',
'east': 'west',
'up': 'down',
'left': 'right'
}
self.linked_rooms[direction] = room_to_link
def get_details(self):
print(self.name)
print("--------------------")
.
.
.
2. Change the link_room( ) method
The goal is to have the primary room add the destination room to the linked_rooms attribute, and then add the primary room to the to the destination room's linked_rooms attribute.
In room.py
.
.
.
def describe(self):
print( self.description )
def link_room(self, room_to_link, direction):
compliment = {
'north': 'south',
'east': 'west',
'up': 'down',
'left': 'right'
}
# add the link for the other room direction
room_to_link.linked_rooms[compliment[direction]] = self
self.linked_rooms[direction] = room_to_link
def get_details(self):
print(self.name)
print("--------------------")
.
.
.
3. Change the link_room( ) instructions from main.py
Now that we have changed the method, we need to change how we call it in the main game.
In main.py
.
.
.
print("There are " + str(Room.number_of_rooms) + " rooms to explore.")
# only link the rooms that are north and east
kitchen.link_room(dining_hall, "south")
dining_hall.link_room(kitchen, "north")
dining_hall.link_room(ballroom, "west")
ballroom.link_room(dining_hall, "east")
dave = Enemy("Dave", "A smelly zombie")
dave.set_conversation("Brrlgrh... rgrhl... brains...")
.
.
.
Extra: Make oneway passages
If we want a link to only be one way, we now need to define a new method to remove a link that we have already done.
in room.py
.
.
.
def describe(self):
print( self.description )
def link_room(self, room_to_link, direction):
compliment = {
'north': 'south',
'east': 'west',
'up': 'down',
'left': 'right'
}
room_to_link.linked_rooms[compliment[direction]] = self
self.linked_rooms[direction] = room_to_link
# add a method to remove a link that exists in a room
def remove_link(self, direction):
if direction in self.linked_rooms:
del self.linked_rooms[direction]
def get_details(self):
print(self.name)
print("--------------------")
.
.
.
then in main.py, remove the direction that you do not want to have linked.
.
.
.
print("There are " + str(Room.number_of_rooms) + " rooms to explore.")
dining_hall.link_room(kitchen, "north")
ballroom.link_room(dining_hall, "east")
# provide the instruction to remove the link from the room. In this case removing ballroom link from the dining_hall
dining_hall.remove_link("west")
dave = Enemy("Dave", "A smelly zombie")
dave.set_conversation("Brrlgrh... rgrhl... brains...")
.
.
.