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

MVP #572

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

MVP #572

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
72 changes: 68 additions & 4 deletions adv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
#######
Expand Down
5 changes: 3 additions & 2 deletions player.py
Original file line number Diff line number Diff line change
@@ -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.")
7 changes: 7 additions & 0 deletions room.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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]
40 changes: 28 additions & 12 deletions world.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@
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
grid_size = 1
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
Expand All @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -85,5 +103,3 @@ def print_rooms(self):
str += "#\n"
print(str)
print("#####")