UXInteract / task_001 /solution /code /index.html
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
4.84 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>15x15 Gomoku</title>
<style>
body { font-family: system-ui, sans-serif; background: #f3e9d2; text-align: center; margin: 0; padding: 16px; }
h1 { color: #5a3e1b; }
#status { font-size: 18px; margin: 10px; min-height: 24px; font-weight: bold; }
#board {
display: grid;
grid-template-columns: repeat(15, 30px);
grid-template-rows: repeat(15, 30px);
gap: 0;
justify-content: center;
margin: 16px auto;
background: #d9b372;
width: max-content;
border: 2px solid #5a3e1b;
}
.cell {
width: 30px; height: 30px;
box-sizing: border-box;
border: 1px solid #a07b3f;
cursor: pointer;
display: flex; align-items: center; justify-content: center;
}
.stone { width: 24px; height: 24px; border-radius: 50%; }
.black { background: radial-gradient(circle at 35% 35%, #666, #000); }
.white { background: radial-gradient(circle at 35% 35%, #fff, #bbb); }
button { padding: 8px 18px; font-size: 16px; cursor: pointer; border-radius: 6px;
border: none; background: #5a3e1b; color: #fff; }
#easter { font-size: 40px; margin-top: 12px; min-height: 48px; }
</style>
</head>
<body>
<h1>15x15 Gomoku</h1>
<div id="status">Black moves first. Place a stone.</div>
<div id="board"></div>
<button id="reset">Restart</button>
<div id="easter"></div>
<script>
const SIZE = 15; // 15x15 board
const TOTAL = SIZE * SIZE; // 225 intersections
const boardEl = document.getElementById('board');
const statusEl = document.getElementById('status');
const easterEl = document.getElementById('easter');
let grid = [];
let current = 'black';
let gameOver = false;
let celebrationTimer = null;
function buildBoard() {
if (celebrationTimer !== null) {
clearInterval(celebrationTimer);
celebrationTimer = null;
}
boardEl.innerHTML = '';
grid = Array.from({ length: SIZE }, () => Array(SIZE).fill(null));
current = 'black';
gameOver = false;
easterEl.textContent = '';
statusEl.textContent = 'Black moves first. Place a stone.';
for (let r = 0; r < SIZE; r++) {
for (let c = 0; c < SIZE; c++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.r = r;
cell.dataset.c = c;
cell.addEventListener('click', onCellClick);
boardEl.appendChild(cell);
}
}
}
function onCellClick(e) {
if (gameOver) return;
const r = +e.currentTarget.dataset.r;
const c = +e.currentTarget.dataset.c;
if (grid[r][c]) return;
grid[r][c] = current;
const stone = document.createElement('div');
stone.className = 'stone ' + current;
e.currentTarget.appendChild(stone);
if (checkWin(r, c, current)) {
gameOver = true;
const winner = current === 'black' ? 'Black' : 'White';
statusEl.textContent = '🎉 Winner: ' + winner + '!';
showEasterEgg();
return;
}
current = current === 'black' ? 'white' : 'black';
statusEl.textContent = (current === 'black' ? 'Black' : 'White') + "'s turn";
}
function checkWin(r, c, color) {
const dirs = [[1, 0], [0, 1], [1, 1], [1, -1]];
for (const [dr, dc] of dirs) {
let count = 1;
for (let s = 1; s < 5; s++) {
const nr = r + dr * s, nc = c + dc * s;
if (nr < 0 || nr >= SIZE || nc < 0 || nc >= SIZE || grid[nr][nc] !== color) break;
count++;
}
for (let s = 1; s < 5; s++) {
const nr = r - dr * s, nc = c - dc * s;
if (nr < 0 || nr >= SIZE || nc < 0 || nc >= SIZE || grid[nr][nc] !== color) break;
count++;
}
if (count >= 5) return true;
}
return false;
}
// celebration animation shown after a winner is decided
function showEasterEgg() {
const easter = ['🎆', '🎉', '🥚', '✨', '🏆'];
let i = 0;
easterEl.textContent = easter[0].repeat(5);
celebrationTimer = setInterval(() => {
easterEl.textContent = easter[i % easter.length].repeat(5);
if (++i > 10) {
clearInterval(celebrationTimer);
celebrationTimer = null;
}
}, 150);
}
document.getElementById('reset').addEventListener('click', buildBoard);
buildBoard();
window.gameTest = {
reset: buildBoard,
move(r, c) {
if (!Number.isInteger(r) || !Number.isInteger(c) || r < 0 || r >= SIZE || c < 0 || c >= SIZE) return this.state();
const cell = boardEl.children[r * SIZE + c];
if (cell) cell.click();
return this.state();
},
state() { return { size: SIZE, board: grid.map(row => [...row]), current, winner: gameOver ? current : null, easterEgg: gameOver && easterEl.textContent }; }
};
</script>
</body>
</html>