From e834a3dbef009fe7ceed1c2c3ac1e912416a3fb1 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sat, 21 Nov 2020 09:00:26 -0800 Subject: [PATCH 01/11] for submission --- adv.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/adv.py b/adv.py index 84f031836..31bbd620c 100644 --- a/adv.py +++ b/adv.py @@ -10,11 +10,11 @@ # You may uncomment the smaller graphs for development and testing purposes. -# map_file = "maps/test_line.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" +# map_file = "maps/main_maze.txt" # Loads the map into a dictionary room_graph=literal_eval(open(map_file, "r").read()) @@ -27,8 +27,8 @@ # Fill this out with directions to walk # traversal_path = ['n', 'n'] -traversal_path = [] - +traversal_path = ['n', 's', 'w', 'e'] +# Do BFS to find path then DFT to move the player # TRAVERSAL TEST - DO NOT MODIFY @@ -51,12 +51,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.") From 8dbbc81a06d8fc7c5bd215dd32715a2166d6cfaf Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sat, 21 Nov 2020 11:22:12 -0800 Subject: [PATCH 02/11] Working out the code --- adv.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/adv.py b/adv.py index 31bbd620c..81213c62f 100644 --- a/adv.py +++ b/adv.py @@ -28,7 +28,42 @@ # Fill this out with directions to walk # traversal_path = ['n', 'n'] traversal_path = ['n', 's', 'w', 'e'] + # Do BFS to find path then DFT to move the player +def dftr_maze(current_room, visited=None): + directions = [] + + # Creating a set to hold visited rooms if visited in None + if visited == None: + visited = set() + + # Finding exits for current_room using player from player and get_exits from room + for move in player.current_room.get_exits(): + # making moves using travel from player + player.travel(move) + + # reverse_move if already visited to find new path + if player.current_room in visited: + player.travel(reverse_move[move]) + # if its a new room, do the following + else: + # add to visited stack + visited.add(player.current_room) + # adding this move to 'direction' + directions.append(move) + # recursive: repeating loop and adding directions ('\' is an escape character in Python) + directions = directions + \ + dftr_maze(player.current_room, visited) + # go_back to previous room + player.travel(reverse_move[move]) + # appending this move to 'directions' + directions.append(reverse_move[move]) + + return directions + + # fill this out with directions to walk + traversal_path = dftr_maze(player.current_room) + # print(dftr_maze) # TRAVERSAL TEST - DO NOT MODIFY From 720ab87e005d83c7461113ca67bcd8d9c2600d18 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sat, 21 Nov 2020 11:56:46 -0800 Subject: [PATCH 03/11] Doing code in BFT. Current code is not working. --- adv.py | 93 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/adv.py b/adv.py index 81213c62f..9df7f4456 100644 --- a/adv.py +++ b/adv.py @@ -30,40 +30,65 @@ traversal_path = ['n', 's', 'w', 'e'] # Do BFS to find path then DFT to move the player -def dftr_maze(current_room, visited=None): - directions = [] - - # Creating a set to hold visited rooms if visited in None - if visited == None: - visited = set() - - # Finding exits for current_room using player from player and get_exits from room - for move in player.current_room.get_exits(): - # making moves using travel from player - player.travel(move) - - # reverse_move if already visited to find new path - if player.current_room in visited: - player.travel(reverse_move[move]) - # if its a new room, do the following - else: - # add to visited stack - visited.add(player.current_room) - # adding this move to 'direction' - directions.append(move) - # recursive: repeating loop and adding directions ('\' is an escape character in Python) - directions = directions + \ - dftr_maze(player.current_room, visited) - # go_back to previous room - player.travel(reverse_move[move]) - # appending this move to 'directions' - directions.append(reverse_move[move]) - - return directions - - # fill this out with directions to walk - traversal_path = dftr_maze(player.current_room) - # print(dftr_maze) + + +def bfs(self, starting_vertex, destination_vertex): + """ + Return a list containing the shortest path from + starting_vertex to destination_vertex in + breath-first order. + """ + visited = set() + queue = deque() + queue.append ([starting_vertex]) + + while len(queue) > 0: + currPath = queue.popleft() + currNode = currPath[-1] + if currNode == destination_vertex: + return currPath + if currNode not in visited: + visited.add(currNode) + for neighbor in self.vertices[currNode]: + newPath = list(currPath) + newPath.append(neighbor) + queue.append(newPath) + return [] + + +# def dftr_maze(current_room, visited=None): +# directions = [] + +# # Creating a set to hold visited rooms if visited in None +# if visited == None: +# visited = set() + +# # Finding exits for current_room using player from player and get_exits from room +# for move in player.current_room.get_exits(): +# # making moves using travel from player +# player.travel(move) + +# # reverse_move if already visited to find new path +# if player.current_room in visited: +# player.travel(reverse_move[move]) +# # if its a new room, do the following +# else: +# # add to visited stack +# visited.add(player.current_room) +# # adding this move to 'direction' +# directions.append(move) +# # recursive: repeating loop and adding directions ('\' is an escape character in Python) +# directions = directions + \ +# dftr_maze(player.current_room, visited) +# # go_back to previous room +# player.travel(reverse_move[move]) +# # appending this move to 'directions' +# directions.append(reverse_move[move]) + +# return directions + +# # fill this out with directions to walk +# traversal_path = dftr_maze(player.current_room) # TRAVERSAL TEST - DO NOT MODIFY From 9c96c69bbaa5aa729d626c2da5b592d37029a00a Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Wed, 25 Nov 2020 10:00:34 -0800 Subject: [PATCH 04/11] Push to view on github --- adv.py | 72 ++++++++++------------------------------------------------ 1 file changed, 12 insertions(+), 60 deletions(-) diff --git a/adv.py b/adv.py index 9df7f4456..d6bfb9a3b 100644 --- a/adv.py +++ b/adv.py @@ -3,6 +3,7 @@ from world import World import random +from collections import deque from ast import literal_eval # Load world @@ -29,67 +30,18 @@ # traversal_path = ['n', 'n'] traversal_path = ['n', 's', 'w', 'e'] -# Do BFS to find path then DFT to move the player - - -def bfs(self, starting_vertex, destination_vertex): - """ - Return a list containing the shortest path from - starting_vertex to destination_vertex in - breath-first order. - """ - visited = set() - queue = deque() - queue.append ([starting_vertex]) - - while len(queue) > 0: - currPath = queue.popleft() - currNode = currPath[-1] - if currNode == destination_vertex: - return currPath - if currNode not in visited: - visited.add(currNode) - for neighbor in self.vertices[currNode]: - newPath = list(currPath) - newPath.append(neighbor) - queue.append(newPath) - return [] - - -# def dftr_maze(current_room, visited=None): -# directions = [] - -# # Creating a set to hold visited rooms if visited in None -# if visited == None: -# visited = set() - -# # Finding exits for current_room using player from player and get_exits from room -# for move in player.current_room.get_exits(): -# # making moves using travel from player -# player.travel(move) - -# # reverse_move if already visited to find new path -# if player.current_room in visited: -# player.travel(reverse_move[move]) -# # if its a new room, do the following -# else: -# # add to visited stack -# visited.add(player.current_room) -# # adding this move to 'direction' -# directions.append(move) -# # recursive: repeating loop and adding directions ('\' is an escape character in Python) -# directions = directions + \ -# dftr_maze(player.current_room, visited) -# # go_back to previous room -# player.travel(reverse_move[move]) -# # appending this move to 'directions' -# directions.append(reverse_move[move]) - -# return directions - -# # fill this out with directions to walk -# traversal_path = dftr_maze(player.current_room) +# Pick a random unexplored direction from current room, travel and log that direction, then loop. When reach dead end, walk back and find nearest room that is unexplored. + +exits = player.current_room.get_exits() +currRoom = player.current_room.id +stack = deque() + +while len(exits) > 0: + currRoom = stack.pop() + if currRoom not in visited_rooms: + visited_rooms.add(currRoom) + print(currNode) # TRAVERSAL TEST - DO NOT MODIFY visited_rooms = set() From 30ce21b19b718bb400a6bd16e5063041e8c5b421 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Sun, 29 Nov 2020 10:10:47 -0800 Subject: [PATCH 05/11] working on bugs --- adv.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/adv.py b/adv.py index d6bfb9a3b..951bd2acb 100644 --- a/adv.py +++ b/adv.py @@ -11,11 +11,11 @@ # You may uncomment the smaller graphs for development and testing purposes. -map_file = "maps/test_line.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" +map_file = "maps/main_maze.txt" # Loads the map into a dictionary room_graph=literal_eval(open(map_file, "r").read()) @@ -29,19 +29,64 @@ # Fill this out with directions to walk # traversal_path = ['n', 'n'] traversal_path = ['n', 's', 'w', 'e'] - +opposite_directions = {'n': 's', 's': 'n', 'w': 'e', 'e': 'w'} # Pick a random unexplored direction from current room, travel and log that direction, then loop. When reach dead end, walk back and find nearest room that is unexplored. exits = player.current_room.get_exits() currRoom = player.current_room.id -stack = deque() - -while len(exits) > 0: - currRoom = stack.pop() - if currRoom not in visited_rooms: - visited_rooms.add(currRoom) - print(currNode) + +# Return a list containing the shortest path from starting_vertex to destination_vertex in breath-first order. + +# def bfs(self, starting_vertex = currRoom, destination_vertex = ?): +# visited_rooms = set() +# queue = deque() +# queue.append ([currRoom]) + +# while len(queue) > 0: +# currPath = queue.popleft() +# currNode = currPath[-1] + +# if currNode is ?: +# return currPath + +# if currNode not in visited_rooms: +# visited_rooms.add(currNode) +# for neighbor in self.visited_rooms: +# newPath = list(currPath) +# newPath.append(neighbor) +# queue.append(newPath) +# return [] + +def traverse_map(starting_room, visited=[]): + # Define a path with what you start with aka no rooms or possible directions + path = [] +# Loop through the exits for the current room + for direction in player.current_room.get_exits(): + # Have the player travel in the given directions + player.travel(direction) +# Check to see if the current room is already in the `visited` array + if player.current_room.id in visited: + # If it is, move the player in the opposite direction, that room has already been visited so we've already checked all possible directions + player.travel(opposite_directions[direction]) +# If it is not + else: + # Add the current room to the `visited` array + visited.append(player.current_room.id) +# Add that direction to the path array + path.append(direction) +# Let the path equal the current path plus the path we just created + path = path + traverse_map(player.current_room.id, visited) +# Have the player travel in the opposite direction of the path + player.travel(opposite_directions[direction]) +# Add the opposite directions to the path array + path.append(opposite_directions[direction]) + + # finally, return the path + return path + + +traversal_path = traverse_map(player.current_room.id) # TRAVERSAL TEST - DO NOT MODIFY visited_rooms = set() From 4739991a88863c2c06a0e1b83465e79ffe54dbc8 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Mon, 30 Nov 2020 20:08:35 -0800 Subject: [PATCH 06/11] Pushing for review --- adv.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/adv.py b/adv.py index 951bd2acb..c16968c73 100644 --- a/adv.py +++ b/adv.py @@ -11,11 +11,11 @@ # You may uncomment the smaller graphs for development and testing purposes. -# map_file = "maps/test_line.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" +# map_file = "maps/main_maze.txt" # Loads the map into a dictionary room_graph=literal_eval(open(map_file, "r").read()) @@ -33,8 +33,17 @@ # Pick a random unexplored direction from current room, travel and log that direction, then loop. When reach dead end, walk back and find nearest room that is unexplored. -exits = player.current_room.get_exits() -currRoom = player.current_room.id +def traversal(starting_room=player.current_room): + exits = player.current_room.get_exits() + currRoom = player.current_room.id + + visited = set() + stack = deque() + stack.append([starting_room]) + + for direction in exits: + if currRoom in visited: + print(visited) # Return a list containing the shortest path from starting_vertex to destination_vertex in breath-first order. @@ -59,30 +68,19 @@ # return [] def traverse_map(starting_room, visited=[]): - # Define a path with what you start with aka no rooms or possible directions path = [] -# Loop through the exits for the current room + for direction in player.current_room.get_exits(): - # Have the player travel in the given directions player.travel(direction) -# Check to see if the current room is already in the `visited` array if player.current_room.id in visited: - # If it is, move the player in the opposite direction, that room has already been visited so we've already checked all possible directions player.travel(opposite_directions[direction]) -# If it is not else: - # Add the current room to the `visited` array visited.append(player.current_room.id) -# Add that direction to the path array path.append(direction) -# Let the path equal the current path plus the path we just created path = path + traverse_map(player.current_room.id, visited) -# Have the player travel in the opposite direction of the path player.travel(opposite_directions[direction]) -# Add the opposite directions to the path array path.append(opposite_directions[direction]) - # finally, return the path return path From f4255d9b8853c1b03887b6f65fadd1f1fe13a6ee Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Tue, 1 Dec 2020 11:24:37 -0800 Subject: [PATCH 07/11] Push for review --- adv.py | 95 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/adv.py b/adv.py index c16968c73..30520aa9d 100644 --- a/adv.py +++ b/adv.py @@ -20,6 +20,7 @@ # Loads the map into a dictionary room_graph=literal_eval(open(map_file, "r").read()) world.load_graph(room_graph) +map_dict = {} # Print an ASCII map world.print_rooms() @@ -33,58 +34,58 @@ # Pick a random unexplored direction from current room, travel and log that direction, then loop. When reach dead end, walk back and find nearest room that is unexplored. -def traversal(starting_room=player.current_room): - exits = player.current_room.get_exits() - currRoom = player.current_room.id +# def traversal(starting_room=player.current_room): +# exits = player.current_room.get_exits() +# currRoom = player.current_room.id - visited = set() - stack = deque() - stack.append([starting_room]) +# visited = set() +# stack = deque() +# stack.append([starting_room]) - for direction in exits: - if currRoom in visited: - print(visited) +# for direction in exits: +# if currRoom in visited: +# print(visited) # Return a list containing the shortest path from starting_vertex to destination_vertex in breath-first order. -# def bfs(self, starting_vertex = currRoom, destination_vertex = ?): -# visited_rooms = set() -# queue = deque() -# queue.append ([currRoom]) - -# while len(queue) > 0: -# currPath = queue.popleft() -# currNode = currPath[-1] - -# if currNode is ?: -# return currPath - -# if currNode not in visited_rooms: -# visited_rooms.add(currNode) -# for neighbor in self.visited_rooms: -# newPath = list(currPath) -# newPath.append(neighbor) -# queue.append(newPath) -# return [] - -def traverse_map(starting_room, visited=[]): - path = [] - - for direction in player.current_room.get_exits(): - player.travel(direction) - if player.current_room.id in visited: - player.travel(opposite_directions[direction]) - else: - visited.append(player.current_room.id) - path.append(direction) - path = path + traverse_map(player.current_room.id, visited) - player.travel(opposite_directions[direction]) - path.append(opposite_directions[direction]) - - return path - - -traversal_path = traverse_map(player.current_room.id) +def bfs(self, starting_room, destination_vertex = "?"): + visited_rooms = set() + queue = deque() + queue.append ([starting_room]) + + while len(queue) > 0: + currPath = queue.popleft() + currNode = currPath[-1] + visited_rooms.add(currNode) + + for direction in map_dict[currNode]: + if currNode == "?": + return currPath + if currNode not in visited_rooms: + for neighbor in self.visited_rooms: + newPath = list(currPath) + newPath.append(neighbor) + queue.append(newPath) + return [] + +# def traverse_map(starting_room, visited=[]): +# path = [] + +# for direction in player.current_room.get_exits(): +# player.travel(direction) +# if player.current_room.id in visited: +# player.travel(opposite_directions[direction]) +# else: +# visited.append(player.current_room.id) +# path.append(direction) +# path = path + traverse_map(player.current_room.id, visited) +# player.travel(opposite_directions[direction]) +# path.append(opposite_directions[direction]) + +# return path + + +# traversal_path = traverse_map(player.current_room.id) # TRAVERSAL TEST - DO NOT MODIFY visited_rooms = set() From 725497f9a566fb967931e70e365cf8b7d2fa76d1 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Tue, 1 Dec 2020 11:31:10 -0800 Subject: [PATCH 08/11] psuedo code --- adv.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/adv.py b/adv.py index 30520aa9d..662592692 100644 --- a/adv.py +++ b/adv.py @@ -68,6 +68,48 @@ def bfs(self, starting_room, destination_vertex = "?"): queue.append(newPath) return [] +# using dft to create the maze +def dft(starting_room): + # reverse the directions + reverse_directions = {'n': 's', 's': 'n', 'e': 'w', 'w': 'e'} + # create counter for rooms the player has been to + # while the len of mapDictionary is not equal to the len of the graph... + # see the room currently in + # find the room id of the current room + # create a dict for the rooms + # check if the room id is not in the mapDictionary... + # then find the possible exits + # add the '?' into the room_dict + # update the room + if traversal_path: + # find the previous room + # add the previous room to the room counter and the room_dict + # add the unexplored rooms to the room id + # otherwise... + # add the room id from mapDictionary to the inner room dict + # create an empyt list of possible exits + # iterate throuogh the room dict + # check if '?' is at the index direction... + # if so, add the direction to the possible exits list + # check if there is an unknown direction... + # use random and shuffle the possible exits + # set the direction to the zero index of possible exits + # add the direction to the traversal path + # to move the player in that direction, use travel function + # Grab player's current room + # set mapDictionary current room id and direction to player room id + # now set the current room id to the counter + # otherwise... + else: + # going to use bfs to search for next exit or possible rooms + # check if the path of the next room is not None and if len of next room not None... + # find index in the range of the len on the next room - 1 (not include current room)... + # find direction in mapDictionary of index of next room + # check if the mapDictionary's index of next room and direction is equal to the index + 1 of next room + # if so, then add the direction to traversal path + # then move the player to that room + # otherwise, break + # def traverse_map(starting_room, visited=[]): # path = [] From 4399503e83cedf86ea4ce761724bee51c16ad160 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Tue, 1 Dec 2020 17:36:11 -0800 Subject: [PATCH 09/11] Working on dft of code. --- adv.py | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/adv.py b/adv.py index 662592692..a45684129 100644 --- a/adv.py +++ b/adv.py @@ -46,6 +46,25 @@ # if currRoom in visited: # print(visited) +# def traverse_map(starting_room, visited=[]): +# path = [] + +# for direction in player.current_room.get_exits(): +# player.travel(direction) +# if player.current_room.id in visited: +# player.travel(opposite_directions[direction]) +# else: +# visited.append(player.current_room.id) +# path.append(direction) +# path = path + traverse_map(player.current_room.id, visited) +# player.travel(opposite_directions[direction]) +# path.append(opposite_directions[direction]) + +# return path + + +# traversal_path = traverse_map(player.current_room.id) + # Return a list containing the shortest path from starting_vertex to destination_vertex in breath-first order. def bfs(self, starting_room, destination_vertex = "?"): @@ -73,13 +92,25 @@ def dft(starting_room): # reverse the directions reverse_directions = {'n': 's', 's': 'n', 'e': 'w', 'w': 'e'} # create counter for rooms the player has been to + visited = set() + stack = deque() + stack.append([starting_room]) + exits = player.current_room.get_exits() + # while the len of mapDictionary is not equal to the len of the graph... + while len(map_dict) > len(room_graph): # see the room currently in + currRoom = stack.pop() # find the room id of the current room # create a dict for the rooms + visited_rooms = {} + # check if the room id is not in the mapDictionary... + if player.current_room.id is not in visited_rooms: # then find the possible exits + if exits = '?': # add the '?' into the room_dict + visited_rooms.append(exits) # update the room if traversal_path: # find the previous room @@ -110,25 +141,6 @@ def dft(starting_room): # then move the player to that room # otherwise, break -# def traverse_map(starting_room, visited=[]): -# path = [] - -# for direction in player.current_room.get_exits(): -# player.travel(direction) -# if player.current_room.id in visited: -# player.travel(opposite_directions[direction]) -# else: -# visited.append(player.current_room.id) -# path.append(direction) -# path = path + traverse_map(player.current_room.id, visited) -# player.travel(opposite_directions[direction]) -# path.append(opposite_directions[direction]) - -# return path - - -# traversal_path = traverse_map(player.current_room.id) - # TRAVERSAL TEST - DO NOT MODIFY visited_rooms = set() player.current_room = world.starting_room From aae51e0cedc358931a37a63c7cb06a6e75931662 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Tue, 1 Dec 2020 18:28:48 -0800 Subject: [PATCH 10/11] Saving progress --- adv.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adv.py b/adv.py index a45684129..f50f9363a 100644 --- a/adv.py +++ b/adv.py @@ -110,8 +110,9 @@ def dft(starting_room): # then find the possible exits if exits = '?': # add the '?' into the room_dict - visited_rooms.append(exits) + visited_rooms.add(exits) # update the room + if traversal_path: # find the previous room # add the previous room to the room counter and the room_dict From 8e4a498ae6dcddc335c69441c9e135f9ce353883 Mon Sep 17 00:00:00 2001 From: Veto Ramirez Date: Mon, 14 Dec 2020 20:52:01 -0800 Subject: [PATCH 11/11] Looking at code for errors --- adv.py | 509 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 436 insertions(+), 73 deletions(-) diff --git a/adv.py b/adv.py index f50f9363a..4fcc6d3e1 100644 --- a/adv.py +++ b/adv.py @@ -1,148 +1,509 @@ -from room import Room -from player import Player -from world import World +# from room import Room +# from player import Player +# from world import World + +# import random +# from collections import deque +# from ast import literal_eval + +# # Load world +# world = World() + +# 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) + +# # 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/main_maze.txt" + +# # Loads the map into a dictionary +# room_graph=literal_eval(open(map_file, "r").read()) +# world.load_graph(room_graph) +# mapDictionary = {} + +# # Print an ASCII map +# world.print_rooms() + +# player = Player(world.starting_room) + +# # Fill this out with directions to walk +# # traversal_path = ['n', 'n'] +# traversal_path = [] +# # opposite_directions = {'n': 's', 's': 'n', 'w': 'e', 'e': 'w'} + +# # Pick a random unexplored direction from current room, travel and log that direction, then loop. When reach dead end, walk back and find nearest room that is unexplored. + +# # def traversal(starting_room=player.current_room): +# # exits = player.current_room.get_exits() +# # current_room = player.current_room.id + +# # visited = set() +# # stack = deque() +# # stack.append([starting_room]) + +# # for direction in exits: +# # if current_room in visited: +# # print(visited) + +# # def traverse_map(starting_room, visited=[]): +# # path = [] + +# # for direction in player.current_room.get_exits(): +# # player.travel(direction) +# # if player.current_room.id in visited: +# # player.travel(opposite_directions[direction]) +# # else: +# # visited.append(player.current_room.id) +# # path.append(direction) +# # path = path + traverse_map(player.current_room.id, visited) +# # player.travel(opposite_directions[direction]) +# # path.append(opposite_directions[direction]) + +# # return path + + +# # traversal_path = traverse_map(player.current_room.id) + +# # Return a list containing the shortest path from starting_vertex to destination_vertex in breath-first order. + +# # def bfs(self, starting_room): +# # visited_rooms = set() +# # queue = Queue() +# # queue.enqueue([starting_room]) + +# # while queue.size() > 0: +# # currPath = queue.dequeue() +# # currNode = currPath[-1] +# # visited_rooms.add(currNode) + +# # for direction in mapDictionary[currNode]: +# # if mapDictionary[currNode][direction] == "?": +# # return currPath +# # if mapDictionary[currNode][direction] not in visited_rooms: +# # newPath = list(currPath) +# # newPath.append(mapDictionary[currNode][direction]) +# # queue.enqueue(newPath) + +# def bfs(starting_room): +# visited_rooms = set() +# queue = Queue() +# queue.enqueue([starting_room]) +# while queue.size() > 0: +# currPath = queue.dequeue() +# currNode = currPath[-1] +# visited_rooms.add(currNode) +# for direction in mapDictionary[currNode]: +# if mapDictionary[currNode][direction] == "?": +# return currPath +# if mapDictionary[currNode][direction] not in visited_rooms: +# newPath = list(currPath) +# newPath.append(mapDictionary[currNode][direction]) +# queue.enqueue(newPath) + +# # using dft to create the maze +# # def dft(starting_room): +# # # reverse the directions +# # reverse_directions = {'n': 's', 's': 'n', 'e': 'w', 'w': 'e'} +# # # create counter for rooms the player has been to +# # # visited = set() +# # # stack = deque() +# # # stack.append([starting_room]) +# # # exits = player.current_room.get_exits() +# # visited = 0 + +# # # while the len of mapDictionary is not equal to the len of the graph... +# # while len(mapDictionary) is not len(room_graph): +# # # see the room currently in +# # current_room = player.current_room +# # # find the room id of the current room +# # room_id = current_room.id +# # # create a dict for the rooms +# # visited_rooms = {} + +# # # check if the room id is not in the mapDictionary... +# # if room_id not in mapDictionary: +# # # then find the possible exits +# # for i in current_room.get_exits(): +# # # add the '?' into the visited_rooms +# # visited_rooms[i] = '?' +# # # update the room +# # if traversal_path: +# # # find the previous room +# # prev_room = reverse_directions[traversal_path[-1]] +# # # add the previous room to the room counter and the visited_rooms +# # visited_rooms[prev_room] = visited +# # # add the unexplored rooms to the room id +# # mapDictionary[room_id]= visited_rooms +# # # otherwise... +# # else: +# # # add the room id from mapDictionary to the inner room dict +# # visited_rooms = mapDictionary[room_id] +# # # create an empyt list of possible exits +# # possible_exits = list() +# # # iterate throuogh the room dict +# # for i in visited_rooms: +# # # check if '?' is at the index direction... +# # if visited_rooms[i] == '?': +# # # if so, add the direction to the possible exits list +# # possible_exits.append(i) +# # # check if there is an unknown direction... +# # if len(possible_exits) != 0: +# # # use random and shuffle the possible exits +# # random.shuffle(possible_exits) +# # # set the direction to the zero index of possible exits +# # direction = possible_exits[0] +# # # add the direction to the traversal path +# # traversal_path.append(direction) +# # # to move the player in that direction, use travel function +# # player.travel(direction) +# # # Grab player's current room +# # player_room = current_room +# # # set mapDictionary current room id and direction to player room id +# # mapDictionary[current_room.id][direction] = player_room.id +# # # now set the current room id to the counter +# # visited = current_room.id +# # # otherwise... +# # else: +# # # going to use bfs to search for next exit or possible rooms +# # next_room = bfs(room_id) +# # # check if the path of the next room is not None and if len of next room not None... +# # if next_room != None and len(next_room) > 0: +# # # find index in the range of the len on the next room - 1 (not include current room)... +# # for i in range(len(next_room) - 1): +# # # find direction in mapDictionary of index of next room +# # for direction in mapDictionary[next_room[i]]: +# # # check if the mapDictionary's index of next room and direction is equal to the index + 1 of next room +# # if mapDictionary[next_room[i]][direction] is next_room[i + 1]: +# # # if so, then add the direction to traversal path +# # traversal_path.append(direction) +# # # then move the player to that room +# # player.travel(direction) +# # # otherwise, break +# # else: +# # break + + + +# # TRAVERSAL TEST - DO NOT MODIFY +# 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) + +# if len(visited_rooms) == len(room_graph): +# print(f"TESTS PASSED: {len(traversal_path)} moves, {len(visited_rooms)} rooms visited") +# else: +# 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.") import random -from collections import deque from ast import literal_eval +from player import Player +from room import Room +# from util import Queue, Stack +from world import World + + +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) + + # Load world world = World() - # You may uncomment the smaller graphs for development and testing purposes. -map_file = "maps/test_line.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" +map_file = "maps/main_maze.txt" # Loads the map into a dictionary -room_graph=literal_eval(open(map_file, "r").read()) +room_graph = literal_eval(open(map_file, "r").read()) world.load_graph(room_graph) -map_dict = {} # Print an ASCII map world.print_rooms() player = Player(world.starting_room) +# Notes from README: +# Start by writing an algorithm that picks a random unexplored direction from the player's current room, travels and logs that direction, then loops. This should cause your player to walk a depth-first traversal. +# You can find the path to the shortest unexplored room by using a breadth-first search for a room with a `'?'` for an exit. If you use the `bfs` code from the homework, you will need to make a few modifications. +# 1. Instead of searching for a target vertex, you are searching for an exit with a `'?'` as the value. If an exit has been explored, you can put it in your BFS queue like normal. +# 2. BFS will return the path as a list of room IDs. You will need to convert this to a list of n/s/e/w directions before you can add it to your traversal path. + # Fill this out with directions to walk # traversal_path = ['n', 'n'] -traversal_path = ['n', 's', 'w', 'e'] -opposite_directions = {'n': 's', 's': 'n', 'w': 'e', 'e': 'w'} +traversal_path = [] -# Pick a random unexplored direction from current room, travel and log that direction, then loop. When reach dead end, walk back and find nearest room that is unexplored. +# Graph as a dict +mapDictionary = {} -# def traversal(starting_room=player.current_room): -# exits = player.current_room.get_exits() -# currRoom = player.current_room.id +# trying with bfs to search for shortest path +# def bfs(starting_room_id): +# # Create an empty queue +# q = Queue() +# # Add a path to the starting room to the queue +# q.enqueue([starting_room_id]) +# # Create an empty set to store visited # visited = set() -# stack = deque() -# stack.append([starting_room]) +# # While the queue is not empty... +# while q.size() > 0: +# # Dequeue the first path +# path = q.dequeue() +# # Grab the last room from the path +# current_room = path[-1] +# # add current_room to visited +# visited.add(current_room) +# # For each direction in the map graph's current room... +# for direction in mapDictionary[current_room]: +# # check if the current room's direction is a '?'... +# if mapDictionary[current_room][direction] is '?': +# # if it is, return path +# return path +# # check else if the current room's direction is not visited... +# if mapDictionary[current_room][direction] not in visited: +# # if not, create a new path, add the direction +# new_path = list(path) +# new_path.append(mapDictionary[current_room][direction]) +# q.enqueue(new_path) -# for direction in exits: -# if currRoom in visited: -# print(visited) -# def traverse_map(starting_room, visited=[]): -# path = [] - -# for direction in player.current_room.get_exits(): -# player.travel(direction) -# if player.current_room.id in visited: -# player.travel(opposite_directions[direction]) +# # using dft to create the maze +# def dft(starting_room): +# # reverse the directions +# reverse_directions = {'n': 's', 's': 'n', 'e': 'w', 'w': 'e'} +# # create counter for rooms the player has been to +# counter = 0 +# # while the len of mapDictionary is not equal to the len of the graph... +# while len(mapDictionary) is not len(room_graph): +# # see the room currently in +# current_room = player.current_room +# # find the room id of the current room +# room_id = current_room.id +# # create a dict for the rooms +# room_dict = {} +# # check if the room id is not in the mapDictionary... +# if room_id not in mapDictionary: +# # then find the possible exits +# for i in current_room.get_exits(): +# # add the '?' into the room_dict +# room_dict[i] = '?' +# # update the room +# if traversal_path: +# # find the previous room +# prevRoom = reverse_directions[traversal_path[-1]] +# # add the previous room to the room counter and the room_dict +# room_dict[prevRoom] = counter +# # add the unexplored rooms to the room id +# mapDictionary[room_id] = room_dict +# # otherwise... # else: -# visited.append(player.current_room.id) -# path.append(direction) -# path = path + traverse_map(player.current_room.id, visited) -# player.travel(opposite_directions[direction]) -# path.append(opposite_directions[direction]) - -# return path - - -# traversal_path = traverse_map(player.current_room.id) +# # add the room id from mapDictionary to the inner room dict +# room_dict = mapDictionary[room_id] +# # create an empyt list of possible exits +# possible_exits = list() +# # iterate throuogh the room dict +# for direction in room_dict: +# # check if '?' is at the index direction... +# if room_dict[direction] is '?': +# # if so, add the direction to the possible exits list +# possible_exits.append(direction) +# # check if there is an unknown direction... +# if len(possible_exits) is not 0: +# # use random and shuffle the possible exits +# random.shuffle(possible_exits) +# # set the direction to the zero index of possible exits +# direction = possible_exits[0] +# # add the direction to the traversal path +# traversal_path.append(direction) +# # to move the player in that direction, use travel function +# player.travel(direction) +# # Grab player's current room +# player_room = player.current_room +# # set mapDictionary current room id and direction to player room id +# mapDictionary[current_room.id][direction] = player_room.id +# # now set the current room id to the counter +# counter = current_room.id +# # otherwise... +# else: +# # going to use bfs to search for next exit or possible rooms +# next_room = bfs(room_id) +# # check if the path of the next room is not None and if len of next room not None... +# if next_room is not None and len(next_room) > 0: +# # find index in the range of the len on the next room - 1 (not include current room)... +# for i in range(len(next_room) - 1): +# # find direction in mapDictionary of index of next room +# for direction in mapDictionary[next_room[i]]: +# # check if the mapDictionary's index of next room and direction is equal to the index + 1 of next room +# if mapDictionary[next_room[i]][direction] is next_room[i + 1]: +# # if so, then add the direction to traversal path +# traversal_path.append(direction) +# # then move the player to that room +# player.travel(direction) +# # otherwise, break +# else: +# break -# Return a list containing the shortest path from starting_vertex to destination_vertex in breath-first order. +############################################## -def bfs(self, starting_room, destination_vertex = "?"): +def bfs(starting_room): visited_rooms = set() - queue = deque() - queue.append ([starting_room]) - - while len(queue) > 0: - currPath = queue.popleft() + queue = Queue() + queue.enqueue([starting_room]) + while queue.size() > 0: + currPath = queue.dequeue() currNode = currPath[-1] visited_rooms.add(currNode) - - for direction in map_dict[currNode]: - if currNode == "?": + for direction in mapDictionary[currNode]: + if mapDictionary[currNode][direction] == "?": return currPath - if currNode not in visited_rooms: - for neighbor in self.visited_rooms: - newPath = list(currPath) - newPath.append(neighbor) - queue.append(newPath) - return [] - -# using dft to create the maze + if mapDictionary[currNode][direction] not in visited_rooms: + newPath = list(currPath) + newPath.append(mapDictionary[currNode][direction]) + queue.enqueue(newPath) + + def dft(starting_room): # reverse the directions reverse_directions = {'n': 's', 's': 'n', 'e': 'w', 'w': 'e'} # create counter for rooms the player has been to - visited = set() - stack = deque() - stack.append([starting_room]) - exits = player.current_room.get_exits() - + # visited = set() + # stack = deque() + # stack.append([starting_room]) + # exits = player.current_room.get_exits() + visited = 0 # while the len of mapDictionary is not equal to the len of the graph... - while len(map_dict) > len(room_graph): + while len(mapDictionary) is not len(room_graph): # see the room currently in - currRoom = stack.pop() + current_room = player.current_room # find the room id of the current room + room_id = current_room.id # create a dict for the rooms visited_rooms = {} - # check if the room id is not in the mapDictionary... - if player.current_room.id is not in visited_rooms: + if room_id not in mapDictionary: # then find the possible exits - if exits = '?': - # add the '?' into the room_dict - visited_rooms.add(exits) + for i in current_room.get_exits(): + # add the '?' into the visited_rooms + visited_rooms[i] = '?' # update the room - if traversal_path: # find the previous room - # add the previous room to the room counter and the room_dict + prev_room = reverse_directions[traversal_path[-1]] + # add the previous room to the room counter and the visited_rooms + visited_rooms[prev_room] = visited # add the unexplored rooms to the room id + mapDictionary[room_id] = visited_rooms # otherwise... + else: # add the room id from mapDictionary to the inner room dict + visited_rooms = mapDictionary[room_id] # create an empyt list of possible exits + possible_exits = list() # iterate throuogh the room dict + for i in visited_rooms: # check if '?' is at the index direction... + if visited_rooms[i] == '?': # if so, add the direction to the possible exits list + possible_exits.append(i) # check if there is an unknown direction... + if len(possible_exits) != 0: # use random and shuffle the possible exits + random.shuffle(possible_exits) # set the direction to the zero index of possible exits + direction = possible_exits[0] # add the direction to the traversal path + traversal_path.append(direction) # to move the player in that direction, use travel function + player.travel(direction) # Grab player's current room + player_room = current_room # set mapDictionary current room id and direction to player room id + mapDictionary[current_room.id][direction] = player_room.id # now set the current room id to the counter + visited = current_room.id # otherwise... else: # going to use bfs to search for next exit or possible rooms + next_room = bfs(room_id) # check if the path of the next room is not None and if len of next room not None... + if next_room != None and len(next_room) > 0: # find index in the range of the len on the next room - 1 (not include current room)... + for i in range(len(next_room) - 1): # find direction in mapDictionary of index of next room + for direction in mapDictionary[next_room[i]]: # check if the mapDictionary's index of next room and direction is equal to the index + 1 of next room + if mapDictionary[next_room[i]][direction] is next_room[i + 1]: # if so, then add the direction to traversal path + traversal_path.append(direction) # then move the player to that room + player.travel(direction) # otherwise, break + else: + break + + +##################################### -# TRAVERSAL TEST - DO NOT MODIFY + +# call dft room_graph, otherwise get INCOMPLETE TRAVERSAL error +dft(room_graph) +print("----") +print(f"MAP DICTIONARY: {mapDictionary}") +print("----") +print(f"TRAVERSAL PATH: {traversal_path}") +print("----") + + +# TRAVERSAL TEST visited_rooms = set() player.current_room = world.starting_room visited_rooms.add(player.current_room) @@ -152,11 +513,13 @@ def dft(starting_room): visited_rooms.add(player.current_room) if len(visited_rooms) == len(room_graph): - print(f"TESTS PASSED: {len(traversal_path)} moves, {len(visited_rooms)} rooms visited") + print( + f"TESTS PASSED: {len(traversal_path)} moves, {len(visited_rooms)} rooms visited") + print("----") else: print("TESTS FAILED: INCOMPLETE TRAVERSAL") print(f"{len(room_graph) - len(visited_rooms)} unvisited rooms") - + print("----") ####### @@ -170,4 +533,4 @@ def dft(starting_room): # elif cmds[0] == "q": # break # else: -# print("I did not understand that command.") +# print("I did not understand that command.") \ No newline at end of file