[Python] Solution of maze

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have coded a random maze and it's path generator that shows output on turtle window. The problem is that this solves the maze but by passing through the walls of the maze can somebody help me tell & how can I restrict my code to obey the right path and avoid touching the walls of the maze

    This is the code of maze and path is generated on the python turtle window

    import turtle
    import random

    cell_size = 10


    def generate_maze(width, height):
    maze = [['-'] * (2 * width + 1) for _ in range(2 * height + 1)]

    def carve(x, y):
    directions = [(2, 0), (-2, 0), (0, 2), (0, -2)]
    random.shuffle(directions)

    for dx, dy in directions:
    nx, ny = x + dx, y + dy

    if 0 < nx < 2 * width and 0 < ny < 2 * height and maze[ny][nx] == '-':
    maze[ny][nx] = ' '
    maze[y + dy // 2][x + dx // 2] = ' '
    carve(nx, ny)

    start_x, start_y = 1, 2 * height - 1
    end_x, end_y = width * 2 - 1, 1

    maze[start_y][start_x] = ' '
    maze[end_y][end_x] = ' '

    carve(start_x, start_y)

    return maze


    def draw_maze_with_turtle(maze):
    turtle.speed(0)
    turtle.hideturtle()
    turtle.bgcolor('black')
    turtle.color('white')

    for y, row in enumerate(maze):
    for x, cell in enumerate(row):
    if cell == '-':
    turtle.penup()
    turtle.goto(x * cell_size - width * cell_size, height * cell_size - y * cell_size)
    turtle.pendown()
    turtle.forward(cell_size)
    turtle.right(90)
    turtle.forward(cell_size)
    turtle.right(90)
    turtle.forward(cell_size)
    turtle.right(90)
    turtle.forward(cell_size)
    turtle.right(90)


    def solve_maze(maze, start, end):
    stack = [start]
    visited = set()

    while stack:
    x, y = stack[-1]

    if (x, y) == end:
    break

    found = False
    for dx, dy in [(0, 2), (0, -2), (2, 0), (-2, 0)]:
    nx, ny = x + dx, y + dy

    if 0 <= nx < 2 * width and 0 <= ny < 2 * height and maze[ny][nx] == ' ' and (nx, ny) not in visited:
    visited.add((nx, ny))
    stack.append((nx, ny))
    found = True
    break

    if not found:
    stack.pop()

    return stack


    def draw_path_with_turtle(path):
    turtle.pencolor('green')
    turtle.pensize(3)

    for x, y in path:
    turtle.penup()
    turtle.goto(x * cell_size - width * cell_size + cell_size / 2, height * cell_size - y * cell_size + cell_size / 2)
    turtle.pendown()
    turtle.dot(cell_size - 2)


    if __name__ == '__main__':
    width = 60
    height = 5

    maze = generate_maze(width, height)

    start_x, start_y = 1, 2 * height - 1
    maze[start_y][start_x] = ' '

    draw_maze_with_turtle(maze)

    start = (start_x, start_y)
    end = (width * 2 - 1, 1)

    path = solve_maze(maze, start, end)
    draw_path_with_turtle(path)

    turtle.delay(0)
    turtle.done()


    A solution for the path touching the walls

    Continue reading...

Compartilhe esta Página