JavaScript Pacman Code: Check Contact Before Collecting
HTML for this example
Select the code and use your copy shortcut. Changes in this preview are temporary.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>JavaScript Pacman Code: Check Contact Before Collecting</title><style>*{box-sizing:border-box}body{margin:0;color:var(--ink,#233342);background:var(--bg,#eef2f3);font:17px/1.6 system-ui,sans-serif}main{max-width:980px;margin:auto;padding:clamp(16px,4vw,38px)}h1{font:700 clamp(30px,5vw,49px)/1.12 Georgia,serif;margin:10px 0 20px;max-width:800px}h2{font-size:24px;line-height:1.3;margin:0 0 15px}h3{font-size:19px;margin:12px 0}p{margin:0 0 16px}.eyebrow{font-weight:750;letter-spacing:.08em;text-transform:uppercase;font-size:16px}header{padding:10px 0 25px;border-bottom:3px solid var(--accent,#24586d);margin-bottom:24px}.lede{font-size:20px;max-width:760px}.panel{background:var(--paper,#fff);padding:clamp(18px,3vw,28px);border:1px solid var(--line,#c6d1d8);border-radius:12px;margin:20px 0}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px}.grid>*{min-width:0}.row{display:flex;gap:12px;flex-wrap:wrap;align-items:center;margin:16px 0}.stack>*{margin:12px 0}button{font:700 17px/1.4 system-ui,sans-serif;cursor:pointer;color:white;background:var(--accent,#24586d);border:2px solid var(--accent,#24586d);border-radius:7px;padding:11px 17px;max-width:100%;overflow-wrap:anywhere}button.secondary{background:transparent;color:var(--ink,#233342)}button[aria-pressed=true]{box-shadow:inset 0 0 0 3px #fff;outline:2px solid var(--ink,#233342)}button:disabled{opacity:.55;cursor:default}input,textarea,select{font:17px/1.5 system-ui,sans-serif;color:inherit;background:white;border:1px solid #778591;padding:10px 12px;border-radius:6px;max-width:100%;width:100%;min-width:0}input[type=checkbox]{width:22px;height:22px}textarea{min-height:110px;resize:vertical}label{display:block;font-weight:700;margin:12px 0 7px}button:focus-visible,input:focus-visible,textarea:focus-visible,summary:focus-visible,[tabindex]:focus-visible{outline:3px solid #aa3d70;outline-offset:4px}.result{padding:18px;border-left:5px solid var(--accent,#24586d);background:var(--soft,#e5eff2);white-space:pre-wrap;overflow-wrap:anywhere;margin:20px 0}.result:empty{display:none}.hint{font-size:16px;color:var(--muted,#435968)}table{border-collapse:collapse;width:100%;table-layout:fixed;margin:16px 0}th,td{text-align:left;vertical-align:top;overflow-wrap:anywhere;padding:12px 8px;border-bottom:1px solid var(--line,#c6d1d8)}caption{text-align:left;font-weight:750;margin:8px 0}details{padding:18px;border:1px dashed #7b8b96;margin:20px 0}summary{cursor:pointer;font-weight:750}pre{font:16px/1.6 ui-monospace,monospace;white-space:pre-wrap;overflow-wrap:anywhere;background:#162735;color:#f5f8fa;padding:18px;border-radius:8px}canvas,svg{display:block;max-width:100%;height:auto}footer{margin:28px 0;font-size:16px}.badge{font-weight:750;color:var(--accent,#24586d)}.metric{font:700 38px/1.3 Georgia,serif}.board{display:grid;gap:5px;max-width:440px;margin:20px auto}.board button{padding:7px;min-height:45px}.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%)}@media(max-width:560px){.grid{grid-template-columns:1fr}main{padding:16px}th,td{padding:10px 5px}.row button{flex-grow:1}.metric{font-size:32px}}
:root{--bg:#162b30;--paper:#203b40;--ink:#f3f6ec;--accent:#346f6d;--soft:#21494a;--muted:#d0e1dd}.collector{grid-template-columns:repeat(7,minmax(0,1fr));max-width:420px}.cell{aspect-ratio:1;background:#315453;color:#f7e7a6;display:flex;align-items:center;justify-content:center;border-radius:4px;font-weight:800;font-size:20px}.cell.wall{background:#14272f}.cell.player{background:#dfc57c;color:#28251d}.cell.guard{background:#b87076;color:#fff}</style></head><body><main><header><p class="eyebrow">Ring collector · Original turn-based model</p><h1>Collect the dots. Watch the patrol.</h1><p class="lede">One legal player action advances both actors from the same snapshot.</p><p class="hint">Original neutral grid collector, not commercial artwork or arcade behavior. The guard moves only on a legal turn.</p></header><section class="panel"><h2>Original ring map</h2><p>P player · G guard · dot collectible · # wall. Coordinates are zero-based.</p><div id="board" class="board collector" tabindex="0" role="group" aria-label="Ring collector, arrow keys move player"><span id="cell0" class="cell"></span><span id="cell1" class="cell"></span><span id="cell2" class="cell"></span><span id="cell3" class="cell"></span><span id="cell4" class="cell"></span><span id="cell5" class="cell"></span><span id="cell6" class="cell"></span><span id="cell7" class="cell"></span><span id="cell8" class="cell"></span><span id="cell9" class="cell"></span><span id="cell10" class="cell"></span><span id="cell11" class="cell"></span><span id="cell12" class="cell"></span><span id="cell13" class="cell"></span><span id="cell14" class="cell"></span><span id="cell15" class="cell"></span><span id="cell16" class="cell"></span><span id="cell17" class="cell"></span><span id="cell18" class="cell"></span><span id="cell19" class="cell"></span><span id="cell20" class="cell"></span><span id="cell21" class="cell"></span><span id="cell22" class="cell"></span><span id="cell23" class="cell"></span><span id="cell24" class="cell"></span><span id="cell25" class="cell"></span><span id="cell26" class="cell"></span><span id="cell27" class="cell"></span><span id="cell28" class="cell"></span><span id="cell29" class="cell"></span><span id="cell30" class="cell"></span><span id="cell31" class="cell"></span><span id="cell32" class="cell"></span><span id="cell33" class="cell"></span><span id="cell34" class="cell"></span></div><div class="row"><button id="up" type="button">up</button><button id="down" type="button">down</button><button id="left" type="button">left</button><button id="right" type="button">right</button></div><p id="status" class="result" role="status"></p><p id="positions"></p><p id="message" role="status"></p><pre id="history"></pre><h2>Staged contact inspections</h2><div class="row"><button id="same" type="button">Shared destination</button><button id="edge" type="button">Exchanged edge</button><button id="last" type="button">Final-item contact</button><button id="reset" type="button" class="secondary">Reset original game</button></div><p id="scenario">Original seed</p></section><footer>Try Final-item contact and explain why the last dot remains after the collision.</footer></main><script>
(()=>{'use strict';const $=id=>document.getElementById(id);
const map=['#######','#P...G#','#.###.#','#.....#','#######'],patrol=[[1,5],[2,5],[3,5],[3,4],[3,3],[3,2],[3,1],[2,1],[1,1],[1,2],[1,3],[1,4]];let player,guardIndex,items,score,turn,phase,history;const key=p=>p.join(',');function seed(){const s=new Set();for(let r=0;r<5;r++)for(let c=0;c<7;c++)if(map[r][c]==='.')s.add(r+','+c);return s;}function render(){const guard=patrol[guardIndex];for(let r=0;r<5;r++)for(let c=0;c<7;c++){const e=$('cell'+(r*7+c)),wall=map[r][c]==='#',p=key(player)===r+','+c,g=key(guard)===r+','+c;e.textContent=wall?'#':p&&g?'P/G':p?'P':g?'G':items.has(r+','+c)?'•':'·';e.className='cell'+(wall?' wall':p?' player':g?' guard':'');}$('status').textContent=phase+' · Turn '+turn+' · Score '+score+' · Remaining '+items.size+' · Conserved '+(score+items.size)+' of 10.';$('positions').textContent='Player ('+key(player)+'); guard ('+key(guard)+'); next patrol ('+key(patrol[(guardIndex+1)%12])+').';$('history').textContent=history.join('\n');}function reset(mode='seed'){player=[1,1];guardIndex=0;items=seed();score=0;turn=0;phase='Playing';history=[];if(mode==='same'||mode==='last'){player=[3,4];guardIndex=1;}if(mode==='edge'){player=[1,5];guardIndex=11;}if(mode==='last'){items=new Set(['3,5']);score=9;}$('scenario').textContent=mode==='seed'?'Original seed':'Staged '+mode+' contact inspection';$('message').textContent='';render();}function move(dr,dc){if(phase!=='Playing')return;const next=[player[0]+dr,player[1]+dc];if(next[0]<0||next[0]>=5||next[1]<0||next[1]>=7||map[next[0]][next[1]]==='#'){$('message').textContent='Blocked: entire turn rejected; guard and counts unchanged.';return;}const old=player.slice(),oldG=patrol[guardIndex],nextIndex=(guardIndex+1)%12,nextG=patrol[nextIndex],same=key(next)===key(nextG),cross=key(next)===key(oldG)&&key(nextG)===key(old);player=next;guardIndex=nextIndex;turn++;if(same||cross){phase='Lost';history.push(turn+': contact '+(same?'same destination':'exchanged edge')+'; no collection.');$('message').textContent='Contact first: Lost. Destination item not collected.';}else{if(items.delete(key(player)))score++;if(!items.size)phase='Won';history.push(turn+': P ('+key(player)+') G ('+key(nextG)+') · score '+score);$('message').textContent=phase==='Won'?'All items collected after surviving the turn.':'Turn survived.';}render();}const dirs={up:[-1,0],down:[1,0],left:[0,-1],right:[0,1]};for(const k of Object.keys(dirs))$(k).addEventListener('click',()=>move(...dirs[k]));$('board').addEventListener('keydown',event=>{if(!['ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(event.key))return;event.preventDefault();if(event.repeat)return;const k=event.key.replace('Arrow','').toLowerCase();move(...dirs[k]);});for(const k of ['same','edge','last'])$(k).addEventListener('click',()=>reset(k));$('reset').addEventListener('click',()=>reset());reset();
})();
</script></body></html>