class Maze: def __init__(self, cells, width, height, startCell=None, exitCell=None): self.cells = cells self.width = width self.height = height self.startCell = startCell self.exitCell = exitCell def getCell(self, x, y): if 0 <= x < self.width and 0 <= y < self.height: return self.cells[y][x] return None def getNeighbors(self, cell): neighbors = [] for dx, dy in ((0, -1), (0, 1), (-1, 0), (1, 0)): nx, ny = cell.x + dx, cell.y + dy neighbor = self.getCell(nx, ny) if neighbor is not None and neighbor.isPassable(): neighbors.append(neighbor) return neighbors def render_lines(self, player_position=None, path=None): path_set = {(c.x, c.y) for c in path} if path else set() player_pos = None if player_position is None else (player_position.x, player_position.y) lines = [] for y in range(self.height): row = [] for x in range(self.width): cell = self.cells[y][x] if player_pos == (x, y): row.append("P") elif cell.isStart: row.append("S") elif cell.isExit: row.append("E") elif cell.isWall: row.append("#") elif (x, y) in path_set: row.append("*") elif cell.weight > 1: row.append(str(cell.weight)) else: row.append(" ") lines.append("".join(row)) return lines def render(self, player_position=None, path=None): return "\n".join(self.render_lines(player_position=player_position, path=path))