现在似乎是开始使用类的好时机。通过一个类,你将能够处理连接到每个房间的东西,以及通过哪个方向。这里有一个简单的例子。在class Room(object):
def __init__(self, name=None):
self.name = str(name)
self.matching_direction = { "N": "S", "S": "N", "W": "E", "E": "W"}
self.open_walls = ["N", "S", "E", "W"]
self.connected_rooms = {}
def connect_room(self, room_obj=None, room_obj_exit=None):
"""
Connect two rooms if able.
Params:
room_obj: Room(Object)
room_obj_exit: Direction(String)
Return: Bool
"""
try:
opposite_direction = self.matching_direction[room_obj_exit]
if opposite_direction not in self.open_walls:
print "{} door is occupied by {}".format(opposite_direction, self.connected_rooms[opposite_