File size: 4,843 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
<!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>