{"spec_id":"maze-printable","library":"muix","language":"javascript","code":"// anyplot.ai\n// maze-printable: Printable Maze Puzzle\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\n//# anyplot-orientation: square\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { useDrawingArea } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\n// The maze is a print artifact: black walls on white paper in both themes; only the page chrome flips\nconst PAPER = \"#FAF8F1\";\nconst WALL_INK = \"#1A1A17\";\nconst START_MARK = \"S\";\nconst GOAL_MARK = \"G\";\nconst TITLE = \"maze-printable · javascript · muix · anyplot.ai\";\nconst TITLE_H = 64;\nconst CAPTION_H = 48;\nconst PAGE_MARGIN = 48; // empty page around the maze, px\nconst WALL_SHARE = 0.075; // interior wall thickness as a share of the cell side\nconst MIN_WALL_PX = 4;\nconst BORDER_FACTOR = 1.75; // outer boundary weight relative to an interior wall\nconst MARK_SHARE = 0.55; // start and goal letters as a share of the cell side\nconst GOAL_DISC_SHARE = 0.4; // radius of the disc behind the goal letter, share of the cell side\nconst CARD_PAD_CELLS = 0.75; // paper margin around the outer boundary, in cells\n\n// Data: replace these parameters with your own maze size and seed\nconst MAZE_WIDTH = 22; // role: width\nconst MAZE_HEIGHT = 22; // role: height\nconst SEED = 42; // role: seed\n\n// Plot: reads only the parameters and the constants above\n// Randomized depth-first search (recursive backtracker, run iteratively): every carved\n// passage links a visited cell to a new one, so the passages form a spanning tree and\n// exactly one route joins the start (top-left) and the goal (bottom-right).\nconst startId = 0;\nconst goalId = MAZE_WIDTH * MAZE_HEIGHT - 1;\nconst passages = new Set([]);\nconst visited = new Set([startId]);\nconst stack = [startId];\nlet rngState = SEED | 0; // mulberry32, the browser has no seeded RNG\nwhile (stack.length > 0) {\n  const current = stack[stack.length - 1];\n  const row = Math.floor(current / MAZE_WIDTH);\n  const col = current % MAZE_WIDTH;\n  const candidates = [\n    row > 0 ? current - MAZE_WIDTH : -1,\n    row < MAZE_HEIGHT - 1 ? current + MAZE_WIDTH : -1,\n    col > 0 ? current - 1 : -1,\n    col < MAZE_WIDTH - 1 ? current + 1 : -1,\n  ].filter((id) => id >= 0 && !visited.has(id));\n  if (candidates.length === 0) {\n    stack.pop();\n    continue;\n  }\n  rngState = (rngState + 0x6d2b79f5) | 0;\n  let z = Math.imul(rngState ^ (rngState >>> 15), 1 | rngState);\n  z = (z + Math.imul(z ^ (z >>> 7), 61 | z)) ^ z;\n  const next = candidates[Math.floor((((z ^ (z >>> 14)) >>> 0) / 4294967296) * candidates.length)];\n  passages.add(`${Math.min(current, next)}|${Math.max(current, next)}`);\n  visited.add(next);\n  stack.push(next);\n}\n\n// Length of the one solution route, a difficulty cue for the caption; the route itself stays hidden\nconst cameFrom = new Map([[startId, startId]]);\nconst queue = [startId];\nfor (let head = 0; head < queue.length; head++) {\n  const current = queue[head];\n  const row = Math.floor(current / MAZE_WIDTH);\n  const col = current % MAZE_WIDTH;\n  const neighbours = [\n    row > 0 ? current - MAZE_WIDTH : -1,\n    row < MAZE_HEIGHT - 1 ? current + MAZE_WIDTH : -1,\n    col > 0 ? current - 1 : -1,\n    col < MAZE_WIDTH - 1 ? current + 1 : -1,\n  ];\n  for (const next of neighbours) {\n    if (next >= 0 && !cameFrom.has(next) && passages.has(`${Math.min(current, next)}|${Math.max(current, next)}`)) {\n      cameFrom.set(next, current);\n      queue.push(next);\n    }\n  }\n}\nlet solutionSteps = 0;\nfor (let node = goalId; node !== startId; node = cameFrom.get(node)) solutionSteps++;\n\n// Interior walls in cell units: every shared edge without a carved passage\nconst walls = [];\nfor (let row = 0; row < MAZE_HEIGHT; row++) {\n  for (let col = 0; col < MAZE_WIDTH; col++) {\n    const id = row * MAZE_WIDTH + col;\n    if (col < MAZE_WIDTH - 1 && !passages.has(`${id}|${id + 1}`)) {\n      walls.push({ x1: col + 1, y1: row, x2: col + 1, y2: row + 1 });\n    }\n    if (row < MAZE_HEIGHT - 1 && !passages.has(`${id}|${id + MAZE_WIDTH}`)) {\n      walls.push({ x1: col, y1: row + 1, x2: col + 1, y2: row + 1 });\n    }\n  }\n}\n\nconst CAPTION = `${MAZE_WIDTH} × ${MAZE_HEIGHT} cells · seed ${SEED} · solution route of ${solutionSteps} steps`;\n\n// Square cells: one cell side in px on both axes, the maze centred on its paper card\nfunction MazeLayer() {\n  const { left, top, width, height } = useDrawingArea();\n  const cell = Math.min(width / (MAZE_WIDTH + 2 * CARD_PAD_CELLS), height / (MAZE_HEIGHT + 2 * CARD_PAD_CELLS));\n  const originX = left + (width - cell * MAZE_WIDTH) / 2;\n  const originY = top + (height - cell * MAZE_HEIGHT) / 2;\n  const pad = cell * CARD_PAD_CELLS;\n  const wallStroke = Math.max(MIN_WALL_PX, cell * WALL_SHARE);\n\n  return (\n    <g>\n      <rect\n        x={originX - pad}\n        y={originY - pad}\n        width={cell * MAZE_WIDTH + 2 * pad}\n        height={cell * MAZE_HEIGHT + 2 * pad}\n        fill={PAPER}\n        stroke={t.grid}\n        strokeWidth={1.5}\n        rx={8}\n      />\n      <rect\n        x={originX}\n        y={originY}\n        width={cell * MAZE_WIDTH}\n        height={cell * MAZE_HEIGHT}\n        fill=\"none\"\n        stroke={WALL_INK}\n        strokeWidth={wallStroke * BORDER_FACTOR}\n        strokeLinejoin=\"round\"\n      />\n      {walls.map((w, i) => (\n        <line\n          key={i}\n          x1={originX + w.x1 * cell}\n          y1={originY + w.y1 * cell}\n          x2={originX + w.x2 * cell}\n          y2={originY + w.y2 * cell}\n          stroke={WALL_INK}\n          strokeWidth={wallStroke}\n          strokeLinecap=\"square\"\n        />\n      ))}\n      <text\n        x={originX + cell / 2}\n        y={originY + cell / 2}\n        fontSize={cell * MARK_SHARE}\n        fontWeight={700}\n        fill={WALL_INK}\n        textAnchor=\"middle\"\n        dominantBaseline=\"central\"\n      >\n        {START_MARK}\n      </text>\n      <circle\n        cx={originX + (MAZE_WIDTH - 0.5) * cell}\n        cy={originY + (MAZE_HEIGHT - 0.5) * cell}\n        r={cell * GOAL_DISC_SHARE}\n        fill={t.palette[0]}\n      />\n      <text\n        x={originX + (MAZE_WIDTH - 0.5) * cell}\n        y={originY + (MAZE_HEIGHT - 0.5) * cell}\n        fontSize={cell * MARK_SHARE}\n        fontWeight={700}\n        fill={WALL_INK}\n        textAnchor=\"middle\"\n        dominantBaseline=\"central\"\n      >\n        {GOAL_MARK}\n      </text>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const mazeSide = Math.min(width, height - TITLE_H - CAPTION_H) - PAGE_MARGIN * 2;\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\", alignItems: \"center\" }}>\n      <div\n        style={{\n          height: TITLE_H,\n          lineHeight: `${TITLE_H}px`,\n          fontSize: 26,\n          fontWeight: 500,\n          color: t.ink,\n        }}\n      >\n        {TITLE}\n      </div>\n      <div style={{ flex: 1, display: \"flex\", alignItems: \"center\", justifyContent: \"center\" }}>\n        <ChartsContainer\n          width={mazeSide}\n          height={mazeSide}\n          series={[]}\n          xAxis={[{ position: \"none\" }]}\n          yAxis={[{ position: \"none\" }]}\n          margin={{ top: 8, right: 8, bottom: 8, left: 8 }}\n          skipAnimation\n        >\n          <MazeLayer />\n        </ChartsContainer>\n      </div>\n      <div style={{ height: CAPTION_H, fontSize: 16, color: t.inkSoft }}>{CAPTION}</div>\n    </div>\n  );\n}\n"}