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
79 changes: 77 additions & 2 deletions projects/adventure/adv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

import random
from ast import literal_eval
import time

# 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"
Expand All @@ -28,6 +29,80 @@
# Fill this out with directions to walk
# traversal_path = ['n', 'n']
traversal_path = []
return_sequence = []

print("STARTING")
my_room_graph = {}
visited_rooms = set()
all_rooms_visited = False
opposite_directions = {
"n": "s",
"s": "n",
"w": "e",
"e": "w"
}

def add_room_to_graph(room_id, exit_options, prev_r=None, prev_r_direction=None):
my_room_graph[room_id] = {}
for exit_option in exit_options:
if exit_option == prev_r_direction:
my_room_graph[room_id][exit_option] = prev_r
else:
my_room_graph[room_id][exit_option] = "?"

def get_moves(current_exits):
moves = []
for exit_option in current_exits:
if my_room_graph[room_id][exit_option] == "?":
moves.append(exit_option)
return moves

# add first room to graph
add_room_to_graph(player.current_room.id, player.current_room.get_exits())

while not all_rooms_visited:
current_exits = player.current_room.get_exits()
room_id = player.current_room.id
print("-"*10)
print(f"Current room: {room_id} - {current_exits}")
print(f"Curr return moves: {return_sequence}")
print(my_room_graph)

my_moves = get_moves(current_exits)
for exit_option in current_exits:
if my_room_graph[room_id][exit_option] == "?":
my_moves.append(exit_option)
if len(my_moves) == 0:
if len(return_sequence) == 0:
all_rooms_visited = True
else:
# return spot until you have moves
my_move = return_sequence.pop(-1)
print(f"moving {my_move}")
traversal_path.append(my_move)
player.travel(my_move)
# if at starting room, reset return_sequence
# check for loop by seeing ...
if player.current_room.id == world.starting_room:
return_sequence = []

else:
my_move = my_moves[0]
print(f"moving {my_move}")
traversal_path.append(my_move)
return_sequence.append(opposite_directions[my_move])
player.travel(my_move)
new_room_id = player.current_room.id

# add new_room_id to room_id's graph
my_room_graph[room_id][my_move] = new_room_id
if new_room_id not in visited_rooms:
# add new room to visited
visited_rooms.add(new_room_id)
# add new_room_id to graph
add_room_to_graph(new_room_id, player.current_room.get_exits(), room_id, opposite_directions[my_move])
print()




Expand Down Expand Up @@ -59,4 +134,4 @@
elif cmds[0] == "q":
break
else:
print("I did not understand that command.")
print("I did not understand that command.")
48 changes: 47 additions & 1 deletion projects/ancestor/ancestor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
from util import Queue

def get_ancestor(ancestors, child):
heirs = []
for heir in ancestors:
if heir[1] == child:
heirs.append(heir[0])
return heirs

def earliest_ancestor(ancestors, starting_node):
pass
#create empty queue
q = Queue()
#add starting node to queue
q.enqueue([starting_node])
#create set to store visited vertices
visited = set()
#initialize path length
path_len = 1
#sets oldest parent as -1 for if no parent
oldest_parent = -1

#while size of q is greater than 0
while q.size() > 0:
#dequeue first path
path = q.dequeue()
#grab the last vertex from the path
cur_node = path[-1]

#if that vertex has not been visited
if cur_node not in visited:
#mark as visited
visited.add(cur_node)

#check for need to update
if len(path) >= path_len and cur_node <oldest_parent or len(path) > path_len:
#updates path length
path_len = len(path)
#updates oldest parent
oldest_parent = cur_node

#then add a path to its parent to the back of the queue
for parent in get_ancestor(ancestors, cur_node):
#copy path
path_copy = list(path)
#append parent to the back
path_copy.append(parent)
q.enqueue(path_copy)

return oldest_parent
78 changes: 77 additions & 1 deletion projects/social/social.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import random

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 User:
def __init__(self, name):
self.name = name
Expand Down Expand Up @@ -44,9 +59,41 @@ def populate_graph(self, num_users, avg_friendships):
self.friendships = {}
# !!!! IMPLEMENT ME

if avg_friendships >= num_users:
print("Could not create graph: The number of users must be greater than the average number of friendships.")
return
# Add users
for i in range(1, num_users + 1):
self.add_user(f"User {i}")

# Generate pairs of IDs to make friendships
total_friendships = avg_friendships * num_users // 2

friendships_to_create = set()

while len(friendships_to_create) < total_friendships:

user1ID = random.randrange(1, num_users + 1)
user2ID = random.randrange(1, num_users + 1)

# pick a different user ID for user2
while user1ID == user2ID:
user2ID = random.randrange(1, num_users + 1)

# ensure that the same two IDs are not added twice
smallerID = user1ID if user1ID < user2ID else user2ID
largerID = user1ID if user1ID > user2ID else user2ID

friendships_to_create.add((smallerID, largerID))

# Create friendships
for friendship in friendships_to_create:

user1ID, user2ID = friendship
self.add_friendship(user1ID, user2ID)

print(f"\nSuccessfully populated graph with {num_users} users, each with an average of {avg_friendships} friendships.\n")


def get_all_social_paths(self, user_id):
"""
Expand All @@ -59,6 +106,35 @@ def get_all_social_paths(self, user_id):
"""
visited = {} # Note that this is a dictionary, not a set
# !!!! IMPLEMENT ME
visited = {} # make dictionary for visited

# initialize dictionary with path to self
# later connections will build off of this entry
visited[user_id] = [user_id]

# use a queue to keep track of all friends that have not been visited
q = Queue()
q.enqueue(user_id)

while q.size() > 0:

current_friend_ID = q.dequeue()
friends_of_current_friend = self.friendships[current_friend_ID]

# process friends of current friend
for friend_ID in friends_of_current_friend:

# add new friend to queue and update path
if friend_ID not in visited:
q.enqueue(friend_ID)

# make a copy of the path to this friend so far,
# and add the current friend's ID
path_to_new_friend = list(visited[current_friend_ID])
path_to_new_friend.append(friend_ID)
# store the path in "visited" to mark the friend as visited
visited[friend_ID] = path_to_new_friend

return visited


Expand All @@ -67,4 +143,4 @@ def get_all_social_paths(self, user_id):
sg.populate_graph(10, 2)
print(sg.friendships)
connections = sg.get_all_social_paths(1)
print(connections)
print(connections)