UXInteract / task_004 /solution /code /index.html
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
5.17 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Four-Player Aeroplane Chess</title>
<style>
body { font-family: system-ui, sans-serif; background: #fdf0d5; text-align: center; margin: 0; padding: 16px; }
h1 { color: #c1121f; }
#board {
display: grid;
grid-template-columns: repeat(14, 36px);
gap: 2px;
justify-content: center;
margin: 16px auto;
width: max-content;
}
.cell {
width: 36px; height: 36px;
border: 1px solid #ccc; border-radius: 6px;
display: flex; align-items: center; justify-content: center;
font-size: 18px; background: #fff;
}
.cell.path { background: #e9ecef; }
.controls { margin: 12px; }
#dice { font-size: 28px; margin: 0 12px; }
button { padding: 10px 20px; font-size: 16px; border: none; border-radius: 6px;
background: #c1121f; color: #fff; cursor: pointer; }
#status { font-size: 18px; font-weight: bold; margin: 8px; min-height: 24px; }
#bases { display: flex; justify-content: center; flex-wrap: wrap; gap: 12px; }
.base { padding: 8px; border: 1px solid #aaa; border-radius: 6px; }
</style>
</head>
<body>
<h1>Four-Player Aeroplane Chess</h1>
<div id="status">Player 1's turn. Roll the dice.</div>
<div id="board"></div>
<div id="bases"></div>
<div class="controls">
<button id="roll">Roll Dice</button>
<span id="dice">🎲</span>
</div>
<script>
const TRACK = 52; // length of the route/path around the board
const PLAYERS = 4; // 4 players
const COLORS = ['🔴', '🟡', '🟢', '🔵'];
const HOME = [0, 13, 26, 39]; // each player's start / base square on the path
const boardEl = document.getElementById('board');
const statusEl = document.getElementById('status');
const diceEl = document.getElementById('dice');
// each player has a position along the route; -1 means still at home/base
const players = [
{ id: 1, color: COLORS[0], pos: -1, start: HOME[0], steps: 0 },
{ id: 2, color: COLORS[1], pos: -1, start: HOME[1], steps: 0 },
{ id: 3, color: COLORS[2], pos: -1, start: HOME[2], steps: 0 },
{ id: 4, color: COLORS[3], pos: -1, start: HOME[3], steps: 0 },
];
let currentPlayer = 0; // whose turn it is
let cells = [];
let testRolls = [];
let lastRoll = null;
let finished = false;
function buildBoard() {
boardEl.innerHTML = '';
cells = [];
// render a 14-wide grid; mark the first TRACK cells as the path/route
for (let i = 0; i < 56; i++) {
const cell = document.createElement('div');
cell.className = 'cell' + (i < TRACK ? ' path' : '');
boardEl.appendChild(cell);
cells.push(cell);
}
render();
}
function render() {
cells.forEach(c => { c.textContent = ''; });
for (const player of players) {
if (player.pos >= 0 && player.pos < TRACK) {
cells[player.pos].textContent += player.color;
}
}
document.getElementById('bases').replaceChildren(...players.map(player => {
const base = document.createElement('div');
base.className = 'base';
base.textContent = `Player ${player.id} ${player.color}: ${player.pos < 0 ? 'Base' : 'Track ' + player.pos}`;
return base;
}));
}
function rollDice() {
return testRolls.length ? testRolls.shift() : 1 + Math.floor(Math.random() * 6); // random 1..6
}
// move the current player's token forward by the dice value
function move(steps) {
const player = players[currentPlayer];
if (player.pos < 0) {
// need a 6 to leave home/base, otherwise stay
if (steps === 6) { player.pos = player.start; }
} else {
player.pos = (player.pos + steps) % TRACK;
player.steps += steps;
}
render();
}
function nextTurn(rolledSix) {
if (!rolledSix) {
currentPlayer = (currentPlayer + 1) % PLAYERS;
}
statusEl.textContent = 'Player ' + players[currentPlayer].id + ' ' +
players[currentPlayer].color + "'s turn";
}
document.getElementById('roll').addEventListener('click', () => {
if (finished) return;
const steps = rollDice();
lastRoll = steps;
diceEl.textContent = ['⚀','⚁','⚂','⚃','⚄','⚅'][steps - 1];
const player = players[currentPlayer];
move(steps);
if (player.pos >= 0 && player.steps >= TRACK) {
finished = true;
statusEl.textContent = '🏆 Player ' + player.id + ' wins!';
return;
}
nextTurn(steps === 6);
});
buildBoard();
function performRoll() { document.getElementById('roll').click(); return window.gameTest.state(); }
window.gameTest = {
reset() {
players.forEach(p => { p.pos = -1; p.steps = 0; });
currentPlayer = 0; testRolls = []; lastRoll = null; finished = false;
statusEl.textContent = "Player 1's turn. Roll the dice.";
diceEl.textContent = '🎲';
render();
},
setRolls(values) { testRolls=[...values]; }, roll: performRoll,
state() { return { players: players.map(p=>({id:p.id,pos:p.pos,start:p.start,steps:p.steps})), currentPlayer, lastRoll, trackLength:TRACK }; }
};
</script>
</body>
</html>