File size: 9,987 Bytes
23fab64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280fe54
23fab64
 
 
 
 
 
 
 
 
 
 
 
 
 
77ac22d
 
 
23fab64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Moonfish Chess</title>
    <link rel="stylesheet" href="https://unpkg.com/@chrisoakman/chessboardjs@1.0.0/dist/chessboard-1.0.0.min.css">
    <style>
        * { box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
            color: #fff;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        h1 {
            text-align: center;
            margin-bottom: 10px;
        }
        .subtitle {
            text-align: center;
            color: #888;
            margin-bottom: 30px;
        }
        .subtitle a { color: #4da6ff; }
        #board-container {
            width: 100%;
            max-width: 500px;
            margin: 0 auto 20px;
        }
        #board { width: 100%; }
        .info-panel {
            background: rgba(255,255,255,0.1);
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
        }
        .status { font-size: 1.2em; margin-bottom: 10px; }
        .controls { display: flex; gap: 10px; flex-wrap: wrap; }
        button {
            background: #4da6ff;
            color: #fff;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1em;
        }
        button:hover { background: #3d8bd9; }
        button:disabled { background: #555; cursor: not-allowed; }
        .move-history {
            background: rgba(0,0,0,0.3);
            border-radius: 5px;
            padding: 10px;
            max-height: 150px;
            overflow-y: auto;
            font-family: monospace;
        }
        .thinking { color: #ffd700; }
        .error { color: #ff6b6b; }
        .win { color: #4ade80; }
        .loss { color: #f87171; }
        .draw { color: #fbbf24; }
        select {
            background: #2a2a4a;
            color: #fff;
            border: 1px solid #555;
            padding: 8px;
            border-radius: 5px;
            font-size: 1em;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>♟️ Moonfish Chess</h1>
        <p class="subtitle">Play against the <a href="https://github.com/luccabb/moonfish" target="_blank" rel="noopener">Moonfish</a> chess engine</p>
        
        <div id="board-container">
            <div id="board"></div>
        </div>
        
        <div class="info-panel">
            <div class="status" id="status">Loading...</div>
            <div class="controls">
                <button id="newGame">New Game</button>
                <select id="playerColor">
                    <option value="white">Play as White</option>
                    <option value="black">Play as Black</option>
                </select>
                <select id="difficulty">
                    <option value="1">Easy</option>
                    <option value="2" selected>Medium</option>
                    <option value="3">Hard</option>
                </select>
            </div>
        </div>
        
        <div class="info-panel">
            <strong>Move History:</strong>
            <div class="move-history" id="moveHistory">-</div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script src="https://unpkg.com/@chrisoakman/chessboardjs@1.0.0/dist/chessboard-1.0.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.3/chess.min.js"></script>
    <script>
        const API_BASE = window.location.origin;
        let board = null;
        let game = new Chess();
        let playerColor = 'white';
        let isThinking = false;

        function updateStatus(msg, className = '') {
            const el = document.getElementById('status');
            el.textContent = msg;
            el.className = 'status ' + className;
        }

        function updateMoveHistory() {
            const moves = game.history();
            let html = '';
            for (let i = 0; i < moves.length; i += 2) {
                const moveNum = Math.floor(i / 2) + 1;
                html += moveNum + '. ' + moves[i];
                if (moves[i + 1]) html += ' ' + moves[i + 1];
                html += '  ';
            }
            document.getElementById('moveHistory').textContent = html || '-';
        }

        function onDragStart(source, piece) {
            if (game.game_over()) return false;
            if (isThinking) return false;
            if ((playerColor === 'white' && piece.search(/^b/) !== -1) ||
                (playerColor === 'black' && piece.search(/^w/) !== -1)) {
                return false;
            }
            if ((game.turn() === 'w' && playerColor !== 'white') ||
                (game.turn() === 'b' && playerColor !== 'black')) {
                return false;
            }
            return true;
        }

        async function makeEngineMove() {
            if (game.game_over()) return;
            
            isThinking = true;
            updateStatus('Moonfish is thinking...', 'thinking');
            
            try {
                // Get legal moves for engine
                const moves = game.moves({ verbose: true });
                if (moves.length === 0) return;
                
                // Call our API to get the best move
                const fen = game.fen();
                const response = await fetch(API_BASE + '/engine-move', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ 
                        fen: fen,
                        depth: parseInt(document.getElementById('difficulty').value)
                    })
                });
                
                if (!response.ok) throw new Error('Engine error');
                
                const data = await response.json();
                const move = game.move({
                    from: data.move.substring(0, 2),
                    to: data.move.substring(2, 4),
                    promotion: data.move.length > 4 ? data.move[4] : undefined
                });
                
                if (move) {
                    board.position(game.fen());
                    updateMoveHistory();
                }
            } catch (err) {
                console.error('Engine error:', err);
                // Fallback: make a random legal move
                const moves = game.moves({ verbose: true });
                if (moves.length > 0) {
                    const randomMove = moves[Math.floor(Math.random() * moves.length)];
                    game.move(randomMove);
                    board.position(game.fen());
                    updateMoveHistory();
                }
            }
            
            isThinking = false;
            checkGameOver();
        }

        function checkGameOver() {
            if (game.in_checkmate()) {
                const winner = game.turn() === 'w' ? 'Black' : 'White';
                const isPlayerWin = (winner.toLowerCase() === playerColor);
                updateStatus(winner + ' wins by checkmate!', isPlayerWin ? 'win' : 'loss');
            } else if (game.in_stalemate()) {
                updateStatus('Draw by stalemate', 'draw');
            } else if (game.in_threefold_repetition()) {
                updateStatus('Draw by repetition', 'draw');
            } else if (game.insufficient_material()) {
                updateStatus('Draw - insufficient material', 'draw');
            } else if (game.in_draw()) {
                updateStatus('Draw', 'draw');
            } else if (game.in_check()) {
                updateStatus(game.turn() === 'w' ? 'White is in check' : 'Black is in check');
            } else {
                updateStatus(game.turn() === 'w' ? 'White to move' : 'Black to move');
            }
        }

        function onDrop(source, target) {
            const move = game.move({
                from: source,
                to: target,
                promotion: 'q'
            });

            if (move === null) return 'snapback';
            
            updateMoveHistory();
            checkGameOver();
            
            if (!game.game_over()) {
                setTimeout(makeEngineMove, 250);
            }
        }

        function onSnapEnd() {
            board.position(game.fen());
        }

        async function newGame() {
            playerColor = document.getElementById('playerColor').value;
            game.reset();
            board.start();
            board.orientation(playerColor);
            updateMoveHistory();
            updateStatus(playerColor === 'white' ? 'Your turn (White)' : 'Moonfish is thinking...', 
                        playerColor === 'black' ? 'thinking' : '');
            
            if (playerColor === 'black') {
                setTimeout(makeEngineMove, 500);
            }
        }

        // Initialize
        const config = {
            draggable: true,
            position: 'start',
            onDragStart: onDragStart,
            onDrop: onDrop,
            onSnapEnd: onSnapEnd,
            pieceTheme: 'https://chessboardjs.com/img/chesspieces/wikipedia/{piece}.png'
        };
        
        board = Chessboard('board', config);
        $(window).resize(() => board.resize());
        
        document.getElementById('newGame').addEventListener('click', newGame);
        document.getElementById('playerColor').addEventListener('change', newGame);
        
        updateStatus('White to move - drag a piece to play!');
    </script>
</body>
</html>