diff --git a/adv.py b/adv.py index 84f031836..785f539bf 100644 --- a/adv.py +++ b/adv.py @@ -9,6 +9,23 @@ world = World() +class Stack: + def __init__(self): + self.stack = [] # empty list + + 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) + + # You may uncomment the smaller graphs for development and testing purposes. # map_file = "maps/test_line.txt" # map_file = "maps/test_cross.txt" @@ -17,7 +34,7 @@ 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) # Print an ASCII map @@ -30,6 +47,53 @@ traversal_path = [] +def travelers_path(direction): + # save our route back to unvisited exits + if direction == "n": + return "s" + elif direction == "s": + return "n" + elif direction == "e": + return "w" + elif direction == "w": + return "e" + + +paths = Stack() +visited = set() +# comparing visited to len of rooms to ensure a complete traversal +while len(visited) < len(world.rooms): + + exits = player.current_room.get_exits() + print("Room:", player.current_room) + print("exits are", exits) + path = [] # empty list for traversal path + + # loop through the available exits for the player based on the current room + for exit in exits: + # check if exit is valid and the room to travel to has not been visited yet + if ( + exit is not None + and player.current_room.get_room_in_direction(exit) not in visited + ): + # if exit exists and we haven't visited + path.append(exit) # append exit to path + print(path, "<~ path") + visited.add(player.current_room) # add current room to visited set + if len(path) > 0: + move = random.randint(0, len(path) - 1) # pick index of move (1 of up to 4) + paths.push(path[move]) # push room to move to onto paths stack + player.travel(path[move]) # travel to new room + traversal_path.append(path[move]) # add new room to traversed path + print("more rooms to explore") + else: + # if no more rooms exist in path + end = paths.pop() # returns and removes last element in paths Stack + player.travel(travelers_path(end)) + traversal_path.append( + travelers_path(end) + ) # appends the last element from the paths Stack to traversed path + print("this is the end of this path") # TRAVERSAL TEST - DO NOT MODIFY visited_rooms = set() @@ -41,13 +105,13 @@ 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" + ) else: print("TESTS FAILED: INCOMPLETE TRAVERSAL") print(f"{len(room_graph) - len(visited_rooms)} unvisited rooms") - - ####### # UNCOMMENT TO WALK AROUND ####### diff --git a/player.py b/player.py index b43e14091..299ebfab8 100644 --- a/player.py +++ b/player.py @@ -1,11 +1,12 @@ class Player: def __init__(self, starting_room): self.current_room = starting_room - def travel(self, direction, show_rooms = False): + + def travel(self, direction, show_rooms=False): next_room = self.current_room.get_room_in_direction(direction) if next_room is not None: self.current_room = next_room - if (show_rooms): + if show_rooms: next_room.print_room_description(self) else: print("You cannot move in that direction.") diff --git a/room.py b/room.py index 83ed04089..47b550380 100644 --- a/room.py +++ b/room.py @@ -11,10 +11,13 @@ def __init__(self, name, description, id=0, x=None, y=None): self.w_to = None self.x = x self.y = y + def __str__(self): return f"\n-------------------\n\n{self.name}\n\n {self.description}\n\n{self.get_exits_string()}\n" + def print_room_description(self, player): print(str(self)) + def get_exits(self): exits = [] if self.n_to is not None: @@ -26,8 +29,10 @@ def get_exits(self): if self.e_to is not None: exits.append("e") return exits + def get_exits_string(self): return f"Exits: [{', '.join(self.get_exits())}]" + def connect_rooms(self, direction, connecting_room): if direction == "n": self.n_to = connecting_room @@ -44,6 +49,7 @@ def connect_rooms(self, direction, connecting_room): else: print("INVALID ROOM CONNECTION") return None + def get_room_in_direction(self, direction): if direction == "n": return self.n_to @@ -55,5 +61,6 @@ def get_room_in_direction(self, direction): return self.w_to else: return None + def get_coords(self): return [self.x, self.y] diff --git a/world.py b/world.py index 3ac3de1eb..8413b7b01 100644 --- a/world.py +++ b/world.py @@ -2,12 +2,14 @@ import random import math + class World: def __init__(self): self.starting_room = None self.rooms = {} self.room_grid = [] self.grid_size = 0 + def load_graph(self, room_graph): num_rooms = len(room_graph) rooms = [None] * num_rooms @@ -15,7 +17,13 @@ def load_graph(self, room_graph): for i in range(0, num_rooms): x = room_graph[i][0][0] grid_size = max(grid_size, room_graph[i][0][0], room_graph[i][0][1]) - self.rooms[i] = Room(f"Room {i}", f"({room_graph[i][0][0]},{room_graph[i][0][1]})",i, room_graph[i][0][0], room_graph[i][0][1]) + self.rooms[i] = Room( + f"Room {i}", + f"({room_graph[i][0][0]},{room_graph[i][0][1]})", + i, + room_graph[i][0][0], + room_graph[i][0][1], + ) self.room_grid = [] grid_size += 1 self.grid_size = grid_size @@ -24,14 +32,22 @@ def load_graph(self, room_graph): for room_id in room_graph: room = self.rooms[room_id] self.room_grid[room.x][room.y] = room - if 'n' in room_graph[room_id][1]: - self.rooms[room_id].connect_rooms('n', self.rooms[room_graph[room_id][1]['n']]) - if 's' in room_graph[room_id][1]: - self.rooms[room_id].connect_rooms('s', self.rooms[room_graph[room_id][1]['s']]) - if 'e' in room_graph[room_id][1]: - self.rooms[room_id].connect_rooms('e', self.rooms[room_graph[room_id][1]['e']]) - if 'w' in room_graph[room_id][1]: - self.rooms[room_id].connect_rooms('w', self.rooms[room_graph[room_id][1]['w']]) + if "n" in room_graph[room_id][1]: + self.rooms[room_id].connect_rooms( + "n", self.rooms[room_graph[room_id][1]["n"]] + ) + if "s" in room_graph[room_id][1]: + self.rooms[room_id].connect_rooms( + "s", self.rooms[room_graph[room_id][1]["s"]] + ) + if "e" in room_graph[room_id][1]: + self.rooms[room_id].connect_rooms( + "e", self.rooms[room_graph[room_id][1]["e"]] + ) + if "w" in room_graph[room_id][1]: + self.rooms[room_id].connect_rooms( + "w", self.rooms[room_graph[room_id][1]["w"]] + ) self.starting_room = self.rooms[0] def print_rooms(self): @@ -40,7 +56,9 @@ def print_rooms(self): rotated_room_grid.append([None] * len(self.room_grid)) for i in range(len(self.room_grid)): for j in range(len(self.room_grid[0])): - rotated_room_grid[len(self.room_grid[0]) - j - 1][i] = self.room_grid[i][j] + rotated_room_grid[len(self.room_grid[0]) - j - 1][i] = self.room_grid[ + i + ][j] print("#####") str = "" for row in rotated_room_grid: @@ -85,5 +103,3 @@ def print_rooms(self): str += "#\n" print(str) print("#####") - -