File size: 5,174 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!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>