|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 6 | + <title>Sudoku DLX — Solution Replay</title> |
| 7 | + <style> |
| 8 | + body { font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; margin: 24px; } |
| 9 | + .grid { border-collapse: collapse; margin: 16px 0; } |
| 10 | + .grid td { width: 32px; height: 32px; text-align: center; font-size: 18px; border: 1px solid #ccc; } |
| 11 | + .grid td.bold-right { border-right: 2px solid #000; } |
| 12 | + .grid td.bold-bottom { border-bottom: 2px solid #000; } |
| 13 | + .stats { margin: 8px 0; color: #444; } |
| 14 | + .controls button { margin-right: 8px; } |
| 15 | + .filled { background: #eaf7ff; } |
| 16 | + .given { background: #f6f6f6; font-weight: 600; } |
| 17 | + </style> |
| 18 | +</head> |
| 19 | +<body> |
| 20 | + <h1>Sudoku DLX — Solution Replay</h1> |
| 21 | + <p>Load a <code>solution_reveal</code> JSON produced by <code>sudoku-dlx solve --trace out.json</code>.</p> |
| 22 | + <input type="file" id="file" accept="application/json" /> |
| 23 | + <div class="stats" id="stats"></div> |
| 24 | + <div class="controls"> |
| 25 | + <button id="btn-begin" disabled>« Begin</button> |
| 26 | + <button id="btn-prev" disabled>‹ Step</button> |
| 27 | + <button id="btn-play" disabled>Play</button> |
| 28 | + <button id="btn-next" disabled>Step ›</button> |
| 29 | + <button id="btn-end" disabled>End »</button> |
| 30 | + </div> |
| 31 | + <table class="grid" id="grid"></table> |
| 32 | + |
| 33 | + <script> |
| 34 | + const gridEl = document.getElementById('grid'); |
| 35 | + const statsEl = document.getElementById('stats'); |
| 36 | + let trace = null, idx = 0, playing = null, initial = null, givenMask = null; |
| 37 | + |
| 38 | + function buildGrid() { |
| 39 | + gridEl.innerHTML = ''; |
| 40 | + for (let r = 0; r < 9; r++) { |
| 41 | + const tr = document.createElement('tr'); |
| 42 | + for (let c = 0; c < 9; c++) { |
| 43 | + const td = document.createElement('td'); |
| 44 | + td.id = `cell-${r}-${c}`; |
| 45 | + if ((c+1) % 3 === 0 && c < 8) td.classList.add('bold-right'); |
| 46 | + if ((r+1) % 3 === 0 && r < 8) td.classList.add('bold-bottom'); |
| 47 | + tr.appendChild(td); |
| 48 | + } |
| 49 | + gridEl.appendChild(tr); |
| 50 | + } |
| 51 | + } |
| 52 | + function setCell(r,c,val,klass) { |
| 53 | + const td = document.getElementById(`cell-${r}-${c}`); |
| 54 | + td.textContent = val || ''; |
| 55 | + td.classList.remove('filled', 'given'); |
| 56 | + if (klass) td.classList.add(klass); |
| 57 | + } |
| 58 | + function loadInitial(initial81) { |
| 59 | + initial = initial81; |
| 60 | + givenMask = []; |
| 61 | + for (let i=0;i<81;i++) { |
| 62 | + const r = Math.floor(i/9), c = i%9; |
| 63 | + const ch = initial81[i]; |
| 64 | + const isGiven = ch !== '.'; |
| 65 | + givenMask.push(isGiven); |
| 66 | + setCell(r,c,isGiven ? ch : '', isGiven ? 'given' : null); |
| 67 | + } |
| 68 | + } |
| 69 | + function replayTo(k) { |
| 70 | + // reset to initial |
| 71 | + loadInitial(initial); |
| 72 | + for (let i=0;i<k;i++) { |
| 73 | + const {r,c,v} = trace.steps[i]; |
| 74 | + setCell(r,c,String(v),'filled'); |
| 75 | + } |
| 76 | + idx = k; |
| 77 | + updateButtons(); |
| 78 | + } |
| 79 | + function updateButtons() { |
| 80 | + const disabled = !trace; |
| 81 | + document.getElementById('btn-begin').disabled = disabled || idx===0; |
| 82 | + document.getElementById('btn-prev').disabled = disabled || idx===0; |
| 83 | + document.getElementById('btn-next').disabled = disabled || idx>=trace.steps.length; |
| 84 | + document.getElementById('btn-end').disabled = disabled || idx>=trace.steps.length; |
| 85 | + document.getElementById('btn-play').disabled = disabled; |
| 86 | + document.getElementById('btn-play').textContent = playing ? 'Pause' : 'Play'; |
| 87 | + } |
| 88 | + |
| 89 | + document.getElementById('file').addEventListener('change', async (e) => { |
| 90 | + const file = e.target.files[0]; |
| 91 | + if (!file) return; |
| 92 | + const text = await file.text(); |
| 93 | + const data = JSON.parse(text); |
| 94 | + if (data.kind !== 'solution_reveal') { alert('Unexpected trace kind'); return; } |
| 95 | + trace = data; idx = 0; |
| 96 | + buildGrid(); |
| 97 | + loadInitial(trace.initial); |
| 98 | + statsEl.textContent = `steps: ${trace.steps.length} · ${trace.stats.ms} ms · nodes ${trace.stats.nodes} · backtracks ${trace.stats.backtracks}`; |
| 99 | + updateButtons(); |
| 100 | + }); |
| 101 | + |
| 102 | + document.getElementById('btn-begin').onclick = () => replayTo(0); |
| 103 | + document.getElementById('btn-prev').onclick = () => replayTo(Math.max(0, idx-1)); |
| 104 | + document.getElementById('btn-next').onclick = () => replayTo(Math.min(trace.steps.length, idx+1)); |
| 105 | + document.getElementById('btn-end').onclick = () => replayTo(trace.steps.length); |
| 106 | + document.getElementById('btn-play').onclick = () => { |
| 107 | + if (!trace) return; |
| 108 | + if (playing) { clearInterval(playing); playing = null; updateButtons(); return; } |
| 109 | + playing = setInterval(() => { |
| 110 | + if (idx >= trace.steps.length) { clearInterval(playing); playing = null; updateButtons(); return; } |
| 111 | + replayTo(idx+1); |
| 112 | + }, 60); |
| 113 | + updateButtons(); |
| 114 | + }; |
| 115 | + |
| 116 | + buildGrid(); |
| 117 | + </script> |
| 118 | +</body> |
| 119 | +</html> |
0 commit comments