diff options
Diffstat (limited to 'src/mazegen/find_shortest_path.py')
| -rw-r--r-- | src/mazegen/find_shortest_path.py | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/mazegen/find_shortest_path.py b/src/mazegen/find_shortest_path.py new file mode 100644 index 0000000..cba43f6 --- /dev/null +++ b/src/mazegen/find_shortest_path.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python3 + +from .maze import MazeGenerator +import typing +from .flush import flush_cells +from .cell import Cell + + +def record_directions(current: Cell, adjacent: Cell, direction: str) -> None: + """Update the shortest path to a given cell + as path of cardinal directions""" + adjacent.DIRECTIONS = current.DIRECTIONS.copy() + adjacent.DIRECTIONS.append(direction) + + +def update_path(current: Cell, adj: Cell) -> None: + """Update the shortest path to a given cell as a path of cell objects""" + adj.PATH = current.PATH.copy() + adj.PATH.append(adj) + + +def process_adjacent_cell(adj: Cell, + queue: typing.List, + current: Cell, + direction: str) -> None: + """check if the adjacent cell is visited; if not: + append it to the queue, mark it as VISITED, + update its PATH and record cardinal directions + from the start of the maze to the adjacent cell""" + if not adj.VISITED: + adj.VISITED = True + queue.append(adj) + update_path(current, adj) + record_directions(current, adj, direction) + + +def has_wall(walls: int, direction: str) -> bool: + """Check whether there is a wall in a given direction""" + # We don't need floor division for N + if direction == "E": + walls = walls // 2 + if direction == "S": + for _ in range(2): + walls = walls // 2 + if direction == "W": + for _ in range(3): + walls = walls // 2 + if walls % 2 == 0: + return False + return True + + +def is_outlet(current: Cell, maze: MazeGenerator) -> bool: + """Check whether the current cell is the outlet to the maze""" + # unpack tuple to make a cell object + x2, y2 = maze.outlet + # x col, y row + end: Cell = maze.grid[x2][y2] + if current == end: + for cell in current.PATH: + cell.IS_PATH = True + return True + return False + + +def compute_shortest_path(maze: MazeGenerator) -> None: + """Find the shortest path from the entry to the outlet""" + queue: typing.List = [] + x1, y1 = maze.entry + # x col, y row + start: Cell = maze.grid[x1][y1] + start.IS_PATH = True + current: Cell = start + queue.append(current) + while queue: + # Have the algorithm reached the end/outlet + if is_outlet(current, maze) is True: + return + # Initiate cell objects for each adjacenting cell + # of 'current' cell + adjacent_west = maze.grid[x1 - 1][y1] + adjacent_south = maze.grid[x1][y1 + 1] + adjacent_east = maze.grid[x1 + 1][y1] + adjacent_north = maze.grid[x1][y1 - 1] + # Initiate a list with cardinal direction and their + # respective adjacent cells + neighbours: typing.List = [("W", adjacent_west), + ("S", adjacent_south), + ("E", adjacent_east), + ("N", adjacent_north) + ] + # Loop through each adjacent cells + for adjacent in neighbours: + # adjacent[0] is the cardinal direction e.g. S for 'South' + # adjacent[1] is the corresponding adjacent cell object + if has_wall(current.WALLS, adjacent[0]) is False: + process_adjacent_cell(adjacent[1], + queue, + current, + adjacent[0]) + # We only enter the block below + # if there are cells left in the queue + if len(queue) > 1: + current = queue[1] + x1, y1 = current.POSITION + queue.pop(0) + + +if __name__ == "__main__": + # MazeGenerator(width, height, entry, outlet, perfect) + # self.rows: int = self.height + 2 + # self.cols: int = self.width + 2 + maze: MazeGenerator = MazeGenerator(10, 14, (1, 1), (10, 14), True) + # print("width, x:", maze.width) + # print("col, x:", maze.cols) + # print("height, y:", maze.height) + # print("rows, y:", maze.rows) + print(f"start: {maze.entry}") + print(f"outlet/end: {maze.outlet}") + maze.initiate_grid() + maze.draw_pattern() + maze.dfs_carve() + flush_cells(maze) + compute_shortest_path(maze) + maze.show_path = True + maze.visualize() + x, y = maze.outlet + outlet = maze.grid[x][y] + print(outlet.DIRECTIONS) |
