{"spec_id":"crossword-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// crossword-basic: Crossword Puzzle Grid\n// Library: muix 9.17.0 | JavaScript 22.23.4\n// Quality: 86/100 | Updated: 2026-11-08\n//# anyplot-orientation: square\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\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// The puzzle is a monochrome print artefact: paper, ink and rules keep their values in both themes\nconst PAPER = \"#FAF8F1\";\nconst INK = \"#1A1A17\";\nconst RULE = \"#4A4A44\";\nconst BLOCK = \"#\"; // blocking-cell symbol in GRID; every other symbol is an entry cell\nconst NUMBER_SHARE = 0.3; // clue-number size as a share of the cell, leaves room for a letter\nconst HEADER_H = 84;\nconst BOARD_PAD = 28;\n\n// Data: replace this block with your own parameters\n// 15 × 15 newspaper-style grid, one string per row: \"#\" blocks, \".\" takes a letter\nconst GRID = [\n  \"....#.....#....\",\n  \"....#.....#....\",\n  \"....#.....#....\",\n  \"##.............\",\n  \"......##.......\",\n  \"...#.......#...\",\n  \"........#......\",\n  \"##...#...#...##\",\n  \"......#........\",\n  \"...#.......#...\",\n  \".......##......\",\n  \".............##\",\n  \"....#.....#....\",\n  \"....#.....#....\",\n  \"....#.....#....\",\n]; // role: grid\n\n// Plot: reads only the parameters and the constants above\nconst rowCount = GRID.length;\nconst colCount = Math.max(...GRID.map((row) => row.length));\nconst isEntry = GRID.map((row) => Array.from({ length: colCount }, (_, c) => c < row.length && row[c] !== BLOCK));\n\n// A cell gets the next clue number, in reading order, when it starts an across or a down word:\n// open itself, closed (or the edge) before it and open after it\nconst cells: { r: number; c: number; entry: boolean; number: number | null }[] = [];\nlet nextNumber = 1;\nlet acrossCount = 0;\nlet downCount = 0;\nfor (let r = 0; r < rowCount; r += 1) {\n  for (let c = 0; c < colCount; c += 1) {\n    const entry = isEntry[r][c];\n    const startsAcross = entry && !(isEntry[r][c - 1] ?? false) && (isEntry[r][c + 1] ?? false);\n    const startsDown = entry && !(isEntry[r - 1]?.[c] ?? false) && (isEntry[r + 1]?.[c] ?? false);\n    acrossCount += startsAcross ? 1 : 0;\n    downCount += startsDown ? 1 : 0;\n    cells.push({ r, c, entry, number: startsAcross || startsDown ? nextNumber++ : null });\n  }\n}\nconst blockCount = cells.filter((cell) => !cell.entry).length;\nconst symmetric = cells.every((cell) => cell.entry === isEntry[rowCount - 1 - cell.r][colCount - 1 - cell.c]);\n\n// Square cells: the board takes the largest cell size that fits, centred in the drawing space\nconst { width: W, height: H } = window.ANYPLOT_SIZE;\nconst cellSize = Math.min((W - 2 * BOARD_PAD) / colCount, (H - HEADER_H - 2 * BOARD_PAD) / rowCount);\nconst marginX = (W - cellSize * colCount) / 2;\nconst marginY = (H - HEADER_H - cellSize * rowCount) / 2;\nconst columnKeys = Array.from({ length: colCount }, (_, c) => String(c));\nconst rowKeys = Array.from({ length: rowCount }, (_, r) => String(r));\n\nconst TITLE = \"crossword-basic · javascript · muix · anyplot.ai\";\nconst subtitle = `${rowCount} × ${colCount} grid · ${blockCount} blocking cells${symmetric ? \" in 180° rotational symmetry\" : \"\"} · ${acrossCount} across and ${downCount} down entries`;\n\nfunction BoardLayer() {\n  const xScale = useXScale(\"column\");\n  const yScale = useYScale(\"row\");\n  const cell = xScale.bandwidth();\n  const left = xScale(columnKeys[0])!;\n  const top = yScale(rowKeys[0])!;\n  return (\n    <g>\n      {cells.map((c) => (\n        <rect\n          key={`cell-${c.r}-${c.c}`}\n          x={xScale(columnKeys[c.c])}\n          y={yScale(rowKeys[c.r])}\n          width={cell}\n          height={cell}\n          fill={c.entry ? PAPER : INK}\n          stroke={RULE}\n          strokeWidth={1.5}\n        />\n      ))}\n      {cells\n        .filter((c) => c.number !== null)\n        .map((c) => (\n          <text\n            key={`number-${c.r}-${c.c}`}\n            x={xScale(columnKeys[c.c])! + cell * 0.09}\n            y={yScale(rowKeys[c.r])! + cell * 0.08}\n            fontSize={cell * NUMBER_SHARE}\n            fontWeight={600}\n            fill={INK}\n            dominantBaseline=\"hanging\"\n          >\n            {c.number}\n          </text>\n        ))}\n      {/* Board frame in theme chrome, so edge blocks stay distinct from a dark page */}\n      <rect x={left} y={top} width={cell * colCount} height={cell * rowCount} fill=\"none\" stroke={t.inkSoft} strokeWidth={3} />\n    </g>\n  );\n}\n\nexport default function Chart() {\n  return (\n    <Box sx={{ width: W, height: H, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\" }}>\n      <Box sx={{ height: HEADER_H, display: \"flex\", flexDirection: \"column\", justifyContent: \"center\", alignItems: \"center\", gap: \"6px\", pt: \"8px\" }}>\n        <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1.2 }}>{TITLE}</Typography>\n        <Typography sx={{ fontSize: 15, color: t.inkSoft, lineHeight: 1.2 }}>{subtitle}</Typography>\n      </Box>\n      <ChartsContainer\n        width={W}\n        height={H - HEADER_H}\n        series={[]}\n        skipAnimation\n        margin={{ top: marginY, bottom: marginY, left: marginX, right: marginX }}\n        xAxis={[{ id: \"column\", scaleType: \"band\", data: columnKeys, categoryGapRatio: 0, position: \"none\" }]}\n        yAxis={[{ id: \"row\", scaleType: \"band\", data: rowKeys, categoryGapRatio: 0, position: \"none\" }]}\n      >\n        <BoardLayer />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}