hay que generar el laberinto en un algoritmo mas complejo: "import tkinter as tk
import random
import time

class MazeApp:
    def __init__(self, master):
        self.master = master
        self.master.title("Generador y Resolver Laberinto - Algoritmo de Kruskal")

        # Entrada para el tamaño del laberinto
        self.size_frame = tk.Frame(master)
        self.size_frame.pack()
        tk.Label(self.size_frame, text="Tamaño (impar): ").pack(side='left')
        self.size_entry = tk.Entry(self.size_frame)
        self.size_entry.pack(side='left')
        self.size_entry.insert(0, "31")  # Tamaño predeterminado

        self.generate_button = tk.Button(master, text="Generar Laberinto", command=self.initialize_maze)
        self.generate_button.pack()

        self.canvas = tk.Canvas(master, width=750, height=750)
        self.canvas.pack()

        self.solve_button = tk.Button(master, text="Resolver Laberinto", command=self.solve_maze)
        self.solve_button.pack(side='left')

        self.play_button = tk.Button(master, text="Jugar Manualmente", command=self.play_maze)
        self.play_button.pack(side='right')

        self.cell_size = 25
        self.grid = None
        self.solution_path = []
        self.player_position = None

    def initialize_maze(self):
        size = int(self.size_entry.get())
        if size % 2 == 0:
            size += 1  # Asegurar que el tamaño sea impar
        self.rows = size
        self.columns = size
        self.start = (1, 1)
        self.end = (self.rows - 2, self.columns - 2)
        self.create_maze()
        self.draw_maze()
        self.solution_path = []

    def create_maze(self):
        self.grid = [[1 for _ in range(self.columns)] for _ in range(self.rows)]
        sets = [[(i, j) for j in range(self.columns)] for i in range(self.rows)]
        walls = []

        # Inicializar conjuntos y paredes
        for i in range(1, self.rows - 1, 2):
            for j in range(1, self.columns - 1, 2):
                self.grid[i][j] = 0  # Pasillos
                if i < self.rows - 2:
                    walls.append(((i, j), (i + 2, j)))
                if j < self.columns - 2:
                    walls.append(((i, j), (i, j + 2)))

        random.shuffle(walls)

        # Implementación del algoritmo de Kruskal
        parent = {}
        for i in range(1, self.rows - 1, 2):
            for j in range(1, self.columns - 1, 2):
                parent[(i, j)] = (i, j)

        def find(u):
            while parent[u] != u:
                u = parent[u]
            return u

        def union(u, v):
            pu = find(u)
            pv = find(v)
            parent[pv] = pu

        for wall in walls:
            cell1, cell2 = wall
            if find(cell1) != find(cell2):
                union(cell1, cell2)
                # Derribar la pared entre cell1 y cell2
                wall_cell = ((cell1[0] + cell2[0]) // 2, (cell1[1] + cell2[1]) // 2)
                self.grid[wall_cell[0]][wall_cell[1]] = 0
            else:
                # Introducir ciclos derribando algunas paredes adicionales al azar
                if random.random() < 0.05:
                    wall_cell = ((cell1[0] + cell2[0]) // 2, (cell1[1] + cell2[1]) // 2)
                    self.grid[wall_cell[0]][wall_cell[1]] = 0

        # Agregar caminos falsos y obstáculos
        for _ in range(int(self.rows * self.columns * 0.05)):
            i = random.randint(1, self.rows - 2)
            j = random.randint(1, self.columns - 2)
            if self.grid[i][j] == 1:
                self.grid[i][j] = 0

    def draw_maze(self):
        self.canvas.delete("all")
        for r in range(self.rows):
            for c in range(self.columns):
                color = "black" if self.grid[r][c] == 1 else "white"
                self.canvas.create_rectangle(c * self.cell_size, r * self.cell_size,
                                             (c + 1) * self.cell_size,
                                             (r + 1) * self.cell_size, fill=color, outline='gray')

        # Inicio
        self.canvas.create_rectangle(self.start[1] * self.cell_size, self.start[0] * self.cell_size,
                                     (self.start[1] + 1) * self.cell_size,
                                     (self.start[0] + 1) * self.cell_size, fill="green")

        # Fin
        self.canvas.create_rectangle(self.end[1] * self.cell_size, self.end[0] * self.cell_size,
                                     (self.end[1] + 1) * self.cell_size,
                                     (self.end[0] + 1) * self.cell_size, fill="red")

    def solve_maze(self):
        self.solution_path = []
        visited = set()
        if self._solve_bfs():
            self.animate_solution()
        else:
            print("No hay solución para el laberinto.")

    def _solve_bfs(self):
        from collections import deque
        queue = deque()
        queue.append((self.start[0], self.start[1], []))
        visited = set()
        visited.add((self.start[0], self.start[1]))
        while queue:
            r, c, path = queue.popleft()
            if (r, c) == self.end:
                self.solution_path = path + [(r, c)]
                return True
            for dr, dc in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                nr, nc = r + dr, c + dc
                if (0 <= nr < self.rows and 0 <= nc < self.columns and
                        self.grid[nr][nc] == 0 and (nr, nc) not in visited):
                    visited.add((nr, nc))
                    queue.append((nr, nc, path + [(r, c)]))
        return False

    def animate_solution(self):
        for r, c in self.solution_path:
            if (r, c) != self.start and (r, c) != self.end:
                self.canvas.create_rectangle(c * self.cell_size, r * self.cell_size,
                                             (c + 1) * self.cell_size,
                                             (r + 1) * self.cell_size, fill="blue")
                self.master.update()
                time.sleep(0.005)

    def play_maze(self):
        self.player_position = list(self.start)
        self.draw_maze()
        # Dibujar al jugador
        self.player = self.canvas.create_rectangle(self.player_position[1] * self.cell_size,
                                                   self.player_position[0] * self.cell_size,
                                                   (self.player_position[1] + 1) * self.cell_size,
                                                   (self.player_position[0] + 1) * self.cell_size,
                                                   fill="yellow")
        self.master.bind("<Key>", self.move_player)

    def move_player(self, event):
        r, c = self.player_position
        if event.keysym == 'Up':
            nr, nc = r - 1, c
        elif event.keysym == 'Down':
            nr, nc = r + 1, c
        elif event.keysym == 'Left':
            nr, nc = r, c - 1
        elif event.keysym == 'Right':
            nr, nc = r, c + 1
        else:
            return

        if 0 <= nr < self.rows and 0 <= nc < self.columns:
            if self.grid[nr][nc] == 0:
                self.player_position = [nr, nc]
                self.canvas.coords(self.player,
                                   nc * self.cell_size,
                                   nr * self.cell_size,
                                   (nc + 1) * self.cell_size,
                                   (nr + 1) * self.cell_size)
                if (nr, nc) == self.end:
                    self.master.unbind("<Key>")
                    self.canvas.create_text(self.columns * self.cell_size // 2,
                                            self.rows * self.cell_size // 2,
                                            text="¡Has ganado!", font=("Helvetica", 24), fill="red")

if __name__ == "__main__":
    root = tk.Tk()
    app = MazeApp(root)
    root.mainloop()"