{"spec_id":"maze-circular","library":"muix","language":"javascript","code":"// anyplot.ai\n// maze-circular: Circular 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 { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\n// Difficulty sets how often the carve keeps extending the newest passage: a high share gives long\n// winding corridors with few branch points (harder), a low one many short branches (easier).\n// The carve is a spanning tree either way, so there is exactly one route between any two cells.\nconst DIFFICULTY_BIAS = { easy: 0.15, medium: 0.55, hard: 0.85 };\nconst MAZE_R = 1.04; // outer-wall radius plus its stroke, in outer-wall radii\nconst LABEL_R = 1.15; // radius of the START label anchor, just past the entry arrow\nconst LABEL_W = 70; // px width reserved for the START label\nconst LABEL_H = 26; // px height reserved for the START label\nconst GAP = 16; // px kept free around the drawing\nconst WALL_W = 3.5; // interior wall stroke in px\nconst OUTER_WALL_W = 5.5; // the outer boundary is drawn heavier\nconst GOAL_INK = \"#1A1A17\"; // the ink with the stronger contrast on the brand green goal disc\n\n// Data: replace this block with your own parameters\nconst RINGS = 7; // role: rings\nconst DIFFICULTY = \"medium\"; // role: difficulty\nconst SEED = 42; // role: seed\nconst SECTORS = 12; // role: extra\n\n// Plot: reads only the parameters and the constants above\n// Cell graph: a hub at the centre, then RINGS rings of SECTORS cells; each edge is a wall that can be opened\nconst edges = [];\nfor (let s = 0; s < SECTORS; s += 1) edges.push({ a: \"hub\", b: `1-${s}`, key: `h-${s}` });\nfor (let r = 1; r <= RINGS; r += 1) {\n  for (let s = 0; s < SECTORS; s += 1) edges.push({ a: `${r}-${s}`, b: `${r}-${(s + 1) % SECTORS}`, key: `c-${r}-${s}` });\n}\nfor (let r = 1; r < RINGS; r += 1) {\n  for (let s = 0; s < SECTORS; s += 1) edges.push({ a: `${r}-${s}`, b: `${r + 1}-${s}`, key: `r-${r}-${s}` });\n}\nconst adjacency = new Map();\nedges.forEach((e) => {\n  [[e.a, e.b], [e.b, e.a]].forEach(([from, to]) => {\n    if (!adjacency.has(from)) adjacency.set(from, []);\n    adjacency.get(from).push({ from, to, key: e.key });\n  });\n});\n\n// Randomised Prim's carve from the hub, biased by difficulty, with a fixed-seed LCG\nconst bias = DIFFICULTY_BIAS[DIFFICULTY];\nconst passages = new Set();\nconst visited = new Set([\"hub\"]);\nconst frontier = [...adjacency.get(\"hub\")];\nlet lastAdded = \"hub\";\nlet state = SEED >>> 0;\nwhile (frontier.length > 0) {\n  state = (Math.imul(state, 1664525) + 1013904223) >>> 0;\n  const continuing = state / 4294967296 < bias ? frontier.filter((e) => e.from === lastAdded) : [];\n  const pool = continuing.length > 0 ? continuing : frontier;\n  state = (Math.imul(state, 1664525) + 1013904223) >>> 0;\n  const edge = pool[Math.floor((state / 4294967296) * pool.length)];\n  frontier.splice(frontier.indexOf(edge), 1);\n  if (visited.has(edge.to)) continue;\n  visited.add(edge.to);\n  lastAdded = edge.to;\n  passages.add(edge.key);\n  adjacency.get(edge.to).forEach((next) => {\n    if (!visited.has(next.to)) frontier.push(next);\n  });\n}\n\n// The entry is the outer cell farthest from the goal along the carved passages\nconst distance = new Map([[\"hub\", 0]]);\nconst queue = [\"hub\"];\nwhile (queue.length > 0) {\n  const cell = queue.shift();\n  adjacency.get(cell).forEach((e) => {\n    if (passages.has(e.key) && !distance.has(e.to)) {\n      distance.set(e.to, distance.get(cell) + 1);\n      queue.push(e.to);\n    }\n  });\n}\nconst entrySector = Array.from({ length: SECTORS }, (_, s) => s).reduce(\n  (best, s) => (distance.get(`${RINGS}-${s}`) > distance.get(`${RINGS}-${best}`) ? s : best),\n  0,\n);\nconst routeCells = distance.get(`${RINGS}-${entrySector}`) + 1;\n\n// Walls in unit-disc geometry: radius 1 is the outer wall, angle 0 points right, sectors run clockwise from 12 o'clock\nconst HUB_R = 1 / (RINGS + 1);\nconst RING_W = (1 - HUB_R) / RINGS;\nconst STEP = (2 * Math.PI) / SECTORS;\nconst walls = [];\nfor (let s = 0; s < SECTORS; s += 1) {\n  if (!passages.has(`h-${s}`)) walls.push({ arc: true, r: HUB_R, a0: -Math.PI / 2 + s * STEP, width: WALL_W });\n}\nfor (let r = 1; r <= RINGS; r += 1) {\n  for (let s = 0; s < SECTORS; s += 1) {\n    if (!passages.has(`c-${r}-${s}`)) {\n      walls.push({ arc: false, r0: HUB_R + (r - 1) * RING_W, r1: HUB_R + r * RING_W, a0: -Math.PI / 2 + (s + 1) * STEP, width: WALL_W });\n    }\n    if (r < RINGS && !passages.has(`r-${r}-${s}`)) {\n      walls.push({ arc: true, r: HUB_R + r * RING_W, a0: -Math.PI / 2 + s * STEP, width: WALL_W });\n    }\n  }\n}\nfor (let s = 0; s < SECTORS; s += 1) {\n  if (s !== entrySector) walls.push({ arc: true, r: 1, a0: -Math.PI / 2 + s * STEP, width: OUTER_WALL_W });\n}\nconst entryAngle = -Math.PI / 2 + (entrySector + 0.5) * STEP;\nconst labelAnchor = Math.cos(entryAngle) > 0.3 ? \"start\" : Math.cos(entryAngle) < -0.3 ? \"end\" : \"middle\";\n\n// Largest px-per-unit that fits the maze and the START label on one axis: the label sits at unit\n// coordinate c and extends `before` px towards the negative side and `after` px towards the positive one\nconst fitUnit = (avail: number, c: number, before: number, after: number) =>\n  Math.min(\n    avail / (2 * MAZE_R),\n    MAZE_R + c > 0 ? (avail - after) / (MAZE_R + c) : Infinity,\n    MAZE_R - c > 0 ? (avail - before) / (MAZE_R - c) : Infinity,\n  );\n// View range on one axis: the content bounds at this scale, centred in the available span\nconst viewRange = (avail: number, unit: number, c: number, before: number, after: number) => {\n  const lo = Math.min(-MAZE_R, c - before / unit);\n  const hi = Math.max(MAZE_R, c + after / unit);\n  const min = lo - (avail / unit - (hi - lo)) / 2;\n  return { min, max: min + avail / unit };\n};\n\nconst TITLE = \"maze-circular · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${RINGS} rings × ${SECTORS} sectors · ${DIFFICULTY} difficulty · seed ${SEED} · one ${routeCells}-cell route from START to GOAL`;\nconst HEADER_H = 92;\n\nfunction Maze() {\n  const xScale = useXScale();\n  const yScale = useYScale();\n  const unit = xScale(1) - xScale(0);\n  const cx = xScale(0);\n  const cy = yScale(0);\n  const at = (r: number, a: number) => [cx + r * unit * Math.cos(a), cy + r * unit * Math.sin(a)];\n  const [tipX, tipY] = at(1.025, entryAngle);\n  const [leftX, leftY] = at(1.11, entryAngle - STEP * 0.07);\n  const [rightX, rightY] = at(1.11, entryAngle + STEP * 0.07);\n  // The label continues outward past the arrow, anchored away from the circle on whichever side the entry lies\n  const [labelX, labelY] = at(LABEL_R, entryAngle);\n  return (\n    <g>\n      {walls.map((w, i) => {\n        const [x0, y0] = at(w.arc ? w.r : w.r0, w.a0);\n        const [x1, y1] = w.arc ? at(w.r, w.a0 + STEP) : at(w.r1, w.a0);\n        const d = w.arc ? `M ${x0} ${y0} A ${w.r * unit} ${w.r * unit} 0 0 1 ${x1} ${y1}` : `M ${x0} ${y0} L ${x1} ${y1}`;\n        return <path key={i} d={d} fill=\"none\" stroke={t.ink} strokeWidth={w.width} strokeLinecap=\"round\" />;\n      })}\n      <path d={`M ${tipX} ${tipY} L ${leftX} ${leftY} L ${rightX} ${rightY} Z`} fill={t.palette[0]} />\n      <text x={labelX} y={labelY} fontSize={20} fontWeight={700} fill={t.ink} textAnchor={labelAnchor} dominantBaseline=\"middle\">\n        START\n      </text>\n      <circle cx={cx} cy={cy} r={HUB_R * unit * 0.62} fill={t.palette[0]} />\n      <text x={cx} y={cy} fontSize={18} fontWeight={700} fill={GOAL_INK} textAnchor=\"middle\" dominantBaseline=\"central\">\n        GOAL\n      </text>\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const bodyH = height - HEADER_H;\n  const availW = width - 2 * GAP;\n  const availH = bodyH - 2 * GAP;\n  // Only the side holding the START label gets extra room; one px-per-unit on both axes keeps the circle round\n  const lx = LABEL_R * Math.cos(entryAngle);\n  const ly = LABEL_R * Math.sin(entryAngle);\n  const left = labelAnchor === \"end\" ? LABEL_W : labelAnchor === \"middle\" ? LABEL_W / 2 : 0;\n  const right = labelAnchor === \"start\" ? LABEL_W : labelAnchor === \"middle\" ? LABEL_W / 2 : 0;\n  const unit = Math.min(fitUnit(availW, lx, left, right), fitUnit(availH, ly, LABEL_H / 2, LABEL_H / 2));\n  const xRange = viewRange(availW, unit, lx, left, right);\n  const yRange = viewRange(availH, unit, ly, LABEL_H / 2, LABEL_H / 2);\n\n  return (\n    <div style={{ width, height, display: \"flex\", flexDirection: \"column\" }}>\n      <div style={{ height: HEADER_H, padding: \"18px 40px 0 40px\", boxSizing: \"border-box\", textAlign: \"center\" }}>\n        <div style={{ fontSize: 26, fontWeight: 600, color: t.ink, lineHeight: 1.25 }}>{TITLE}</div>\n        <div style={{ fontSize: 18, color: t.inkSoft, marginTop: 6 }}>{SUBTITLE}</div>\n      </div>\n      <ChartsContainer\n        width={width}\n        height={bodyH}\n        series={[]}\n        skipAnimation\n        margin={{ left: GAP, right: GAP, top: GAP, bottom: GAP }}\n        xAxis={[{ id: \"x\", scaleType: \"linear\", ...xRange, position: \"none\" }]}\n        yAxis={[{ id: \"y\", scaleType: \"linear\", ...yRange, reverse: true, position: \"none\" }]}\n      >\n        <Maze />\n      </ChartsContainer>\n    </div>\n  );\n}\n"}