from ion import * from random import randint # Define constants for the maze MAZE_WIDTH = 20 MAZE_HEIGHT = 11 WALL_CHAR = '#' SPACE_CHAR = ' ' START_CHAR = 'S' END_CHAR = 'E' # Generate the maze maze = [[WALL_CHAR for _ in range(MAZE_WIDTH)] for _ in range(MAZE_HEIGHT)] start = (randint(0, MAZE_HEIGHT-1), randint(0, MAZE_WIDTH-1)) end = (randint(0, MAZE_HEIGHT-1), randint(0, MAZE_WIDTH-1)) while start == end: end = (randint(0, MAZE_HEIGHT-1), randint(0, MAZE_WIDTH-1)) maze[start[0]][start[1]] = START_CHAR maze[end[0]][end[1]] = END_CHAR for row in range(1, MAZE_HEIGHT-1): for col in range(1, MAZE_WIDTH-1): if maze[row][col] != START_CHAR and maze[row][col] != END_CHAR: if randint(0, 100) < 30: maze[row][col] = WALL_CHAR else: maze[row][col] = SPACE_CHAR # Define the pathfinding function def find_path(maze, start, end): queue = [start] visited = set() path = {} found = False while queue and not found: current = queue.pop(0) if current == end: found = True else: row, col = current neighbors = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)] for neighbor in neighbors: n_row, n_col = neighbor if 0 <= n_row < MAZE_HEIGHT and 0 <= n_col < MAZE_WIDTH and maze[n_row][n_col] != WALL_CHAR and neighbor not in visited: queue.append(neighbor) visited.add(neighbor) path[neighbor] = current if found: current = end path_list = [current] while current != start: current = path[current] path_list.append(current) path_list.reverse() return path_list else: return None # Find the path and print the maze with the path highlighted path = find_path(maze, start, end) if path is not None: for row in range(MAZE_HEIGHT): for col in range(MAZE_WIDTH): if (row, col) == start: print(START_CHAR, end='') elif (row, col) == end: print(END_CHAR, end='') elif (row, col) in path: print('+', end='') else: print(maze[row][col], end='') print() else: print("No path found.")