Skip to content
Open
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: 16 additions & 56 deletions python_pathfinder.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,39 @@
# This function reads in a maze from a file and returns it as a list of lists of integers.
# Each integer corresponds to a different type of cell in the maze.
# 0 = empty cell
# 1 = wall
# 3 = end of the maze
# 5 = start of the maze
#code has been optimised to run faster using less caching and more direct functions
from collections import deque

def load_maze(filename):
with open(filename, "r") as file:
maze = [[int(num) for num in line.strip().split(",")] for line in file]
if len(maze) == 0:
print("Maze Not loaded.")
return maze

# This function takes in a maze and returns the coordinates of the start and end points.
# It searches the maze for the first occurrence of a 5 (start) and a 3 (end) and returns their coordinates as a tuple.
# If either the start or end point is not found, None is returned.
def find_start_and_end(maze):
rows = len(maze)
cols = len(maze[0])
start = None
end = None
for i in range(rows):
for j in range(cols):
if maze[i][j] == 5:
for i, row in enumerate(maze):
for j, cell in enumerate(row):
if cell == 5:
start = (i, j)
elif maze[i][j] == 3:
elif cell == 3:
end = (i, j)
# If both start and end points have been found, return them as a tuple.
if start != None and end != None:
if start and end:
return start, end

# This function takes in a maze and finds the shortest path from the start to the end point using BFS.
# It returns a list of coordinates representing the path from start to end.
# If no path is found, it returns None.
def solve_maze(maze):
# Find the start and end positions in the maze
start, end = find_start_and_end(maze)
# Get the number of rows and columns in the maze
rows = len(maze)
cols = len(maze[0])
# Initialize visited and parent 2D arrays to keep track of visited cells and their parents.
visited = [[False for j in range(cols)] for i in range(rows)]
parent = [[None for j in range(cols)] for i in range(rows)]
# Initialize the queue with the start cell and mark it as visited.
queue = [start]
# Mark the starting position as visited
visited[start[0]][start[1]] = True
# While the queue is not empty, continue BFS.
visited = set()
parent = [[None for _ in range(cols)] for _ in range(rows)]
queue = deque([start])
visited.add(start)
while queue:
current = queue.pop(0)
# If the current position is the end position, break out of the loop
current = queue.popleft()
if current == end:
break
row, col = current
neighbors = []
# Check if the neighboring positions are valid and not blocked
if row > 0 and maze[row - 1][col] != 1:
neighbors.append((row - 1, col))
if row < rows - 1 and maze[row + 1][col] != 1:
Expand All @@ -62,16 +42,13 @@ def solve_maze(maze):
neighbors.append((row, col - 1))
if col < cols - 1 and maze[row][col + 1] != 1:
neighbors.append((row, col + 1))
# For each unvisited neighbor, add it to the queue, mark it as visited, and set its parent to the current cell.
for neighbor in neighbors:
if not visited[neighbor[0]][neighbor[1]]:
if neighbor not in visited:
queue.append(neighbor)
visited[neighbor[0]][neighbor[1]] = True
visited.add(neighbor)
parent[neighbor[0]][neighbor[1]] = current
# If the end point has no parent, it means no path was found, so return None.
if parent[end[0]][end[1]] is None:
return None
# Construct the path from start to end by backtracking from the end position
path = []
current = end
while current != start:
Expand All @@ -82,13 +59,10 @@ def solve_maze(maze):
return path

def print_maze(maze):
# Prints the maze as a grid of numbers
for row in maze:
print(" ".join(str(num) for num in row))

def print_solution(maze, path):
# Prints the maze with the solution path marked as 5's.
# Walls are represented by 1's and the end point is a 3.
for i in range(len(maze)):
for j in range(len(maze[0])):
if maze[i][j] == 1:
Expand All @@ -98,18 +72,4 @@ def print_solution(maze, path):
elif (i, j) in path:
print("5", end=" ")
else:
print("0", end=" ")
print()

def main():
filename = "test_maze.txt"
maze = load_maze(filename)
path = solve_maze(maze)
if path != None:
print("Solution: ")
print_solution(maze, path)
else:
print("No solution found")

if __name__ == "__main__":
main()
print("0", end