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

CSPT13-Karthik-SC-Graphs #601

Open
wants to merge 1 commit 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
107 changes: 89 additions & 18 deletions adv.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from room import Room
from player import Player
from world import World

from collections import deque
import random
from ast import literal_eval

# Load world
world = World()

#construct a traversal graph
#do dft for finding all the possible room player can move
#do bfs for finding unexplored direction

# You may uncomment the smaller graphs for development and testing purposes.
# map_file = "maps/test_line.txt"
Expand All @@ -24,14 +27,84 @@
world.print_rooms()

player = Player(world.starting_room)
player.current_room = world.starting_room

# Fill this out with directions to walk
# traversal_path = ['n', 'n']
traversal_path = []



# TRAVERSAL TEST - DO NOT MODIFY
backtrack = {
'n': 's',
'e': 'w',
'w': 'e',
's': 'n'}

def bfs(curr_room):
visited = set()
queue = deque()
queue.append((curr_room,[]))

while len(queue)>0:
(room,path) = queue.popleft()
if room in visited:
continue
visited.add(room)
for direction in visited_room[room]:
if visited_room[room][direction] == '?':
return [path,direction]
else:
newPath = path.copy()
newPath.append(direction)
next_room = visited_room[room][direction]
queue.append((next_room,newPath))

def dft(unexplored_diection):
stack = deque()
stack.append(unexplored_diection)

while len(stack)>0:
curr_exit = stack.pop()
move_direction = curr_exit[-1]
if move_direction not in visited_room[player.current_room.id]:
continue
elif visited_room[player.current_room.id][move_direction] == '?':
prev_room = player.current_room.id
player.travel(move_direction)
traversal_path.append(move_direction)

visited_room[prev_room][move_direction] = player.current_room.id
opposite_val = backtrack[move_direction]

if player.current_room.id not in visited_room:
visited_room[player.current_room.id] = {opposite_val:prev_room}
else:
visited_room[player.current_room.id][opposite_val] = prev_room

for direction in player.current_room.get_exits():
if direction not in visited_room[player.current_room.id]:
visited_room[player.current_room.id][direction] = '?'
new_dir = []
new_dir.append(direction)
stack.append(new_dir)

unexplored_diection = bfs(player.current_room.id)

if unexplored_diection != None:
for direction in unexplored_diection[0]:
player.travel(direction)
traversal_path.append(direction)
dft([unexplored_diection[1]])


starting_dir = random.choice(player.current_room.get_exits())

visited_room = {player.current_room.id:{}}
for direction in player.current_room.get_exits():
visited_room[player.current_room.id][direction] = '?'

dft(starting_dir)

# TRAVERSAL TEST
visited_rooms = set()
player.current_room = world.starting_room
visited_rooms.add(player.current_room)
Expand All @@ -46,17 +119,15 @@
print("TESTS FAILED: INCOMPLETE TRAVERSAL")
print(f"{len(room_graph) - len(visited_rooms)} unvisited rooms")



#######
# 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.")
# #######
# # 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.")