diff --git a/projects/adventure/adv.py b/projects/adventure/adv.py index 8bc540b5e..b455b4127 100644 --- a/projects/adventure/adv.py +++ b/projects/adventure/adv.py @@ -29,12 +29,98 @@ # traversal_path = ['n', 'n'] traversal_path = [] - - # TRAVERSAL TEST visited_rooms = set() player.current_room = world.starting_room visited_rooms.add(player.current_room) +#MY_CODE############################################################################################################################################################################################################### +''' +roomDirections....keeps track of visited rooms and their none traveld directions + +rooms.............temporarily holds a given nodes directions + +opposite..........dictionary used to get the oposite of a given direction +''' +roomDirections = {} +rooms = {} +opposite = {'n':'s', 's':'n', 'e':'w', 'w': 'e'} + +#less wordy +currRoomExits = player.current_room.get_exits() +curRoomId = player.current_room.id + +#adds starting room to rooms dictionary +for i in currRoomExits: + roomDirections[i] = player.current_room.get_room_in_direction(i).id +rooms[player.current_room.id] = roomDirections + +#until all 500 rooms have been visited +while len(rooms) != 500: + ''' + bug... it was traveling twice in one loop and I was tired + so this was just the quickest solution at the time. It's fine. + ''' + bug = 0 + + #updates less wordy varibles + currRoomExits = player.current_room.get_exits() + curRoomId = player.current_room.id + + #resets roomDirections and travel direction for new room + roomDirections = {} + travelDirection = 0 + + if curRoomId not in rooms: + #adds current rooms original directions to roomDirections + for direction in currRoomExits: + roomDirections[direction] = player.current_room.get_room_in_direction(direction).id + + #adds roomDirections to it's room in rooms + rooms[curRoomId] = roomDirections + + #sets the opposite direction of the last known direction on the current node to 'origin' + rooms[curRoomId][opposite[traversal_path[-1]]] = 'origin' + + + #if there is more than one direction + if len(rooms[curRoomId]) != 1: + #if you've already traveled the road or it's your way back then try the next travel direction + while currRoomExits[travelDirection] not in rooms[curRoomId] or rooms[curRoomId][currRoomExits[travelDirection]] == 'origin': + ''' + travelDirection...a number used to access the original directions list of the current node. + if rooms says you can't travel a road you just up the number to change directions. + ''' + travelDirection = travelDirection +1 + + + #makes sure the road you want to travel to has not been traveled in this direction and is not an origin road so you don't cut yourself off + if currRoomExits[travelDirection] in rooms[curRoomId] and rooms[curRoomId][currRoomExits[travelDirection]] != 'origin': + #before you travel remove this road from currentRoom so you can't travel it again + rooms[curRoomId].pop(currRoomExits[travelDirection]) + + #traveling... + player.travel(currRoomExits[travelDirection]) + traversal_path.append(currRoomExits[travelDirection]) + + #here that bug from earlier lol this let's us know traveling has already happened this loop + bug += 1 + + #This checks if all that's left is to go back then it goes back to the last known node with more than one exit + if len(rooms[curRoomId]) == 1 and bug == 0: + + #same princible as line 88 + while currRoomExits[travelDirection] not in rooms[curRoomId]: + travelDirection +=1 + + #traveling... + rooms[curRoomId].pop(currRoomExits[travelDirection]) + player.travel(currRoomExits[travelDirection]) + traversal_path.append(currRoomExits[travelDirection]) + +#resets for test +player.current_room = world.starting_room +visited_rooms.add(player.current_room) +###################################################################################################################################################################################################################### for move in traversal_path: player.travel(move) @@ -45,18 +131,69 @@ else: print("TESTS FAILED: INCOMPLETE TRAVERSAL") print(f"{len(room_graph) - len(visited_rooms)} unvisited rooms") + print(traversal_path) - - +''' ####### # UNCOMMENT TO WALK AROUND ####### +roomDirections = {} +rooms = {} +opposite = {'n':'s', 's':'n', 'e':'w', 'w': 'e'} + +#prints current room basic info player.current_room.print_room_description(player) + +#adds starting room to rooms +if player.current_room.id not in rooms: + for i in player.current_room.get_exits(): + roomDirections[i] = player.current_room.get_room_in_direction(i).id + rooms[player.current_room.id] = roomDirections + + +#original testing code#################################################################### while True: + #resets roomDirections + roomDirections = {} + + #takes input direction (not important) cmds = input("-> ").lower().split(" ") - if cmds[0] in ["n", "s", "e", "w"]: + + + if cmds[0] in ["n", "s", "e", "w"] and cmds[0] in rooms[player.current_room.id]: + #travels + rooms[player.current_room.id].pop(cmds[0]) + player.travel(cmds[0], True) + + + #addes to rooms + if player.current_room.id not in rooms: + #adds to roomDirections + for i in player.current_room.get_exits(): + roomDirections[i] = player.current_room.get_room_in_direction(i).id + + rooms[player.current_room.id] = roomDirections + + rooms[player.current_room.id][opposite[cmds[0]]] = 'origin' + + + + #quits game (not important) elif cmds[0] == "q": break + #make sure its nsew (not important) else: print("I did not understand that command.") + + + + #TEST PRINTS + print('current',roomDirections) + for i in rooms: + print(i, rooms[i]) + print(player.current_room.get_exits()[0]) +''' + + + diff --git a/projects/adventure/player.py b/projects/adventure/player.py index b43e14091..226b7d392 100644 --- a/projects/adventure/player.py +++ b/projects/adventure/player.py @@ -8,4 +8,4 @@ def travel(self, direction, show_rooms = False): if (show_rooms): next_room.print_room_description(self) else: - print("You cannot move in that direction.") + print("You cannot move in that direction.", direction, self.current_room.id) diff --git a/projects/graph/graph.py b/projects/graph/graph.py index 59fecae4b..8f7f62e88 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -13,42 +13,81 @@ def add_vertex(self, vertex_id): """ Add a vertex to the graph. """ - pass # TODO + self.vertices[vertex_id] = set() def add_edge(self, v1, v2): """ Add a directed edge to the graph. """ - pass # TODO + self.vertices[v1].add(v2) def get_neighbors(self, vertex_id): """ Get all neighbors (edges) of a vertex. """ - pass # TODO + return self.vertices[vertex_id] def bft(self, starting_vertex): """ Print each vertex in breadth-first order beginning from starting_vertex. """ - pass # TODO + print('-----------------------------BFT----------------------------------') + que = Queue() + que.enqueue(starting_vertex) + visited = set() + while que.size() != 0 : + current = que.dequeue() + + if current not in visited: + visited.add(current) + print(current) + + for edge in self.vertices[current]: + if edge not in visited: + que.enqueue(edge) + def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ - pass # TODO + print('-----------------------------DFT----------------------------------') + stack = Stack() + stack.push(starting_vertex) + visited = set() + while stack.size() != 0 : + current = stack.pop() + + if current not in visited: + visited.add(current) + print(current) - def dft_recursive(self, starting_vertex): + for edge in self.vertices[current]: + if edge not in visited: + stack.push(edge) + + def dft_recursive(self, starting_vertex, visited=None, stack = Stack()): """ Print each vertex in depth-first order beginning from starting_vertex. This should be done using recursion. """ - pass # TODO + if visited == None: + print('------------------------------DFTR---------------------------------') + + visited = set() + if starting_vertex not in visited: + visited.add(starting_vertex) + + print(starting_vertex) + for edge in self.vertices[starting_vertex]: + if edge not in visited: + stack.push(edge) + + self.dft_recursive(stack.pop(), visited, stack) def bfs(self, starting_vertex, destination_vertex): """ @@ -56,17 +95,56 @@ def bfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in breath-first order. """ - pass # TODO + print('------------------------------BFS---------------------------------') + que = Queue() + que.enqueue([starting_vertex]) + visited = set() + + while que.size() != 0 : + path = que.dequeue() + current = path[-1] + + if current not in visited: + visited.add(current) + + for edge in self.vertices[current]: + new_path = list(path) + new_path.append(edge) + + if new_path[-1] == destination_vertex: + return new_path + + que.enqueue(new_path) + def dfs(self, starting_vertex, destination_vertex): """ Return a list containing a path from starting_vertex to destination_vertex in depth-first order. """ - pass # TODO + print('----------------------------DFS-----------------------------------') + stack = Stack() + stack.push([starting_vertex]) + visited = set() + + while stack.size() != 0 : + path = stack.pop() + current = path[-1] - def dfs_recursive(self, starting_vertex, destination_vertex): + if current not in visited: + visited.add(current) + + for edge in self.vertices[current]: + new_path = list(path) + new_path.append(edge) + + if new_path[-1] == destination_vertex: + return new_path + + stack.push(new_path) + + def dfs_recursive(self, starting_vertex, destination_vertex,visited=None, stack = Stack()): """ Return a list containing a path from starting_vertex to destination_vertex in @@ -74,7 +152,30 @@ def dfs_recursive(self, starting_vertex, destination_vertex): This should be done using recursion. """ - pass # TODO + + if visited == None: + print('-----------------------------DFSR----------------------------------') + visited = set() + stack.push([starting_vertex]) + + path = stack.pop() + + if starting_vertex not in visited: + visited.add(starting_vertex) + + for edge in self.vertices[starting_vertex]: + if edge not in visited: + new_path = list(path) + new_path.append(edge) + + if new_path[-1] == destination_vertex: + return new_path + + + stack.push(new_path) + + return self.dfs_recursive(new_path[-1], destination_vertex, visited, stack) + if __name__ == '__main__': graph = Graph() # Instantiate your graph