{"spec_id":"maze-printable","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nmaze-printable: Printable Maze Puzzle\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 75/100 | Updated: 2026-05-16\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\n\n# Maze generation using Depth-First Search (DFS) algorithm\nnp.random.seed(42)\n\nwidth = 25  # Number of cells horizontally\nheight = 25  # Number of cells vertically\n\n# Initialize maze grid: 0 = wall, 1 = passage\n# Using 2*size+1 to account for walls between cells\nmaze_width = 2 * width + 1\nmaze_height = 2 * height + 1\nmaze = np.zeros((maze_height, maze_width), dtype=int)\n\n# DFS maze generation\nvisited = np.zeros((height, width), dtype=bool)\nstack = [(0, 0)]\nvisited[0, 0] = True\nmaze[1, 1] = 1  # Start cell is passage\n\ndirections = [(0, 1), (1, 0), (0, -1), (-1, 0)]  # right, down, left, up\n\nwhile stack:\n    cy, cx = stack[-1]\n\n    # Find unvisited neighbors\n    neighbors = []\n    for dy, dx in directions:\n        ny, nx = cy + dy, cx + dx\n        if 0 <= ny < height and 0 <= nx < width and not visited[ny, nx]:\n            neighbors.append((ny, nx, dy, dx))\n\n    if neighbors:\n        # Choose random neighbor\n        ny, nx, dy, dx = neighbors[np.random.randint(len(neighbors))]\n\n        # Remove wall between current cell and neighbor\n        wall_y = 2 * cy + 1 + dy\n        wall_x = 2 * cx + 1 + dx\n        maze[wall_y, wall_x] = 1\n\n        # Mark neighbor as passage and visited\n        maze[2 * ny + 1, 2 * nx + 1] = 1\n        visited[ny, nx] = True\n        stack.append((ny, nx))\n    else:\n        stack.pop()\n\n# Define start and goal positions (in maze coordinates)\nstart_y, start_x = 1, 1  # Top-left cell\ngoal_y, goal_x = maze_height - 2, maze_width - 2  # Bottom-right cell\n\n# Create figure (square format for maze)\nfig, ax = plt.subplots(figsize=(12, 12), facecolor=PAGE_BG)\n\n# Draw maze - walls and passages with theme-adaptive colors\n# For printable maze: black walls on white passages works in both themes\n# Light: black walls, white passages; Dark: white walls, dark passages\nmaze_display = maze.copy().astype(float)\nif THEME == \"dark\":\n    maze_display = 1 - maze_display\n\nax.imshow(maze_display, cmap=\"gray\", interpolation=\"nearest\", aspect=\"equal\")\nax.set_facecolor(PAGE_BG)\n\n# Mark start position with \"S\"\nax.text(start_x, start_y, \"S\", fontsize=28, fontweight=\"bold\", ha=\"center\", va=\"center\", color=INK)\n\n# Mark goal position with \"G\"\nax.text(goal_x, goal_y, \"G\", fontsize=28, fontweight=\"bold\", ha=\"center\", va=\"center\", color=INK)\n\n# Remove axes for clean printable appearance\nax.set_xticks([])\nax.set_yticks([])\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"bottom\"].set_visible(False)\nax.spines[\"left\"].set_visible(False)\n\n# Title\nax.set_title(\"maze-printable · matplotlib · anyplot.ai\", fontsize=24, color=INK, pad=20)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}