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

finished with 996 moves #598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
133 changes: 116 additions & 17 deletions adv.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import sys
sys.path.append('../graph')

from util import Stack, Queue

from room import Room
from player import Player
from world import World
Expand All @@ -8,12 +13,15 @@
# 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"
# map_file = "maps/test_cross.txt"
# map_file = "maps/test_loop.txt"
# map_file = "maps/test_loop_fork.txt"
#map_file = "maps/test_line.txt"
#map_file = "maps/test_cross.txt"
#map_file = "maps/test_loop.txt"
#map_file = "maps/test_loop_fork.txt"
map_file = "maps/main_maze.txt"

# Loads the map into a dictionary
Expand All @@ -23,19 +31,109 @@
# Print an ASCII map
world.print_rooms()


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



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

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

def bfs(current_room):
"""
BFS for the unexplored room, then
Return the path to it
"""
#add the visited room
visited = set()
#rooms to check
q =[]
q.append((current_room, []))
count = 0
#create a visited vertex
while len(q)>0:
#dequeue the current room exist
(room, path) = q.pop(0)
if room in visited :
continue
else:
visited.add(room)
for direction in visited_room[room]:
if visited_room[room][direction] == '?':
return [path, direction]
elif visited_room[room][direction] is not None:
update_path = path.copy()
update_path.append(direction)
next_room = visited_room[room][direction]
q.append((next_room, update_path))
return None
import random
def dft(unexplored_dir):
#create an empty stack and add the starting room exists directions
stack = Stack()
stack.push(unexplored_dir)

#while stack is not empty
while stack.size() >0:
#pop the current room exits direction
current_exit = stack.pop()
move_dir =current_exit[-1]
# if this direction is not explored
if move_dir not in visited_room[player.current_room.id]:
continue
elif visited_room[player.current_room.id][move_dir] =='?':
previous_room = player.current_room.id
#move player in that direction
player.travel(move_dir)
# store the movement in the traversal path
traversal_path.append(move_dir)
# update the unexplored direction in the dictionary
visited_room[previous_room][move_dir] = player.current_room.id
opposite_value = opposit_dic[move_dir]
if player.current_room.id not in visited_room:
#if visited_room[player.current_room.id]
visited_room[player.current_room.id] = {opposite_value:previous_room}
else:
visited_room[player.current_room.id][opposite_value]= previous_room
# get all the neighbour room direction
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.push(new_dir)
unexplored_dir = bfs(player.current_room.id)
if unexplored_dir !=None:
for direction in unexplored_dir[0]:
player.travel(direction)
traversal_path.append(direction)
dft([unexplored_dir[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])



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



# TRAVERSAL TEST - DO NOT MODIFY
# TRAVERSAL TEST
visited_rooms = set()
player.current_room = world.starting_room
visited_rooms.add(player.current_room)

for move in traversal_path:
player.travel(move)
visited_rooms.add(player.current_room)
Expand All @@ -48,15 +146,16 @@




#######
# 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.")
28 changes: 28 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

# Note: This Queue class is sub-optimal. Why?
class Queue():
def __init__(self):
self.queue = []
def enqueue(self, value):
self.queue.append(value)
def dequeue(self):
if self.size() > 0:
return self.queue.pop(0)
else:
return None
def size(self):
return len(self.queue)

class Stack():
def __init__(self):
self.stack = []
def push(self, value):
self.stack.append(value)
def pop(self):
if self.size() > 0:
return self.stack.pop()
else:
return None
def size(self):
return len(self.stack)