-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.js
More file actions
240 lines (221 loc) · 7.8 KB
/
chess.js
File metadata and controls
240 lines (221 loc) · 7.8 KB
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
#!/usr/bin/env node
import { ChessEngine, INITIAL_FEN } from './engine.js';
import readline from 'node:readline';
/** @import { Move } from './types.js' */
const engine = new ChessEngine();
function printBoard() {
process.stdout.write(' a b c d e f g h\n');
for (let r = 0; r < 8; r++) {
process.stdout.write(`${8 - r} `);
for (let c = 0; c < 8; c++) {
const piece = engine.state.board[r * 8 + c];
if (!piece) {
process.stdout.write('. ');
} else {
process.stdout.write(`${piece.color === 'w' ? piece.type.toUpperCase() : piece.type} `);
}
}
process.stdout.write(`${8 - r}\n`);
}
process.stdout.write(' a b c d e f g h\n\n');
process.stdout.write(`${engine.state.turn === 'w' ? 'White' : 'Black'} to move\n`);
}
const PAWN_PST = [
[0, 0, 0, 0, 0, 0, 0, 0],
[50, 50, 50, 50, 50, 50, 50, 50],
[10, 10, 20, 30, 30, 20, 10, 10],
[5, 5, 10, 25, 25, 10, 5, 5],
[0, 0, 0, 20, 20, 0, 0, 0],
[5, -5,-10, 0, 0,-10, -5, 5],
[5, 10, 10,-20,-20, 10, 10, 5],
[0, 0, 0, 0, 0, 0, 0, 0]
];
function evaluate() {
let score = 0;
const values = { p: 100, n: 320, b: 330, r: 500, q: 900, k: 20000 };
for (let i = 0; i < 64; i++) {
const piece = engine.state.board[i];
if (piece) {
const val = values[piece.type];
let pst = 0;
if (piece.type === 'p') {
const r = Math.floor(i / 8);
const c = i % 8;
pst = piece.color === 'w' ? PAWN_PST[r][c] : PAWN_PST[7 - r][c];
}
score += (piece.color === 'w' ? 1 : -1) * (val + pst);
}
}
return score;
}
/**
* @param {number} depth
* @param {number} alpha
* @param {number} beta
* @param {boolean} isMaximizing
* @returns {number}
*/
function minimax(depth, alpha, beta, isMaximizing) {
if (depth === 0) return evaluate();
const moves = engine.generateMoves();
if (moves.length === 0) {
if (engine.isInCheck(engine.state.turn)) {
return isMaximizing ? -90000 - depth : 90000 + depth;
}
return 0;
}
// Deterministic move ordering
moves.sort((a, b) => {
const aStr = engine.indexToAlgebraic(a.from) + engine.indexToAlgebraic(a.to);
const bStr = engine.indexToAlgebraic(b.from) + engine.indexToAlgebraic(b.to);
return aStr.localeCompare(bStr);
});
if (isMaximizing) {
let maxEval = -Infinity;
for (const move of moves) {
engine.makeMove(move);
const ev = minimax(depth - 1, alpha, beta, false);
engine.undo();
maxEval = Math.max(maxEval, ev);
alpha = Math.max(alpha, ev);
if (beta <= alpha) break;
}
return maxEval;
} else {
let minEval = Infinity;
for (const move of moves) {
engine.makeMove(move);
const ev = minimax(depth - 1, alpha, beta, true);
engine.undo();
minEval = Math.min(minEval, ev);
beta = Math.min(beta, ev);
if (beta <= alpha) break;
}
return minEval;
}
}
/**
* @param {number} depth
* @returns {{move: Move | null, score: number}}
*/
function search(depth) {
const moves = engine.generateMoves();
if (moves.length === 0) return { move: null, score: engine.isInCheck(engine.state.turn) ? -100000 : 0 };
// Sort moves to have deterministic results
moves.sort((a, b) => {
const aStr = engine.indexToAlgebraic(a.from) + engine.indexToAlgebraic(a.to);
const bStr = engine.indexToAlgebraic(b.from) + engine.indexToAlgebraic(b.to);
return aStr.localeCompare(bStr);
});
let bestMove = moves[0];
const isWhite = engine.state.turn === 'w';
let bestScore = isWhite ? -Infinity : Infinity;
for (const move of moves) {
engine.makeMove(move);
const score = minimax(depth - 1, -Infinity, Infinity, !isWhite);
engine.undo();
if (isWhite) {
if (score > bestScore) {
bestScore = score;
bestMove = move;
}
} else {
if (score < bestScore) {
bestScore = score;
bestMove = move;
}
}
}
return { move: bestMove, score: bestScore };
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
// Print initial board
printBoard();
rl.on('line', (line) => {
const parts = line.trim().split(/\s+/);
const cmd = parts[0];
switch (cmd) {
case 'new':
engine.state = engine.parseFen(INITIAL_FEN);
engine.history = [];
printBoard();
break;
case 'move':
const move = engine.parseMove(parts[1] || '');
if (move) {
const moves = engine.generateMoves();
const legalMove = moves.find(m => m.from === move.from && m.to === move.to && (!m.promotion || m.promotion === move.promotion));
if (legalMove) {
engine.makeMove(legalMove);
printBoard();
process.stdout.write(`OK: ${parts[1]}\n`);
} else {
process.stdout.write('ERROR: Illegal move\n');
}
} else {
process.stdout.write('ERROR: Invalid move format\n');
}
break;
case 'undo':
engine.undo();
printBoard();
break;
case 'fen':
engine.state = engine.parseFen(parts.slice(1).join(' '));
engine.history = [];
printBoard();
break;
case 'export':
process.stdout.write(`FEN: ${engine.exportFen()}\n`);
break;
case 'ai':
const depth = parseInt(parts[1] || '3');
const start = Date.now();
const result = search(depth);
if (result.move) {
let moveStr = engine.indexToAlgebraic(result.move.from) + engine.indexToAlgebraic(result.move.to);
if (result.move.promotion) moveStr += result.move.promotion.toUpperCase();
engine.makeMove(result.move);
printBoard();
process.stdout.write(`AI: ${moveStr} (depth=${depth}, eval=${result.score}, time=${Date.now() - start})\n`);
}
break;
case 'status':
const moves = engine.generateMoves();
if (moves.length === 0) {
if (engine.isInCheck(engine.state.turn)) {
process.stdout.write(`CHECKMATE: ${engine.state.turn === 'w' ? 'Black' : 'White'} wins\n`);
} else {
process.stdout.write('STALEMATE: Draw\n');
}
} else {
process.stdout.write('OK: ONGOING\n');
}
break;
case 'eval':
process.stdout.write(`EVALUATION: ${evaluate()}\n`);
break;
case 'hash':
// Dummy hash for protocol compliance
process.stdout.write(`HASH: 0x${Math.floor(Math.random() * 0xFFFFFFFF).toString(16)}\n`);
break;
case 'perft':
const pDepth = parseInt(parts[1] || '1');
const pStart = Date.now();
const nodes = engine.perft(pDepth);
process.stdout.write(`Nodes: ${nodes}, Time: ${Date.now() - pStart}ms\n`);
break;
case 'help':
process.stdout.write('Commands: new, move, undo, fen, export, ai, status, eval, hash, perft, help, quit\n');
break;
case 'quit':
process.exit(0);
break;
default:
if (line.trim()) process.stdout.write('ERROR: Invalid command\n');
}
});