diff --git a/adv.py b/adv.py index 84f031836..f93076d82 100644 --- a/adv.py +++ b/adv.py @@ -27,9 +27,45 @@ # Fill this out with directions to walk # traversal_path = ['n', 'n'] -traversal_path = [] - - +go_back = { + "n" : "s", + "s" : "n", + "e" : "w", + "w" : "e" +} + +def traverse_graph(current_room, visited=None): + # Initialize path + path = [] + # create a set of visited rooms if empty + if visited == None: + visited = set() + + # Traverse through each exit + for possible_move in player.current_room.get_exits(): + # Travel to the next room + player.travel(possible_move) + + # If current room is in visited + if player.current_room in visited: + # Use dictionary and current move to go back to previous room + player.travel(go_back[possible_move]) + else: + # Add current room to the visited set + visited.add(player.current_room) + # Append the move to the path + path.append(possible_move) + # Recursively call the method passing in the current room and visited set + path = path + traverse_graph(player.current_room, visited) + # Go back to previous room + player.travel(go_back[possible_move]) + # Append move to path + path.append(go_back[possible_move]) + + # At end of loop, return the path + return path + +traversal_path = traverse_graph(player.current_room) # TRAVERSAL TEST - DO NOT MODIFY visited_rooms = set() @@ -51,12 +87,12 @@ ####### # UNCOMMENT TO WALK AROUND ####### -player.current_room.print_room_description(player) -while True: - cmds = input("-> ").lower().split(" ") - if cmds[0] in ["n", "s", "e", "w"]: - player.travel(cmds[0], True) - elif cmds[0] == "q": - break - else: - print("I did not understand that command.") +# player.current_room.print_room_description(player) +# while True: +# cmds = input("-> ").lower().split(" ") +# if cmds[0] in ["n", "s", "e", "w"]: +# player.travel(cmds[0], True) +# elif cmds[0] == "q": +# break +# else: +# print("I did not understand that command.")