File size: 5,818 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<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>