haz que el laberinto sea mas complejo y dificil de resolver: "import tkinter as tk
import random
import time

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

        # 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, "21")

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

        self.canvas = tk.Canvas(master, width=525, height=525)
        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):
        # Implementar el algoritmo de backtracking recursivo
        self.grid = [[1 for _ in range(self.columns)] for _ in range(self.rows)]
        self._carve_passages_from(1, 1)

    def _carve_passages_from(self, cx, cy):
        directions = [(2, 0), (-2, 0), (0, 2), (0, -2)]
        random.shuffle(directions)
        self.grid[cy][cx] = 0
        for dx, dy in directions:
            nx, ny = cx + dx, cy + dy
            if 0 < ny < self.rows - 1 and 0 < nx < self.columns - 1 and self.grid[ny][nx] == 1:
                self.grid[cy + dy//2][cx + dx//2] = 0
                self._carve_passages_from(nx, ny)

    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_dfs(self.start[0], self.start[1], visited):
            self.animate_solution()
        else:
            print("No hay solución para el laberinto.")

    def _solve_dfs(self, r, c, visited):
        if (r, c) == self.end:
            self.solution_path.append((r, c))
            return True

        if (r < 0 or r >= self.rows or
            c < 0 or c >= self.columns or
            self.grid[r][c] == 1 or
            (r, c) in visited):
            return False

        visited.add((r, c))
        self.solution_path.append((r, c))

        # Explorar en cada dirección en orden aleatorio para incrementar la dificultad
        directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
        random.shuffle(directions)
        for dr, dc in directions:
            if self._solve_dfs(r + dr, c + dc, visited):
                return True

        self.solution_path.pop()
        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.01)

    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()"