Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- world.loadGraph(roomGraph)
- # UNCOMMENT TO VIEW MAP
- world.printRooms()
- player = Player("Name", world.startingRoom)
- # FILL THIS IN
- # I will use this travesal path array to pass the tests
- traversalPath = []
- # traversal obj will contain all my rooms
- traversalRooms = {}
- backtrack_stack = []
- traversalMaze = True
- while traversalMaze:
- # get the current_room we are in
- current_room = player.currentRoom.id
- print(f"We are at room {current_room}")
- print(traversalRooms)
- # if the room we are in doesnt exist on the graph yet
- # lets add the exits object to it
- if current_room not in traversalRooms:
- # instantiate a all exits obj
- all_possible_exits = {}
- for exit in player.currentRoom.getExits():
- all_possible_exits[exit] = "?"
- traversalRooms[current_room] = all_possible_exits
- # we need all of the current exits for the room
- all_current_exits = traversalRooms[current_room]
- if "n" in all_current_exits and all_current_exits["n"] == "?":
- # print('north')
- # append the direction we are going to the traversalPath
- traversalPath.append("n")
- # travel to the north since north exists
- player.travel("n")
- # get the new room we are in
- room_to_the_north = player.currentRoom.id
- traversalRooms[current_room]["n"] = room_to_the_north
- # add it if it doesnt exist on the graph or else we will have a broken graph
- if room_to_the_north not in traversalRooms:
- all_possible_exits = {}
- for exit in player.currentRoom.getExits():
- all_possible_exits[exit] = "?"
- traversalRooms[room_to_the_north] = all_possible_exits
- else:
- # set the current room's opposite to be the entrace that we have which is current_room
- traversalRooms[room_to_the_north]["s"] = current_room
- # add it to the backtrack stack
- backtrack_stack.append('s')
- elif "s" in all_current_exits and all_current_exits["s"] == "?":
- # print('south')
- # append the direction we are going to the traversalPath
- traversalPath.append("s")
- # travel to the south since south exists
- player.travel("s")
- # get the new room we are in
- room_to_the_south = player.currentRoom.id
- traversalRooms[current_room]["s"] = room_to_the_south
- if room_to_the_south not in traversalRooms:
- all_possible_exits = {}
- for exit in player.currentRoom.getExits():
- all_possible_exits[exit] = "?"
- traversalRooms[room_to_the_south] = all_possible_exits
- else:
- # set the current room's opposite to be the entrace that we have which is current_room
- traversalRooms[room_to_the_south]["n"] = current_room
- # add it to the backtrack stack
- backtrack_stack.append('n')
- elif "w" in all_current_exits and all_current_exits["w"] == "?":
- # rinse and repeat with the algo on south and north
- # print('west')
- traversalPath.append("w")
- # print('debugging')
- player.travel("w")
- room_to_the_west = player.currentRoom.id
- traversalRooms[current_room]["w"] = room_to_the_west
- if room_to_the_west not in traversalRooms:
- all_possible_exits = {}
- for exit in player.currentRoom.getExits():
- all_possible_exits[exit] = "?"
- traversalRooms[room_to_the_west] = all_possible_exits
- else:
- # set the current room's opposite to be the entrace that we have which is current_room
- traversalRooms[room_to_the_west]["e"] = current_room
- # add it to the backtrack stack
- backtrack_stack.append('e')
- elif "e" in all_current_exits and all_current_exits["e"] == "?":
- # rinse and repeat with the algo on south and north
- # print('west')
- traversalPath.append("e")
- # print('debugging')
- player.travel("e")
- room_to_the_east = player.currentRoom.id
- traversalRooms[current_room]["e"] = room_to_the_east
- if room_to_the_east not in traversalRooms:
- all_possible_exits = {}
- for exit in player.currentRoom.getExits():
- all_possible_exits[exit] = "?"
- traversalRooms[room_to_the_east] = all_possible_exits
- else:
- # set the current room's opposite to be the entrace that we have which is current_room
- traversalRooms[room_to_the_east]["w"] = current_room
- # add it to the backtrack stack
- backtrack_stack.append('w')
- else:
- # if we have no backtrack to do we will break the while loop
- if len(backtrack_stack) == 0:
- print(' \n\n\n\n\n\nend\n\n\n\n\n\n')
- traversalMaze = False
- else:
- # Just like DFS we get the last element and work on it.
- # So we can backtrack properly
- path_to_backtrack_travel = backtrack_stack.pop()
- # Travel back to the backtrack travel path
- player.travel(path_to_backtrack_travel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement