Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprint Challenge: Graphs | Oliver Abreu #587

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions adv.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,44 @@
# traversal_path = ['n', 'n']
traversal_path = []


# <-- START CODE
visited = {}

oppostie_dir = {
'n': 's', 's': 'n', 'e': 'w', 'w': 'e'
}

backtrack_route = [None]

# while number of rooms visited is less than total number of rooms
while len(visited) < len(room_graph) - 1:
# if the current room has not yet been visited
if player.current_room.id not in visited:
# get all the exits of the current room
visited[player.current_room.id] = player.current_room.get_exits()
# if we have come in from another room,
if backtrack_route[-1]:
# remove the direction we just came from the current room (in visited rooms)
visited[player.current_room.id].remove(backtrack_route[-1])
else:
continue
# while the current room you are in does not have any unvisted exits
while len(visited[player.current_room.id]) == 0:
# the previous path is equal to the last direction in the backtrack route
previous_path = backtrack_route.pop()
# add to the traversal path the previous path
traversal_path.append(previous_path)
# travel the previous path
player.travel(previous_path)
# next direction is equal to the most recently visited room
next_direction = visited[player.current_room.id].pop()
# add the next direction to the traversal path
traversal_path.append(next_direction)
# add to the backtracking route by adding the opposite direction to the next direction
backtrack_route.append(oppostie_dir[next_direction])
# go in the next direction!
player.travel(next_direction)
# --> END CODE

# TRAVERSAL TEST - DO NOT MODIFY
visited_rooms = set()
Expand All @@ -51,12 +88,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.")