def traverse(): graph = Graph() visited = set() test_path = list() mapped_rooms = graph.dft( player.current_room ) # maps out the graph connecting each room with its neighbors rooms = [ room for room in mapped_rooms ] # a list of all rooms in the order in which they were added to the stack while len(visited) < len(room_graph) - 1: path = graph.bfs( rooms[0], rooms[1] ) # As long as there's rooms to visit, we take the shortest path between the first two rooms while len( path ) > 1: # While there is at least two rooms in the shortest path we check if the adjacent_room is a neighbor of the current_room and if it is we append the direction value to the adjacent_room current_room = path[0] adjacent_room = path[1] if adjacent_room in mapped_rooms[current_room]: test_path.append(mapped_rooms[current_room][adjacent_room]) path.remove(current_room) rooms.remove( rooms[0] ) # Removes the room you checked and ads it to the visited so that you can keep checking each room through the list visited.add(rooms[0]) return test_path
def play(): # create an array of visited rooms to keep track of visited = set() graph = Graph() # the path we take in that one run of the game trav_path = [] # using dfs to find the rooms - find all rooms on possible branch d_rooms = graph.dfs(player.current_room) # returns and array of the rooms in d_rooms rooms = [d_room for d_room in d_rooms] # while total visited is less than the total rooms in graph of world - 1 while (len(visited) < len(room_graph) - 1): # current room is the first room in the rooms array curr_room = rooms[0] # the next traverse will be to the next item in the rooms array next_room = rooms[1] # use a bfs to find the shortest path to destination - explores all the neighbour nodes shortest = graph.bfs(curr_room, next_room) # loop through shortest until nothing left while len(shortest) > 1: # find the neighbours curr_room_nays = d_rooms[shortest[0]] # next traverse will be to the next item in shortest array next_room = shortest[1] # if the next room (in the shortest path) exists in the current neighbours of curr room if next_room in curr_room_nays: trav_path.append(curr_room_nays[next_room]) # remove the first room from the queue shortest.remove(shortest[0]) # remove the current room from the total rooms rooms.remove(curr_room) # pop it on to the visited rooms array visited.add(curr_room) return trav_path
direction = random.choice(exits) else: direction = exits[0] else: # If we backtracked from a dead end, take next avail clockwise turn direction = exits[0] backtracked = False player.travel(direction) traversal_path.append(direction) visited_rooms.add(player.current_room) graph.add_room(player.current_room.id, player.current_room.get_exits()) graph.connect_rooms(current_room.id, player.current_room.id, direction) else: route = graph.bfs(player.current_room.id) for direction in route: player.travel(direction) traversal_path.append(direction) backtracked = True if len(traversal_path) < shortest_traversal: print(f"NEW RECORD FOUND WITH SEED {seed}") shortest_traversal = len(traversal_path) # TRAVERSAL TEST visited_rooms = set() player.current_room = world.starting_room visited_rooms.add(player.current_room) for move in traversal_path: