def setMaze(self, size): self.size = size # the dimension of one side of the maze self.cells = [Cell.Cell(i, size) for i in range(size**2)] self.startingCell = self.cells[0] self.endingCell = self.cells[size**2 - 1] self.mazeWalls = [] self.allRect = [] self.genMaze()
def BFS(maze): visited = [[False] * maze.cols for _ in range(maze.rows)] start = Cell(maze.get_start_coord()) end = Cell(maze.get_end_coord()) queue = [start] visited[start.coord[0]][start.coord[1]] = True while queue and queue[0].coord != end.coord: cur_cell = queue.pop(0) for d in directions: new_coord = (cur_cell.coord[0] + d[0], cur_cell.coord[1] + d[1]) if maze.is_valid_point(new_coord, visited): new = Cell(new_coord) new.prev = cur_cell queue.append(new) visited[new_coord[0]][new_coord[1]] = True if queue: return queue[0] else: raise IndexError