{"spec_id":"sudoku-basic","library":"muix","language":"javascript","code":"// anyplot.ai\n// sudoku-basic: Basic Sudoku Grid\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, useXScale, useYScale } from \"@mui/x-charts/hooks\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst EMPTY = 0; // the value that marks a cell without a given\nconst THIN = 1; // cell line width (px)\nconst THICK = 4; // box border width (px)\nconst DIGIT_SHARE = 0.56; // digit height as a share of the cell side\nconst HEADER_H = 96;\nconst PAD = 40;\n\n// Data: the puzzle's givens row by row from the top, 0 marks an empty cell\nconst GRID = [\n  [5, 3, 0, 0, 7, 0, 0, 0, 0],\n  [6, 0, 0, 1, 9, 5, 0, 0, 0],\n  [0, 9, 8, 0, 0, 0, 0, 6, 0],\n  [8, 0, 0, 0, 6, 0, 0, 0, 3],\n  [4, 0, 0, 8, 0, 3, 0, 0, 1],\n  [7, 0, 0, 0, 2, 0, 0, 0, 6],\n  [0, 6, 0, 0, 0, 0, 2, 8, 0],\n  [0, 0, 0, 4, 1, 9, 0, 0, 5],\n  [0, 0, 0, 0, 8, 0, 0, 7, 9],\n]; // role: grid\n\n// Plot: reads only the grid and the constants above\nconst N = GRID.length;\nconst BOX = Math.round(Math.sqrt(N)); // a 9×9 grid splits into 3×3 boxes\nconst indices = Array.from({ length: N }, (_, i) => i);\nconst givens = GRID.flatMap((row, r) =>\n  row.flatMap((value, c) => (value === EMPTY ? [] : [{ id: `${r}-${c}`, r, c, value }])),\n);\nconst boundaries = Array.from({ length: N + 1 }, (_, i) => i);\n// thin cell lines first, so the thick box borders paint over them where they meet\nconst lineLayers = [\n  { id: \"cells\", at: boundaries.filter((i) => i % BOX !== 0), width: THIN, stroke: t.inkSoft },\n  { id: \"boxes\", at: boundaries.filter((i) => i % BOX === 0), width: THICK, stroke: t.ink },\n];\n\nconst TITLE = \"sudoku-basic · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${N} × ${N} grid in ${BOX} × ${BOX} boxes · ${givens.length} givens · ${N * N - givens.length} empty cells`;\n\nfunction GridLayer() {\n  const xScale = useXScale(\"col\");\n  const yScale = useYScale(\"row\");\n  const { left, top, width, height } = useDrawingArea();\n  const cell = xScale.bandwidth();\n\n  return (\n    <g>\n      {lineLayers.map((layer) => (\n        <g key={layer.id} stroke={layer.stroke} strokeWidth={layer.width} strokeLinecap=\"square\">\n          {layer.at.map((i) => (\n            <g key={i}>\n              <line x1={left + i * cell} y1={top} x2={left + i * cell} y2={top + height} />\n              <line x1={left} y1={top + i * cell} x2={left + width} y2={top + i * cell} />\n            </g>\n          ))}\n        </g>\n      ))}\n      {givens.map((g) => (\n        <text\n          key={g.id}\n          x={(xScale(g.c) ?? 0) + cell / 2}\n          y={(yScale(g.r) ?? 0) + cell / 2}\n          textAnchor=\"middle\"\n          dominantBaseline=\"central\"\n          fontSize={cell * DIGIT_SHARE}\n          fontWeight={600}\n          fill={t.ink}\n        >\n          {g.value}\n        </text>\n      ))}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const { width, height } = window.ANYPLOT_SIZE;\n  const side = Math.min(width, height - HEADER_H);\n\n  return (\n    <Box sx={{ width, height, bgcolor: t.pageBg, display: \"flex\", flexDirection: \"column\", alignItems: \"center\" }}>\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={side}\n        height={side}\n        margin={{ top: PAD, right: PAD, bottom: PAD, left: PAD }}\n        skipAnimation\n        series={[]}\n        xAxis={[{ id: \"col\", scaleType: \"band\", data: indices, categoryGapRatio: 0, position: \"none\" }]}\n        yAxis={[{ id: \"row\", scaleType: \"band\", data: indices, categoryGapRatio: 0, position: \"none\" }]}\n      >\n        <GridLayer />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}