Sudoku constraint lab
HTML for this example
Select the code and use your copy shortcut. Changes in this preview are temporary.
<!doctype html><html lang="en-US"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sudoku constraint lab</title><style>*{box-sizing:border-box}html{color-scheme:light}body{margin:0;background:var(--bg,#eef1f4);color:var(--ink,#233449);font:16px/1.5 system-ui,-apple-system,Segoe UI,sans-serif}main{max-width:1040px;margin:auto;padding:26px 28px 44px}header{margin-bottom:20px}h1{font-size:clamp(26px,4vw,36px);letter-spacing:-.035em;line-height:1.15;margin:5px 0 10px}h2{font-size:21px;line-height:1.3;margin:0 0 15px}h3{font-size:17px;margin:0 0 9px}p{margin:8px 0 16px}.kicker{font-size:12px;text-transform:uppercase;letter-spacing:.12em;font-weight:750;color:var(--accent,#315b87)}.muted,small{color:#556473}.panel{margin:20px 0;padding:24px;border:1px solid var(--line,#ccd5df);border-radius:10px;background:#fff}.hero{padding:24px;background:var(--hero,#183b5c);color:#fff;border-radius:12px;margin:18px 0}.hero .kicker,.hero small{color:#d9e5ef}.big{font-size:clamp(32px,5vw,52px);line-height:1.15;font-weight:750;letter-spacing:-.035em;overflow-wrap:anywhere;margin:10px 0 15px}.row{display:flex;flex-wrap:wrap;gap:15px;align-items:end}.row>label{flex:1;min-width:140px}.grid{display:grid;grid-template-columns:1fr 1fr;gap:20px}.thirds{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:15px}label{display:block;font-size:14px;font-weight:650}input,select,textarea{font:inherit;font-size:16px;color:inherit;display:block;width:100%;margin-top:6px;padding:11px 12px;border:1px solid #a9b4c1;border-radius:6px;background:white;min-width:0}input[type=checkbox]{display:inline;width:auto;margin:0 8px 0 0;accent-color:var(--accent,#315b87)}textarea{min-height:100px;resize:vertical}button{font:inherit;font-weight:650;min-height:44px;border:1px solid var(--accent,#315b87);border-radius:6px;background:var(--accent,#315b87);color:white;padding:10px 16px;cursor:pointer}button.secondary{background:white;color:var(--accent,#315b87)}button:disabled{opacity:.5;cursor:default}button[aria-pressed=true]{box-shadow:0 0 0 3px #eab655}button:hover:not(:disabled){filter:brightness(.94)}:focus-visible{outline:3px solid #e5a72f;outline-offset:3px}.actions{display:flex;flex-wrap:wrap;gap:10px;margin:18px 0 0}[hidden]{display:none!important}.error{color:#a3232a;font-weight:650}.error:not(:empty){padding:12px;background:#fff0ed;border-left:4px solid #ab3439;margin:14px 0}.note{padding:14px;background:#edf2f6;border-radius:6px;margin:14px 0}.mono,pre{font-family:ui-monospace,Consolas,monospace;font-variant-numeric:tabular-nums}pre{white-space:pre-wrap;overflow-wrap:anywhere;font-size:14px;line-height:1.7;margin:0}.mono{overflow-wrap:anywhere}table{width:100%;border-collapse:collapse;table-layout:fixed;font-size:14px}th,td{text-align:left;vertical-align:top;overflow-wrap:anywhere;padding:11px 8px;border-bottom:1px solid #d4dce4}th{font-size:12px;text-transform:uppercase;letter-spacing:.035em;color:#536372}th:first-child,td:first-child{padding-left:0}.tag{display:inline-block;padding:4px 8px;border:1px solid currentColor;border-radius:4px;font-size:12px;font-weight:750}.rule{border-top:1px solid #cbd4dd;padding-top:18px}.line{height:30px;border-bottom:1px solid #aab5bf}.stat{font-size:34px;line-height:1.15;font-weight:750}.stat small{font:14px system-ui;display:block;margin-top:8px}details{margin:20px 0;border-top:1px solid #cbd4dd;padding:18px 0}summary{font-weight:700;cursor:pointer}details p{margin-top:15px}a{color:var(--accent,#315b87)}footer{font-size:13px;color:#556473;margin-top:25px}fieldset{min-width:0;border:1px solid #c8d2db;border-radius:7px;padding:18px;margin:15px 0}legend{font-weight:750;padding:0 6px}@media(max-width:720px){main{padding:20px}.panel,.hero{padding:20px}.thirds{gap:10px}}@media(max-width:540px){main{padding:16px}.grid,.thirds{grid-template-columns:1fr}.panel,.hero{padding:18px}.row{gap:12px}.row>label{min-width:100px}h1{font-size:27px}th,td{padding:10px 5px;font-size:13px}.big{font-size:37px}}.surface{background:white;border:1px solid #ccd5df;border-radius:10px;padding:24px;margin:18px 0}.metrics{display:flex;gap:34px;flex-wrap:wrap}.metric b{font-size:clamp(32px,5vw,52px);line-height:1.1;display:block;letter-spacing:-.04em}.metric span{display:block;margin-top:8px;font-size:14px}.strip{display:grid;grid-template-columns:repeat(10,minmax(0,1fr));gap:7px;margin:22px 0}.unit{padding:10px 3px;text-align:center;background:#e5f0eb;border-bottom:4px solid #327660;font-size:13px;font-weight:700}.unit.rework{background:#fff0d5;border-color:#aa701d}.unit.fail{background:#f9e2e0;border-color:#ab3d35}.ledger-row{display:grid;grid-template-columns:100px 1fr 140px;gap:18px;padding:18px 0;border-bottom:1px solid #d2dbe3;align-items:start}.ledger-row p{margin:5px 0}.amount{text-align:right;font-size:22px;font-weight:750}.pill{display:inline-block;border-radius:20px;background:#e7edf2;padding:5px 10px;font-size:12px;font-weight:700}.timeline{border-left:3px solid #c7d4de;padding:0 0 0 22px;margin:16px 0}.timeline article{padding:10px 0 18px}.timeline h3{margin-bottom:5px}.timeline p{margin:5px 0}.artifact{font-family:Georgia,serif}.warning{background:#fff3db;border-left:4px solid #a66d18;padding:15px}.empty{color:#5d6771;font-style:italic}.check{color:#22694f}.bad{color:#9a332e}button.linklike{padding:8px 12px;min-height:40px;background:white;color:var(--accent,#315b87)}@media(max-width:540px){.surface{padding:18px}.ledger-row{grid-template-columns:65px 1fr;gap:12px}.ledger-row .amount{grid-column:2;text-align:left}.strip{gap:3px}.unit{font-size:11px;padding:8px 0}.metrics{gap:22px}.metric b{font-size:38px}}.puzzle{max-width:760px;margin:auto}.puzzle-top{display:flex;justify-content:space-between;gap:15px;align-items:start}.puzzle-top p{margin:4px 0 15px}.sudoku{display:grid;grid-template-columns:repeat(9,minmax(0,1fr));border:2px solid #2d455c;max-width:540px;margin:10px auto 20px}.cell{position:relative;aspect-ratio:1;border-right:1px solid #b6c5d2;border-bottom:1px solid #b6c5d2;text-align:center;display:flex;align-items:center;justify-content:center;background:#edf2f6;font-size:clamp(17px,3.5vw,29px);font-weight:650}.cell:nth-child(3n){border-right:2px solid #2d455c}.cell:nth-child(9n){border-right:0}.cell:nth-child(n+19):nth-child(-n+27),.cell:nth-child(n+46):nth-child(-n+54){border-bottom:2px solid #2d455c}.cell span small{position:absolute;left:3px;top:0;font:9px system-ui;color:#516477}.cell input{border:0;border-radius:0;background:white;padding:0;margin:0;height:100%;text-align:center;font-size:inherit;font-weight:700;color:#245d85}.cell input:focus{outline:3px solid #dca83b;outline-offset:-3px}.cell input[aria-invalid=true]{background:#fff0e6}.sr{position:absolute;width:1px;height:1px;padding:0;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap}@media(max-width:540px){.puzzle{padding:14px}.puzzle-top{display:block}.cell span small{font-size:7px}.sudoku{margin-top:15px}}</style></head><body style="--bg:#eef3f7;--accent:#3a627f"><main><header><div class="kicker">Original 72-given teaching fixture</div><h1>Sudoku constraint lab</h1><p class="muted">Fill the final row while keeping all 72 givens intact.</p></header><section class="surface puzzle"><div class="puzzle-top"><div><h2 id="status" aria-live="polite"></h2><p id="progress"></p></div><span class="pill">G = given · E = entry</span></div><div id="board" class="sudoku" role="group" aria-label="Nine by nine Sudoku; rows one through eight are given, row nine editable"></div><p id="feedback" class="note" aria-live="polite"></p><div class="actions"><button type="button" id="reset">Reset entries</button><button type="button" id="conflict" class="secondary">Try r9c1 = 1</button><button type="button" id="partial" class="secondary">Try r9c2 = 1</button></div></section><details><summary>Developer key · separate from entries</summary><p>The forced final row is <b class="mono">912345678</b>. Each column omits exactly one digit. A completed board must also satisfy all nine rows and all nine boxes; matching this text alone is not the completion test.</p></details><details><summary>Entry and reset rules</summary><p>Commit a blank or one digit 1–9 in an E cell. Invalid committed text is rejected as a whole and the previous accepted value is restored. G cells are immutable text. A legal digit can still conflict with a row, column, or box; the evidence appears below the board.</p><p>Reset clears the nine entries. This is a deliberately simple fixture, with no generator, difficulty rating, solver, score, or learner assessment. A fully blank puzzle would have many solutions; one solution would not prove uniqueness.</p></details><footer>Local session only · No network or saved progress. Keep maintained source records authoritative.</footer></main><script>const $=id=>document.getElementById(id);const esc=s=>String(s).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
const givens=Object.freeze(('123456789/456789123/789123456/234567891/567891234/891234567/345678912/678912345').split('/').join('').split('').map(Number));let entries=Array(9).fill(''),composing=Array(9).fill(false),inputMessage='';
function boardValues(){return [...givens,...entries.map(v=>v===''?0:Number(v))]}function validate(b){const conflicts=[],invalid=[];let changed=false;for(let i=0;i<81;i++){if(i<72&&b[i]!==givens[i])changed=true;if(!Number.isInteger(b[i])||b[i]<0||b[i]>9)invalid.push(i)}const groups=[];for(let r=0;r<9;r++)groups.push({name:'Row '+(r+1),cells:Array.from({length:9},(_,c)=>r*9+c)});for(let c=0;c<9;c++)groups.push({name:'Column '+(c+1),cells:Array.from({length:9},(_,r)=>r*9+c)});for(let br=0;br<3;br++)for(let bc=0;bc<3;bc++)groups.push({name:'Box '+(br*3+bc+1),cells:Array.from({length:9},(_,i)=>(br*3+Math.floor(i/3))*9+bc*3+i%3)});for(const g of groups)for(let v=1;v<=9;v++){const cells=g.cells.filter(i=>b[i]===v);if(cells.length>1)conflicts.push({unit:g.name,value:v,cells})}const filled=b.filter(v=>Number.isInteger(v)&&v>=1&&v<=9).length;return {filled,conflicts,invalid,changed,complete:filled===81&&!invalid.length&&!conflicts.length&&!changed}}
const coordinate=i=>'r'+(Math.floor(i/9)+1)+'c'+(i%9+1);
function render(){const v=validate(boardValues());$('status').textContent=v.complete?'Complete · all constraints satisfied':v.conflicts.length?'Conflict · review the rule evidence':'Incomplete · no rule conflicts';$('progress').textContent=v.filled+' / 81 filled · '+entries.filter(Boolean).length+' / 9 editable cells filled';$('feedback').textContent=inputMessage||(v.conflicts.length?v.conflicts.map(c=>c.unit+' repeats '+c.value+' at '+c.cells.map(coordinate).join(' and ')).join('; '):v.complete?'All rows, columns, and boxes contain digits 1–9 once, with every given unchanged.':'Valid partial state. Empty cells still prevent completion.');for(let i=0;i<9;i++){const e=$('e'+i);if(!composing[i])e.value=entries[i];e.setAttribute('aria-invalid',String(v.conflicts.some(c=>c.cells.includes(72+i))))}}
function commit(i,raw){if(!/^(?:[1-9])?$/.test(raw)){inputMessage=coordinate(72+i)+': enter a blank or one digit 1–9. Rejected the full entry; previous value restored.';render();return false}entries[i]=raw;inputMessage='';render();return true}function reset(){entries=Array(9).fill('');composing=Array(9).fill(false);inputMessage='';render()}
$('board').innerHTML=Array.from({length:81},(_,i)=>i<72?'<div class="cell"><span aria-label="'+coordinate(i)+', given '+givens[i]+'"><small>G</small>'+givens[i]+'</span></div>':'<div class="cell"><label class="sr" for="e'+(i-72)+'">'+coordinate(i)+' editable, blank or one digit 1–9</label><input id="e'+(i-72)+'" inputmode="numeric" autocomplete="off" aria-label="'+coordinate(i)+' editable" aria-describedby="feedback"></div>').join('');for(let i=0;i<9;i++){const e=$('e'+i);e.addEventListener('compositionstart',()=>{composing[i]=true});e.addEventListener('compositionend',()=>{composing[i]=false;commit(i,e.value)});e.addEventListener('input',ev=>{if(!composing[i]&&!ev.isComposing)commit(i,e.value)});e.addEventListener('change',()=>{if(!composing[i])commit(i,e.value)})}$('reset').onclick=reset;$('conflict').onclick=()=>{reset();commit(0,'1');$('e0').focus()};$('partial').onclick=()=>{reset();commit(1,'1');$('e1').focus()};render();</script></body></html>