Skip to content
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/Library/Frameworks/Python.framework/Versions/3.8/bin/python3"
}
80 changes: 73 additions & 7 deletions projects/adventure/adv.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
from room import Room
from player import Player
from world import World

import random
import math
from ast import literal_eval
from projects.adventure.util import Stack, Queue
from projects.adventure.room import Room
from projects.adventure.player import Player
from projects.adventure.world import World

# 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_cross.txt"
map_file = "projects/adventure/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 = "projects/adventure/maps/main_maze.txt"

# Loads the map into a dictionary
room_graph=literal_eval(open(map_file, "r").read())
world.load_graph(room_graph)

def get_neighbors(compas):
neighbors = []

[n],[s],[w],[e] = compas
# north
if n > 0 and room_graph[n-1][s][w][e] == '?':
neighbors.append((n-1, s, w, e))
# next path -- len(room_graph)-1 && room_graph compas == "?"
if n < len(room_graph) - 1 and room_graph[n+1][s][w][e] == '?':
neighbors.append((n+1, s, w, e))
# south
if s > 0 and room_graph[n][s-1][w][e] == '?':
neighbors.append((n, s-1, w, e))
# next path -- len(room_graph[compas]) && room_graph compas == "?"
if s < len(room_graph[compas]) and room_graph[n+1][s][w][e] == '?':
neighbors.append((n+1, s, w, e))

# Print an ASCII map
world.print_rooms()

Expand All @@ -28,6 +45,55 @@
# Fill this out with directions to walk
# traversal_path = ['n', 'n']
traversal_path = []
visited = set()
counter = 0
path = []
# string = {n:s, s:n, e:w, w:e}
opposite_room = {'n': 's','s': 'n','e': 'w','w': 'e'}
room = {}

while len(visited) < len(room_graph):
# pop or dequeue the vertex of the queue:
current_room = player.current_room.id
# if vertex not in visited_vertices:
if current_room not in visited:
# add the vertex to the visited set()
visited.add(current_room)
possible_exits = player.current_room.get_exits()
room[current_room] = possible_exits
# loop tthrough all poss rooms
while len(room[current_room]) >= 0:
# more rooms? keep looping.
if len(room[current_room]) > 0:
# assign player_move to room queue
player_move = room[current_room].pop()
get_room = player_move[-1]

if get_room not in visited:
# add to the path
path.append(player_move)
# save player_move
traversal_path.append(player_move)
# add player_move to the room
player.travel(player_move)
counter += 1

# break so it wont break again
break

# traverse backwards
elif len(room[current_room]) == 0:
# pop prev move from path
prev = path.pop()
# find the prev direction.
prev_direction = opposite_room[prev]
# append prev move to traveral_path
traversal_path.append(prev_direction)
player.travel(prev_direction)
counter += 1
break





Expand Down
29 changes: 29 additions & 0 deletions projects/adventure/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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)
81 changes: 80 additions & 1 deletion projects/ancestor/ancestor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,82 @@


# create a BFS w/ cache and add conditions to earliest_ancestor()


# ultils because import was not working
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)


# graph


def earliest_ancestor(ancestors, starting_node):
pass
# the one at the farthest distance
# from the input individual.
# If there is more than one ancestor
# tied for "earliest", return the one
# with the lowest numeric ID. If the
# input individual has no parents,
# the function should return -1.

# turn the ancestors list into a adjacency list
adj_list = {}
for anc_pair in ancestors:
# add both vertices to adj_list
if anc_pair[0] not in adj_list:
adj_list[anc_pair[0]] = set()
if anc_pair[1] not in adj_list:
adj_list[anc_pair[1]] = set()
# add the edge between the 2 vertices
adj_list[anc_pair[1]].add(anc_pair[0])
print(adj_list)

# build the graph
# starting_node = []
queue = [ [starting_node] ]
visited = set()
max_path_length = 1
current_earliest_ancestor = -1

# traverse the graph BFS
while len(queue) > 0:
# dequeue the the current path + vertex
# get the current vertex out of the path
cur_path = queue.pop(0) # pop the "first" item
cur_vertex = cur_path[-1]
print(cur_path)

# if the vertex has not been visited
if cur_vertex not in visited:
# add the vertex to the visited set
visited.add(cur_vertex)

if len(cur_path) > max_path_length or len(cur_path) >= max_path_length and cur_vertex < current_earliest_ancestor:
max_path_length = len(cur_path)
current_earliest_ancestor = cur_vertex

for neighbors in adj_list[cur_vertex]:
# copy the current path
n_path = list(cur_path)
# add the neighbor to the queue
n_path.append(neighbors)
queue.append(n_path)
return current_earliest_ancestor



# print(earliest_ancestor(ancestors_list, 5))
Loading