Download task_050/solution/code/index.html from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 5.82 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_050/solution/code/index.html
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_050/solution/code/index.html
-
curl -L -o index.html https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_050/solution/code/index.html
5.82 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Reversi / Othello</title> | |
| <style> | |
| body{font-family:sans-serif;background:#1b5e20;color:#fff;text-align:center;margin:0;padding:16px} | |
| h1{font-size:24px;margin:8px} | |
| #hud{font-size:18px;margin:8px} | |
| #board{display:inline-grid;grid-template-columns:repeat(8,44px);grid-gap:2px; | |
| background:#0d3b12;padding:6px;border-radius:6px;margin:12px auto} | |
| .cell{width:44px;height:44px;background:#2e7d32;border-radius:3px;cursor:pointer; | |
| display:flex;align-items:center;justify-content:center} | |
| .cell:hover{background:#388e3c} | |
| .disc{width:34px;height:34px;border-radius:50%;box-shadow:0 1px 2px rgba(0,0,0,.4)} | |
| .disc.B{background:#111} | |
| .disc.W{background:#fafafa} | |
| .cell.legal::after{content:'';width:12px;height:12px;border-radius:50%; | |
| background:rgba(255,255,255,.35)} | |
| button{font-size:16px;padding:6px 12px;margin:4px} | |
| </style> | |
| </head> | |
| <body> | |
| <h1>Reversi / Othello</h1> | |
| <div id="hud"> | |
| Turn: <span data-testid="turn">B</span> · | |
| Black: <span data-testid="count-b">2</span> · | |
| White: <span data-testid="count-w">2</span> · | |
| Status: <span data-testid="status">playing</span> | |
| <span data-testid="winner"></span> | |
| </div> | |
| <div id="board" data-testid="board"></div> | |
| <div> | |
| <button data-testid="new-game">New Game</button> | |
| <p>Click a highlighted square to place a piece.</p> | |
| </div> | |
| <script> | |
| (function(){ | |
| const N = 8; | |
| const DIRS = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]; | |
| const opp = t => (t === 'B' ? 'W' : 'B'); | |
| const inB = (r,c) => r>=0 && r<N && c>=0 && c<N; | |
| const G = { | |
| board: [], turn: 'B', status: 'playing', | |
| init(){ | |
| this.board = Array.from({length:N}, () => Array(N).fill(null)); | |
| this.board[3][3] = 'W'; this.board[3][4] = 'B'; | |
| this.board[4][3] = 'B'; this.board[4][4] = 'W'; | |
| this.turn = 'B'; | |
| this.status = 'playing'; | |
| render(); | |
| return this.getState(); | |
| }, | |
| setState(board, turn){ | |
| this.board = board.map(row => row.map(v => (v === 'B' || v === 'W') ? v : null)); | |
| this.turn = (turn === 'W') ? 'W' : 'B'; | |
| this.status = 'playing'; | |
| // A position with no legal move for either side is already terminal. | |
| if (this._movesFor(this.turn).length === 0 && this._movesFor(opp(this.turn)).length === 0){ | |
| this.status = 'over'; | |
| } | |
| render(); | |
| return this.getState(); | |
| }, | |
| getState(){ | |
| const counts = this._counts(); | |
| let winner = null; | |
| if (this.status === 'over'){ | |
| if (counts.B > counts.W) winner = 'B'; | |
| else if (counts.W > counts.B) winner = 'W'; | |
| else winner = 'draw'; | |
| } | |
| return { | |
| board: this.board.map(r => r.slice()), | |
| turn: this.turn, | |
| status: this.status, | |
| counts: counts, | |
| winner: winner | |
| }; | |
| }, | |
| legalMoves(){ | |
| return this._movesFor(this.turn); | |
| }, | |
| play(r, c){ | |
| if (this.status === 'over') return this.getState(); | |
| const flips = this._flips(r, c, this.turn); | |
| if (flips.length === 0) return this.getState(); // illegal / occupied -> no-op | |
| this.board[r][c] = this.turn; | |
| for (const [fr, fc] of flips) this.board[fr][fc] = this.turn; | |
| // switch turn; auto-pass if opponent has no move; over if neither can move | |
| const next = opp(this.turn); | |
| if (this._movesFor(next).length > 0){ | |
| this.turn = next; | |
| } else if (this._movesFor(this.turn).length > 0){ | |
| // opponent passes; same player moves again (turn stays) | |
| } else { | |
| this.status = 'over'; | |
| } | |
| render(); | |
| return this.getState(); | |
| }, | |
| // ---- internals ---- | |
| _flips(r, c, turn){ | |
| if (!inB(r,c) || this.board[r][c] !== null) return []; | |
| const o = opp(turn); | |
| const all = []; | |
| for (const [dr,dc] of DIRS){ | |
| const line = []; | |
| let nr = r+dr, nc = c+dc; | |
| while (inB(nr,nc) && this.board[nr][nc] === o){ line.push([nr,nc]); nr+=dr; nc+=dc; } | |
| if (line.length && inB(nr,nc) && this.board[nr][nc] === turn) all.push.apply(all, line); | |
| } | |
| return all; | |
| }, | |
| _movesFor(turn){ | |
| const out = []; | |
| for (let r=0;r<N;r++) for (let c=0;c<N;c++) | |
| if (this._flips(r,c,turn).length) out.push([r,c]); | |
| return out; | |
| }, | |
| _counts(){ | |
| let B=0, W=0; | |
| for (const row of this.board) for (const v of row){ if (v==='B') B++; else if (v==='W') W++; } | |
| return { B, W }; | |
| } | |
| }; | |
| function render(){ | |
| const board = document.getElementById('board'); | |
| if (!board) return; | |
| const legal = new Set((G.status === 'playing' ? G.legalMoves() : []).map(m => m[0]+','+m[1])); | |
| board.innerHTML = ''; | |
| for (let r=0;r<N;r++) for (let c=0;c<N;c++){ | |
| const cell = document.createElement('div'); | |
| cell.className = 'cell'; | |
| cell.dataset.r = r; cell.dataset.c = c; | |
| const v = G.board[r][c]; | |
| if (v){ | |
| const d = document.createElement('div'); | |
| d.className = 'disc ' + v; | |
| cell.appendChild(d); | |
| } else if (legal.has(r+','+c)){ | |
| cell.classList.add('legal'); | |
| } | |
| board.appendChild(cell); | |
| } | |
| const st = G.getState(); | |
| const set = (id, val) => { const e = document.querySelector('[data-testid="'+id+'"]'); if (e) e.textContent = val; }; | |
| set('turn', st.turn); | |
| set('count-b', st.counts.B); | |
| set('count-w', st.counts.W); | |
| set('status', st.status); | |
| set('winner', st.winner ? ('· Winner: ' + st.winner) : ''); | |
| } | |
| document.addEventListener('click', e => { | |
| const cell = e.target.closest && e.target.closest('.cell'); | |
| if (cell){ G.play(+cell.dataset.r, +cell.dataset.c); return; } | |
| if (e.target && e.target.matches('[data-testid="new-game"]')) G.init(); | |
| }); | |
| window.__game = G; | |
| G.init(); | |
| })(); | |
| </script> | |
| </body> | |
| </html> | |