gigachess 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -108
- package/dist/attacks.d.ts +6 -3
- package/dist/attacks.d.ts.map +1 -1
- package/dist/attacks.js +45 -1
- package/dist/board.d.ts +111 -45
- package/dist/board.d.ts.map +1 -1
- package/dist/board.js +747 -14
- package/dist/castling.d.ts +41 -0
- package/dist/castling.d.ts.map +1 -0
- package/dist/castling.js +267 -0
- package/dist/chess.d.ts +30 -28
- package/dist/chess.d.ts.map +1 -1
- package/dist/chess.js +835 -458
- package/dist/chess960.d.ts +1 -1
- package/dist/chess960.d.ts.map +1 -1
- package/dist/chess960.js +2 -2
- package/dist/chessjs.d.ts +13 -0
- package/dist/chessjs.d.ts.map +1 -0
- package/dist/chessjs.js +23 -0
- package/dist/chessops/board.d.ts +1 -1
- package/dist/chessops/board.d.ts.map +1 -1
- package/dist/chessops/chess.js +1 -1
- package/dist/chessops/convert.d.ts +1 -1
- package/dist/chessops/convert.d.ts.map +1 -1
- package/dist/chessops/convert.js +1 -6
- package/dist/chessops/fenInternal.d.ts +1 -1
- package/dist/chessops/fenInternal.d.ts.map +1 -1
- package/dist/chessops/fenInternal.js +2 -2
- package/dist/chessops/index.js +2 -2
- package/dist/chessops/setup.js +1 -1
- package/dist/chessops/types.js +1 -1
- package/dist/chesstree.d.ts +1 -1
- package/dist/chesstree.d.ts.map +1 -1
- package/dist/chesstree.js +5 -5
- package/dist/core.d.ts +1 -1
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +2 -2
- package/dist/fen.d.ts.map +1 -1
- package/dist/fen.js +84 -48
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -8
- package/dist/packedMove.js +2 -2
- package/dist/san.d.ts.map +1 -1
- package/dist/san.js +148 -172
- package/dist/squareSet.js +1 -1
- package/dist/types.d.ts +6 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/zobrist.d.ts +22 -0
- package/dist/zobrist.d.ts.map +1 -1
- package/dist/zobrist.js +107 -36
- package/dist/zobristBlob.js +1 -1
- package/package.json +8 -6
package/dist/chess.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/chess.ts — core chess rules, Position/Setup, dests, isLegal, isCheck, perft etc
|
|
2
|
-
// MIT
|
|
2
|
+
// MIT gigachess, clean-room from specs + FIDE notes (no G P L)
|
|
3
3
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
4
4
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
5
5
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
@@ -11,20 +11,25 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
11
11
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
12
12
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
13
13
|
};
|
|
14
|
-
var _Chess_instances, _Chess_startFen, _Chess_history,
|
|
14
|
+
var _Chess_instances, _Chess_board, _Chess_startFen, _Chess_history, _Chess_movetext, _Chess_positions, _Chess_describe, _Chess_fenOf;
|
|
15
15
|
import * as sq from "./squareSet.js";
|
|
16
16
|
import * as board from "./board.js";
|
|
17
|
+
import { Board } from "./board.js";
|
|
17
18
|
import * as attacks from "./attacks.js";
|
|
18
19
|
import { Color, Role } from "./types.js";
|
|
19
20
|
import { opposite, squareFile, squareRank, parseSquare, squareName } from "./util.js";
|
|
20
|
-
import { zobristTablesLoaded, zobristAfterMove
|
|
21
|
+
import { zobristTablesLoaded, zobristAfterMove } from "./zobrist.js";
|
|
21
22
|
import { parseFen, makeFen } from "./fen.js";
|
|
22
23
|
import { parseSan, makeSan, parseUci, makeUci } from "./san.js";
|
|
23
|
-
import { packOf, packedToMoves } from "./packedMove.js";
|
|
24
|
+
import { packOf, packedToMoves, PROMO_NONE, PROMO_KNIGHT, PROMO_BISHOP, PROMO_ROOK, PROMO_QUEEN, } from "./packedMove.js";
|
|
24
25
|
import { build as buildTreeWrapper, pgnImport as pgnImportData } from "./chesstree.js";
|
|
25
26
|
import { pieceAt } from "./board.js";
|
|
27
|
+
import { CASTLE_WK, CASTLE_WQ, CASTLE_BK, CASTLE_BQ, CASTLE_CLEAR_STD, CASTLING_RIGHTS_TABLE, PLAN_WHITE_K, PLAN_WHITE_Q, PLAN_BLACK_K, PLAN_BLACK_Q, getCastlingMask, CASTLE_PATH_LO, CASTLE_PATH_HI, CASTLE_TRAVERSAL_WHITE, CASTLE_TRAVERSAL_BLACK, } from "./castling.js";
|
|
26
28
|
// helpers
|
|
27
29
|
export function isCheck(pos) {
|
|
30
|
+
if (pos.checkers !== undefined) {
|
|
31
|
+
return ((pos.checkers.lo | pos.checkers.hi) !== 0);
|
|
32
|
+
}
|
|
28
33
|
const ksq = board.kingSquare(pos.board, pos.turn);
|
|
29
34
|
if (ksq === undefined)
|
|
30
35
|
return false;
|
|
@@ -129,73 +134,22 @@ function positionsEqualForRepetition(a, b) {
|
|
|
129
134
|
return false;
|
|
130
135
|
return true;
|
|
131
136
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
if (p.rookFrom === to || p.kingTo === to)
|
|
149
|
-
return p;
|
|
150
|
-
}
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Single source of truth for castling detection, shared by makeMove, isLegal,
|
|
155
|
-
* makeSan, destsFast and genLegalMoves (design D2). Accepts both input
|
|
156
|
-
* representations:
|
|
157
|
-
* - king→own-rook square (baseline / 960 input, e1h1), and
|
|
158
|
-
* - king→normalized landing square (e1g1; a two-file king step),
|
|
159
|
-
* provided the corresponding castling right exists and the rook is actually
|
|
160
|
-
* on its origin square. Returns null for every non-castling move — in
|
|
161
|
-
* particular for an ordinary one-square king step to g1/c1/g8/c8, which the
|
|
162
|
-
* previous per-call-site heuristics misclassified when ANY color (even the
|
|
163
|
-
* opponent) held castling rights (root cause of the perft parity defect).
|
|
164
|
-
*/
|
|
165
|
-
export function detectCastling(pos, from, to) {
|
|
166
|
-
if (pos.castling.white.size === 0 && pos.castling.black.size === 0)
|
|
167
|
-
return null;
|
|
168
|
-
const piece = board.pieceAt(pos.board, from);
|
|
169
|
-
if (!piece || piece.role !== Role.King || piece.color !== pos.turn)
|
|
170
|
-
return null;
|
|
171
|
-
const rights = piece.color === Color.White ? pos.castling.white : pos.castling.black;
|
|
172
|
-
if (rights.size === 0)
|
|
173
|
-
return null;
|
|
174
|
-
const rank = squareRank(from);
|
|
175
|
-
// Input form 1: king captures own rook on its origin square (baseline/960).
|
|
176
|
-
if (rights.has(to) && squareRank(to) === rank) {
|
|
177
|
-
const target = board.pieceAt(pos.board, to);
|
|
178
|
-
if (target && target.color === piece.color && target.role === Role.Rook) {
|
|
179
|
-
return castlingPlanFor(piece.color, from, to);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
// Input form 2: normalized landing square — a two-file step on the same rank.
|
|
183
|
-
if (squareRank(to) === rank && Math.abs(squareFile(to) - squareFile(from)) === 2) {
|
|
184
|
-
// The landing square must be empty for castling.
|
|
185
|
-
if (!board.pieceAt(pos.board, to)) {
|
|
186
|
-
for (const rs of rights) {
|
|
187
|
-
const plan = castlingPlanFor(piece.color, from, rs);
|
|
188
|
-
if (plan.kingTo !== to)
|
|
189
|
-
continue;
|
|
190
|
-
// The right's rook must actually be present on its origin square.
|
|
191
|
-
const rp = board.pieceAt(pos.board, rs);
|
|
192
|
-
if (rp && rp.color === piece.color && rp.role === Role.Rook)
|
|
193
|
-
return plan;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return null;
|
|
198
|
-
}
|
|
137
|
+
// ---------- castling: one detect/apply path (ADR-013 as amended) ----------
|
|
138
|
+
// ADR-013 bake-off result (change purechess-gates-green, task 1.1): the
|
|
139
|
+
// baseline-style king-captures-rook representation (e1h1) is the single
|
|
140
|
+
// canonical OUTPUT encoding used by dests/allDests/genLegalMoves (and hence
|
|
141
|
+
// makeUci). It measured equal-or-faster than the normalized-landing encoding
|
|
142
|
+
// (perft d3 11.26 vs 11.42 ms median; makeMove walk 425 vs 430 ms; allDests
|
|
143
|
+
// 6.27 vs 6.39 ms — see bench/castling-bakeoff.mjs) and deletes the
|
|
144
|
+
// canonicalization layer: standard-chess and Chess960 handling converge and
|
|
145
|
+
// dests are byte-identical to baseline. Both INPUT forms are still accepted
|
|
146
|
+
// by detectCastling (king→rook square e1h1, and king→landing e1g1 as a
|
|
147
|
+
// two-file step) so `parseUci("e1g1")` in standard chess and 960 `e1h1`
|
|
148
|
+
// input keep working (UCI protocol boundary documented in ADR-013: engines
|
|
149
|
+
// are sent e1g1 for standard castling; conversion belongs to the engine
|
|
150
|
+
// boundary, not to makeUci).
|
|
151
|
+
import { detectCastling, castlingPlanFor, planForDest, } from "./castling.js";
|
|
152
|
+
export { detectCastling, castlingPlanFor, planForDest, };
|
|
199
153
|
// ---------- move execution (play) ----------
|
|
200
154
|
/**
|
|
201
155
|
* Shared board-edit sequence used by BOTH the pure `makeMove` and the
|
|
@@ -296,63 +250,68 @@ export function makeMove(pos, move) {
|
|
|
296
250
|
applyBoardEdits(nb, pos, from, to, move.to, piece, isEnPassant, plan, isPromotion, move.promotion ?? null, captured);
|
|
297
251
|
// handle promotion: pawn must promote if reaching back rank
|
|
298
252
|
// Already handled via move.promotion
|
|
299
|
-
// update castling rights —
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
let newBlack = pos.castling.black;
|
|
307
|
-
// if king moves, remove all rights for that color — a fresh empty set; no
|
|
308
|
-
// copy of the old one is needed
|
|
309
|
-
if (piece.role === Role.King) {
|
|
310
|
-
if (pos.turn === Color.White) {
|
|
311
|
-
if (newWhite.size > 0)
|
|
312
|
-
newWhite = new Set();
|
|
313
|
-
}
|
|
314
|
-
else {
|
|
315
|
-
if (newBlack.size > 0)
|
|
316
|
-
newBlack = new Set();
|
|
317
|
-
}
|
|
253
|
+
// update castling rights — standard chess uses 4-bit integer mask and O(1) table lookup
|
|
254
|
+
let newCastling;
|
|
255
|
+
let newMask;
|
|
256
|
+
if (!pos.isChess960) {
|
|
257
|
+
const mask = pos.castlingMask ?? getCastlingMask(pos);
|
|
258
|
+
newMask = (mask & CASTLE_CLEAR_STD[from] & CASTLE_CLEAR_STD[to]) >>> 0;
|
|
259
|
+
newCastling = CASTLING_RIGHTS_TABLE[newMask];
|
|
318
260
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
if (
|
|
323
|
-
if (
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
newWhite = next;
|
|
261
|
+
else {
|
|
262
|
+
let newWhite = pos.castling.white;
|
|
263
|
+
let newBlack = pos.castling.black;
|
|
264
|
+
if (piece.role === Role.King) {
|
|
265
|
+
if (pos.turn === Color.White) {
|
|
266
|
+
if (newWhite.size > 0)
|
|
267
|
+
newWhite = new Set();
|
|
327
268
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
const next = new Set(newBlack);
|
|
332
|
-
next.delete(from);
|
|
333
|
-
newBlack = next;
|
|
269
|
+
else {
|
|
270
|
+
if (newBlack.size > 0)
|
|
271
|
+
newBlack = new Set();
|
|
334
272
|
}
|
|
335
273
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
274
|
+
if (piece.role === Role.Rook) {
|
|
275
|
+
if (pos.turn === Color.White) {
|
|
276
|
+
if (newWhite.has(from)) {
|
|
277
|
+
const next = new Set(newWhite);
|
|
278
|
+
next.delete(from);
|
|
279
|
+
newWhite = next;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
if (newBlack.has(from)) {
|
|
284
|
+
const next = new Set(newBlack);
|
|
285
|
+
next.delete(from);
|
|
286
|
+
newBlack = next;
|
|
287
|
+
}
|
|
344
288
|
}
|
|
345
289
|
}
|
|
346
|
-
|
|
347
|
-
if (
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
290
|
+
if (captured && captured.role === Role.Rook) {
|
|
291
|
+
if (captured.color === Color.White) {
|
|
292
|
+
if (newWhite.has(to)) {
|
|
293
|
+
const next = new Set(newWhite);
|
|
294
|
+
next.delete(to);
|
|
295
|
+
newWhite = next;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
if (newBlack.has(to)) {
|
|
300
|
+
const next = new Set(newBlack);
|
|
301
|
+
next.delete(to);
|
|
302
|
+
newBlack = next;
|
|
303
|
+
}
|
|
351
304
|
}
|
|
352
305
|
}
|
|
306
|
+
newCastling = {
|
|
307
|
+
white: newWhite,
|
|
308
|
+
black: newBlack,
|
|
309
|
+
whiteKing: newWhite.has(7),
|
|
310
|
+
whiteQueen: newWhite.has(0),
|
|
311
|
+
blackKing: newBlack.has(63),
|
|
312
|
+
blackQueen: newBlack.has(56),
|
|
313
|
+
};
|
|
353
314
|
}
|
|
354
|
-
// en passant capture already handled (captured pawn not rook, so no)
|
|
355
|
-
// if rook moved in castling, its origin already removed, but rook destination not relevant
|
|
356
315
|
// en passant square for next position
|
|
357
316
|
let newEp = null;
|
|
358
317
|
if (piece.role === Role.Pawn && Math.abs(squareRank(to) - squareRank(from)) === 2) {
|
|
@@ -367,23 +326,27 @@ export function makeMove(pos, move) {
|
|
|
367
326
|
// fullmove
|
|
368
327
|
const newFull = pos.turn === Color.Black ? (pos.fullmoves ?? pos.fullmove ?? 1) + 1 : (pos.fullmoves ?? pos.fullmove ?? 1);
|
|
369
328
|
const newTurn = opposite(pos.turn);
|
|
370
|
-
const
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
329
|
+
const nextKingSq = pos.kingSq
|
|
330
|
+
? [pos.kingSq[0], pos.kingSq[1]]
|
|
331
|
+
: [board.kingSquare(pos.board, Color.White) ?? -1, board.kingSquare(pos.board, Color.Black) ?? -1];
|
|
332
|
+
if (piece.role === Role.King) {
|
|
333
|
+
nextKingSq[pos.turn] = to;
|
|
334
|
+
}
|
|
335
|
+
nb.kingSq = nextKingSq;
|
|
336
|
+
const newCheckers = attacks.kingAttackers(nb, newTurn);
|
|
378
337
|
const newPos = {
|
|
379
338
|
board: nb,
|
|
380
339
|
turn: newTurn,
|
|
381
340
|
castling: newCastling,
|
|
341
|
+
castlingMask: newMask,
|
|
342
|
+
isChess960: pos.isChess960 ?? false,
|
|
382
343
|
epSquare: newEp,
|
|
383
344
|
halfmoves: newHalf,
|
|
384
345
|
fullmoves: newFull,
|
|
385
346
|
halfmove: newHalf,
|
|
386
347
|
fullmove: newFull,
|
|
348
|
+
kingSq: nextKingSq,
|
|
349
|
+
checkers: newCheckers,
|
|
387
350
|
};
|
|
388
351
|
// Incremental O(1) Zobrist maintenance (design D2). Skipped (positions
|
|
389
352
|
// carry no zobrist fields) until the Polyglot key tables have loaded via
|
|
@@ -400,59 +363,96 @@ export function makeMove(pos, move) {
|
|
|
400
363
|
// alias play
|
|
401
364
|
export const play = makeMove;
|
|
402
365
|
// ---------- pseudo-legal generation ----------
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
366
|
+
const P_W_P = { color: Color.White, role: Role.Pawn };
|
|
367
|
+
const P_W_N = { color: Color.White, role: Role.Knight };
|
|
368
|
+
const P_W_B = { color: Color.White, role: Role.Bishop };
|
|
369
|
+
const P_W_R = { color: Color.White, role: Role.Rook };
|
|
370
|
+
const P_W_Q = { color: Color.White, role: Role.Queen };
|
|
371
|
+
const P_W_K = { color: Color.White, role: Role.King };
|
|
372
|
+
const P_B_P = { color: Color.Black, role: Role.Pawn };
|
|
373
|
+
const P_B_N = { color: Color.Black, role: Role.Knight };
|
|
374
|
+
const P_B_B = { color: Color.Black, role: Role.Bishop };
|
|
375
|
+
const P_B_R = { color: Color.Black, role: Role.Rook };
|
|
376
|
+
const P_B_Q = { color: Color.Black, role: Role.Queen };
|
|
377
|
+
const P_B_K = { color: Color.Black, role: Role.King };
|
|
378
|
+
function whitePawnPseudoDests(pos, from) {
|
|
409
379
|
const occ = pos.board.occupied;
|
|
410
|
-
const
|
|
411
|
-
const isWhite = color === Color.White;
|
|
380
|
+
const them = pos.board.black;
|
|
412
381
|
let lo = 0, hi = 0;
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
|
|
382
|
+
const to1 = from + 8;
|
|
383
|
+
if (!sq.has(occ, to1)) {
|
|
384
|
+
if (to1 < 32)
|
|
385
|
+
lo |= (1 << to1) >>> 0;
|
|
416
386
|
else
|
|
417
|
-
hi |= (1 << (
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
const oneSq = oneRank * 8 + file;
|
|
423
|
-
if (!sq.has(occ, oneSq)) {
|
|
424
|
-
add(oneSq);
|
|
425
|
-
// double from starting rank
|
|
426
|
-
const startRank = color === Color.White ? 1 : 6;
|
|
427
|
-
if (rank === startRank) {
|
|
428
|
-
const twoRank = rank + dir * 2;
|
|
429
|
-
const twoSq = twoRank * 8 + file;
|
|
430
|
-
if (!sq.has(occ, twoSq))
|
|
431
|
-
add(twoSq);
|
|
387
|
+
hi |= (1 << (to1 - 32)) >>> 0;
|
|
388
|
+
if (from >= 8 && from <= 15) {
|
|
389
|
+
const to2 = from + 16;
|
|
390
|
+
if (!sq.has(occ, to2)) {
|
|
391
|
+
lo |= (1 << to2) >>> 0;
|
|
432
392
|
}
|
|
433
393
|
}
|
|
434
394
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
const
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
// target color via color-occupancy membership instead of pieceAt object
|
|
444
|
-
if (sq.has(whiteOcc, capSq) !== isWhite)
|
|
445
|
-
add(capSq);
|
|
395
|
+
const f = from & 7;
|
|
396
|
+
if (f > 0) {
|
|
397
|
+
const capL = from + 7;
|
|
398
|
+
if (sq.has(them, capL) || pos.epSquare === capL) {
|
|
399
|
+
if (capL < 32)
|
|
400
|
+
lo |= (1 << capL) >>> 0;
|
|
401
|
+
else
|
|
402
|
+
hi |= (1 << (capL - 32)) >>> 0;
|
|
446
403
|
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
404
|
+
}
|
|
405
|
+
if (f < 7) {
|
|
406
|
+
const capR = from + 9;
|
|
407
|
+
if (sq.has(them, capR) || pos.epSquare === capR) {
|
|
408
|
+
if (capR < 32)
|
|
409
|
+
lo |= (1 << capR) >>> 0;
|
|
410
|
+
else
|
|
411
|
+
hi |= (1 << (capR - 32)) >>> 0;
|
|
450
412
|
}
|
|
451
413
|
}
|
|
452
414
|
return { lo: lo >>> 0, hi: hi >>> 0 };
|
|
453
415
|
}
|
|
454
|
-
function
|
|
455
|
-
const
|
|
416
|
+
function blackPawnPseudoDests(pos, from) {
|
|
417
|
+
const occ = pos.board.occupied;
|
|
418
|
+
const them = pos.board.white;
|
|
419
|
+
let lo = 0, hi = 0;
|
|
420
|
+
const to1 = from - 8;
|
|
421
|
+
if (!sq.has(occ, to1)) {
|
|
422
|
+
if (to1 < 32)
|
|
423
|
+
lo |= (1 << to1) >>> 0;
|
|
424
|
+
else
|
|
425
|
+
hi |= (1 << (to1 - 32)) >>> 0;
|
|
426
|
+
if (from >= 48 && from <= 55) {
|
|
427
|
+
const to2 = from - 16;
|
|
428
|
+
if (!sq.has(occ, to2)) {
|
|
429
|
+
hi |= (1 << (to2 - 32)) >>> 0;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
const f = from & 7;
|
|
434
|
+
if (f > 0) {
|
|
435
|
+
const capL = from - 9;
|
|
436
|
+
if (sq.has(them, capL) || pos.epSquare === capL) {
|
|
437
|
+
if (capL < 32)
|
|
438
|
+
lo |= (1 << capL) >>> 0;
|
|
439
|
+
else
|
|
440
|
+
hi |= (1 << (capL - 32)) >>> 0;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (f < 7) {
|
|
444
|
+
const capR = from - 7;
|
|
445
|
+
if (sq.has(them, capR) || pos.epSquare === capR) {
|
|
446
|
+
if (capR < 32)
|
|
447
|
+
lo |= (1 << capR) >>> 0;
|
|
448
|
+
else
|
|
449
|
+
hi |= (1 << (capR - 32)) >>> 0;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return { lo: lo >>> 0, hi: hi >>> 0 };
|
|
453
|
+
}
|
|
454
|
+
function genPseudoDests(pos, from, piece) {
|
|
455
|
+
const p = piece ?? board.pieceAt(pos.board, from);
|
|
456
456
|
if (!p || p.color !== pos.turn)
|
|
457
457
|
return sq.empty();
|
|
458
458
|
const occ = pos.board.occupied;
|
|
@@ -460,7 +460,7 @@ function genPseudoDests(pos, from) {
|
|
|
460
460
|
let pseudo;
|
|
461
461
|
switch (p.role) {
|
|
462
462
|
case Role.Pawn:
|
|
463
|
-
pseudo =
|
|
463
|
+
pseudo = p.color === Color.White ? whitePawnPseudoDests(pos, from) : blackPawnPseudoDests(pos, from);
|
|
464
464
|
break;
|
|
465
465
|
case Role.Knight:
|
|
466
466
|
pseudo = attacks.knightAttacks(from);
|
|
@@ -486,76 +486,102 @@ function genPseudoDests(pos, from) {
|
|
|
486
486
|
// `from` IS this color's king square by definition (invariant: exactly
|
|
487
487
|
// one king per color), so no kingSquare lookup is needed here.
|
|
488
488
|
// Conditions to add castling dest: rights exist, between empty, not in check, traversal not attacked
|
|
489
|
-
if (
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
489
|
+
if (!pos.isChess960) {
|
|
490
|
+
const mask = pos.castlingMask ?? getCastlingMask(pos);
|
|
491
|
+
if (p.color === Color.White) {
|
|
492
|
+
if (ks === 4 && (mask & (CASTLE_WK | CASTLE_WQ)) !== 0 && !isCheck(pos)) {
|
|
493
|
+
const occLo = occ.lo;
|
|
494
|
+
if ((mask & CASTLE_WK) !== 0 && (occLo & 0x60) === 0) {
|
|
495
|
+
const rp = board.pieceAt(pos.board, 7);
|
|
496
|
+
if (rp && rp.color === Color.White && rp.role === Role.Rook) {
|
|
497
|
+
if (!attacks.isAttacked(pos.board, 5, Color.Black) && !attacks.isAttacked(pos.board, 6, Color.Black)) {
|
|
498
|
+
pseudo = sq.or(pseudo, sq.singleton(7));
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
if ((mask & CASTLE_WQ) !== 0 && (occLo & 0x0E) === 0) {
|
|
503
|
+
const rp = board.pieceAt(pos.board, 0);
|
|
504
|
+
if (rp && rp.color === Color.White && rp.role === Role.Rook) {
|
|
505
|
+
if (!attacks.isAttacked(pos.board, 3, Color.Black) && !attacks.isAttacked(pos.board, 2, Color.Black)) {
|
|
506
|
+
pseudo = sq.or(pseudo, sq.singleton(0));
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
if (ks === 60 && (mask & (CASTLE_BK | CASTLE_BQ)) !== 0 && !isCheck(pos)) {
|
|
514
|
+
const occHi = occ.hi;
|
|
515
|
+
if ((mask & CASTLE_BK) !== 0 && (occHi & 0x60000000) === 0) {
|
|
516
|
+
const rp = board.pieceAt(pos.board, 63);
|
|
517
|
+
if (rp && rp.color === Color.Black && rp.role === Role.Rook) {
|
|
518
|
+
if (!attacks.isAttacked(pos.board, 61, Color.White) && !attacks.isAttacked(pos.board, 62, Color.White)) {
|
|
519
|
+
pseudo = sq.or(pseudo, sq.singleton(63));
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
if ((mask & CASTLE_BQ) !== 0 && (occHi & 0x0E000000) === 0) {
|
|
524
|
+
const rp = board.pieceAt(pos.board, 56);
|
|
525
|
+
if (rp && rp.color === Color.Black && rp.role === Role.Rook) {
|
|
526
|
+
if (!attacks.isAttacked(pos.board, 59, Color.White) && !attacks.isAttacked(pos.board, 58, Color.White)) {
|
|
527
|
+
pseudo = sq.or(pseudo, sq.singleton(56));
|
|
528
|
+
}
|
|
521
529
|
}
|
|
522
530
|
}
|
|
523
|
-
if (attacked)
|
|
524
|
-
continue;
|
|
525
|
-
// Also need dest not occupied by own? already filtered via own? But G1/C1 between empty ensures not occupied, but check if dest is occupied by opponent capture? Castling destination should be empty, but could be rook capture? No, destination must be empty per between check. For queen-side, B1 may be occupied but dest C1 must be empty; between includes B1,C1,D1, so if B1 occupied, fails. So okay.
|
|
526
|
-
// Add dest
|
|
527
|
-
const bit = sq.singleton(dest);
|
|
528
|
-
pseudo = sq.or(pseudo, bit);
|
|
529
531
|
}
|
|
530
532
|
}
|
|
531
533
|
}
|
|
532
|
-
else
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
534
|
+
else {
|
|
535
|
+
// Chess960 fallback using precomputed CASTLE_PATH and traversal tables
|
|
536
|
+
if (p.color === Color.White) {
|
|
537
|
+
if (pos.castling.white.size > 0 && !isCheck(pos)) {
|
|
538
|
+
const kingFile = ks & 7;
|
|
539
|
+
for (const rs of pos.castling.white) {
|
|
540
|
+
const rp = board.pieceAt(pos.board, rs);
|
|
541
|
+
if (!rp || rp.color !== p.color || rp.role !== Role.Rook)
|
|
542
|
+
continue;
|
|
543
|
+
const rookFile = rs & 7;
|
|
544
|
+
const idx = (kingFile << 3) | rookFile;
|
|
545
|
+
if (((CASTLE_PATH_LO[idx] & occ.lo) | (CASTLE_PATH_HI[idx] & occ.hi)) !== 0)
|
|
546
|
+
continue;
|
|
547
|
+
const trav = CASTLE_TRAVERSAL_WHITE[idx];
|
|
548
|
+
let attacked = false;
|
|
549
|
+
for (let ti = 0; ti < trav.length; ti++) {
|
|
550
|
+
if (attacks.isAttacked(pos.board, trav[ti], Color.Black)) {
|
|
551
|
+
attacked = true;
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (attacked)
|
|
556
|
+
continue;
|
|
557
|
+
pseudo = sq.or(pseudo, sq.singleton(rs));
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
else {
|
|
562
|
+
if (pos.castling.black.size > 0 && !isCheck(pos)) {
|
|
563
|
+
const kingFile = ks & 7;
|
|
564
|
+
for (const rs of pos.castling.black) {
|
|
565
|
+
const rp = board.pieceAt(pos.board, rs);
|
|
566
|
+
if (!rp || rp.color !== p.color || rp.role !== Role.Rook)
|
|
567
|
+
continue;
|
|
568
|
+
const rookFile = rs & 7;
|
|
569
|
+
const idx = 64 | (kingFile << 3) | rookFile;
|
|
570
|
+
if (((CASTLE_PATH_LO[idx] & occ.lo) | (CASTLE_PATH_HI[idx] & occ.hi)) !== 0)
|
|
571
|
+
continue;
|
|
572
|
+
const trav = CASTLE_TRAVERSAL_BLACK[idx & 63];
|
|
573
|
+
let attacked = false;
|
|
574
|
+
for (let ti = 0; ti < trav.length; ti++) {
|
|
575
|
+
if (attacks.isAttacked(pos.board, trav[ti], Color.White)) {
|
|
576
|
+
attacked = true;
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (attacked)
|
|
581
|
+
continue;
|
|
582
|
+
pseudo = sq.or(pseudo, sq.singleton(rs));
|
|
554
583
|
}
|
|
555
584
|
}
|
|
556
|
-
if (attacked)
|
|
557
|
-
continue;
|
|
558
|
-
pseudo = sq.or(pseudo, sq.singleton(dest));
|
|
559
585
|
}
|
|
560
586
|
}
|
|
561
587
|
break;
|
|
@@ -594,11 +620,6 @@ isPromotion, promotion) {
|
|
|
594
620
|
return false;
|
|
595
621
|
return !attacks.isAttacked(destScratch, ksq, opposite(pos.turn));
|
|
596
622
|
}
|
|
597
|
-
// Module-level scratch, sanctioned by the FP policy for hot loops: owned by
|
|
598
|
-
// analyzeCheckContext (a leaf — no re-entrant movegen runs while a context is
|
|
599
|
-
// live within one dests/allDests/genLegalMoves call) and cleared before every
|
|
600
|
-
// use. Never escapes the enclosing call.
|
|
601
|
-
const scratchPinRays = new Map();
|
|
602
623
|
// Reused castling plan scratch (same ownership discipline): avoids allocating
|
|
603
624
|
// two plan objects per analyzed position in the perft/movegen hot loop. The
|
|
604
625
|
// referenced position data is read-only and consumed within the same call.
|
|
@@ -608,37 +629,64 @@ const scratchCastlingPlanB = { side: "king", kingFrom: 0, kingTo: 0, rookFrom: 0
|
|
|
608
629
|
export function analyzeCheckContext(pos) {
|
|
609
630
|
const us = pos.turn;
|
|
610
631
|
const them = opposite(us);
|
|
611
|
-
scratchPinRays.clear();
|
|
612
632
|
const ksq = board.kingSquare(pos.board, us);
|
|
613
633
|
if (ksq === undefined) {
|
|
614
634
|
// degenerate (kingless) position: no pins, no masks
|
|
615
|
-
return { us, ksq: -1, doubleCheck: false, checkMask: sq.FULL, kingSafe: sq.EMPTY,
|
|
635
|
+
return { us, ksq: -1, doubleCheck: false, checkMask: sq.FULL, kingSafe: sq.EMPTY, pinned: sq.EMPTY, castlingPlans: [] };
|
|
616
636
|
}
|
|
617
|
-
const ctx = { us, ksq, doubleCheck: false, checkMask: sq.FULL, kingSafe: sq.EMPTY,
|
|
637
|
+
const ctx = { us, ksq, doubleCheck: false, checkMask: sq.FULL, kingSafe: sq.EMPTY, pinned: sq.EMPTY, castlingPlans: [] };
|
|
618
638
|
// Precompute the position's castling plans once (design D2): the hot loops
|
|
619
639
|
// (destsFast king branch, genLegalMoves) look them up instead of re-running
|
|
620
640
|
// the full detectCastling per king move. Plans are written into the reused
|
|
621
641
|
// scratch array — no allocation in the hot loop.
|
|
622
642
|
ctx.castlingPlans = scratchCastlingPlans;
|
|
623
643
|
ctx.castlingPlans.length = 0;
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
644
|
+
if (!pos.isChess960) {
|
|
645
|
+
const mask = pos.castlingMask ?? getCastlingMask(pos);
|
|
646
|
+
if (us === Color.White) {
|
|
647
|
+
if ((mask & CASTLE_WK) !== 0 && ksq === 4) {
|
|
648
|
+
const rp = board.pieceAt(pos.board, 7);
|
|
649
|
+
if (rp && rp.color === Color.White && rp.role === Role.Rook)
|
|
650
|
+
ctx.castlingPlans.push(PLAN_WHITE_K);
|
|
651
|
+
}
|
|
652
|
+
if ((mask & CASTLE_WQ) !== 0 && ksq === 4) {
|
|
653
|
+
const rp = board.pieceAt(pos.board, 0);
|
|
654
|
+
if (rp && rp.color === Color.White && rp.role === Role.Rook)
|
|
655
|
+
ctx.castlingPlans.push(PLAN_WHITE_Q);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
else {
|
|
659
|
+
if ((mask & CASTLE_BK) !== 0 && ksq === 60) {
|
|
660
|
+
const rp = board.pieceAt(pos.board, 63);
|
|
661
|
+
if (rp && rp.color === Color.Black && rp.role === Role.Rook)
|
|
662
|
+
ctx.castlingPlans.push(PLAN_BLACK_K);
|
|
663
|
+
}
|
|
664
|
+
if ((mask & CASTLE_BQ) !== 0 && ksq === 60) {
|
|
665
|
+
const rp = board.pieceAt(pos.board, 56);
|
|
666
|
+
if (rp && rp.color === Color.Black && rp.role === Role.Rook)
|
|
667
|
+
ctx.castlingPlans.push(PLAN_BLACK_Q);
|
|
638
668
|
}
|
|
639
669
|
}
|
|
640
670
|
}
|
|
641
|
-
|
|
671
|
+
else {
|
|
672
|
+
const rights = us === Color.White ? pos.castling.white : pos.castling.black;
|
|
673
|
+
if (rights.size > 0) {
|
|
674
|
+
for (const rs of rights) {
|
|
675
|
+
const rp = board.pieceAt(pos.board, rs);
|
|
676
|
+
if (rp && rp.color === us && rp.role === Role.Rook) {
|
|
677
|
+
const slot = ctx.castlingPlans.length === 0 ? scratchCastlingPlanA : scratchCastlingPlanB;
|
|
678
|
+
const next = castlingPlanFor(us, ksq, rs);
|
|
679
|
+
slot.side = next.side;
|
|
680
|
+
slot.kingFrom = next.kingFrom;
|
|
681
|
+
slot.kingTo = next.kingTo;
|
|
682
|
+
slot.rookFrom = next.rookFrom;
|
|
683
|
+
slot.rookTo = next.rookTo;
|
|
684
|
+
ctx.castlingPlans.push(slot);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
const checkers = pos.checkers ?? attacks.kingAttackers(pos.board, us);
|
|
642
690
|
const nCheckers = sq.popcount(checkers);
|
|
643
691
|
if (nCheckers === 1) {
|
|
644
692
|
const c = sq.first(checkers);
|
|
@@ -654,16 +702,45 @@ export function analyzeCheckContext(pos) {
|
|
|
654
702
|
const themRQ = sq.or(sq.and(themOcc, pos.board.rook), sq.and(themOcc, pos.board.queen));
|
|
655
703
|
const noOcc = sq.EMPTY;
|
|
656
704
|
const snipers = sq.or(sq.and(themRQ, attacks.rookAttacks(ksq, noOcc)), sq.and(themBQ, attacks.bishopAttacks(ksq, noOcc)));
|
|
657
|
-
|
|
705
|
+
let pinnedLo = 0, pinnedHi = 0;
|
|
706
|
+
let sLo = snipers.lo >>> 0, sHi = snipers.hi >>> 0;
|
|
707
|
+
while (sLo !== 0) {
|
|
708
|
+
const lsb = (sLo & -sLo) >>> 0;
|
|
709
|
+
const sniper = Math.clz32(lsb) ^ 31;
|
|
710
|
+
sLo = (sLo ^ lsb) >>> 0;
|
|
658
711
|
const b = attacks.between(ksq, sniper);
|
|
659
|
-
const
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
712
|
+
const bLo = (b.lo & pos.board.occupied.lo) >>> 0;
|
|
713
|
+
const bHi = (b.hi & pos.board.occupied.hi) >>> 0;
|
|
714
|
+
const nBlockers = sq.popcount({ lo: bLo, hi: bHi });
|
|
715
|
+
if (nBlockers === 1) {
|
|
716
|
+
const blockerSq = bLo !== 0 ? (Math.clz32((bLo & -bLo) >>> 0) ^ 31) : (32 + (Math.clz32((bHi & -bHi) >>> 0) ^ 31));
|
|
717
|
+
if (!sq.has(themOcc, blockerSq)) {
|
|
718
|
+
if (blockerSq < 32)
|
|
719
|
+
pinnedLo |= (1 << blockerSq) >>> 0;
|
|
720
|
+
else
|
|
721
|
+
pinnedHi |= (1 << (blockerSq - 32)) >>> 0;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
while (sHi !== 0) {
|
|
726
|
+
const lsb = (sHi & -sHi) >>> 0;
|
|
727
|
+
const sniper = 32 + (Math.clz32(lsb) ^ 31);
|
|
728
|
+
sHi = (sHi ^ lsb) >>> 0;
|
|
729
|
+
const b = attacks.between(ksq, sniper);
|
|
730
|
+
const bLo = (b.lo & pos.board.occupied.lo) >>> 0;
|
|
731
|
+
const bHi = (b.hi & pos.board.occupied.hi) >>> 0;
|
|
732
|
+
const nBlockers = sq.popcount({ lo: bLo, hi: bHi });
|
|
733
|
+
if (nBlockers === 1) {
|
|
734
|
+
const blockerSq = bLo !== 0 ? (Math.clz32((bLo & -bLo) >>> 0) ^ 31) : (32 + (Math.clz32((bHi & -bHi) >>> 0) ^ 31));
|
|
735
|
+
if (!sq.has(themOcc, blockerSq)) {
|
|
736
|
+
if (blockerSq < 32)
|
|
737
|
+
pinnedLo |= (1 << blockerSq) >>> 0;
|
|
738
|
+
else
|
|
739
|
+
pinnedHi |= (1 << (blockerSq - 32)) >>> 0;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
666
742
|
}
|
|
743
|
+
ctx.pinned = { lo: pinnedLo >>> 0, hi: pinnedHi >>> 0 };
|
|
667
744
|
// King-safe mask: attackedness evaluated with our king removed from the
|
|
668
745
|
// occupancy, so sliders x-raying the king keep it attacked on the far side.
|
|
669
746
|
const occWithoutKing = sq.minus(pos.board.occupied, sq.singleton(ksq));
|
|
@@ -687,7 +764,7 @@ export function analyzeCheckContext(pos) {
|
|
|
687
764
|
* pure set intersection for all cases except the trap cases listed above.
|
|
688
765
|
*/
|
|
689
766
|
export function destsFast(pos, from, piece, ctx) {
|
|
690
|
-
const pseudo = genPseudoDests(pos, from);
|
|
767
|
+
const pseudo = genPseudoDests(pos, from, piece);
|
|
691
768
|
if (sq.isEmpty(pseudo))
|
|
692
769
|
return pseudo;
|
|
693
770
|
if (piece.role === Role.King) {
|
|
@@ -732,9 +809,13 @@ export function destsFast(pos, from, piece, ctx) {
|
|
|
732
809
|
epLegal = moveLeavesKingSafe(pos, piece, from, epSquare, epSquare, true, null, false, null);
|
|
733
810
|
}
|
|
734
811
|
let result = sq.and(nonEp, ctx.checkMask);
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
result =
|
|
812
|
+
if (sq.has(ctx.pinned, from)) {
|
|
813
|
+
const pIdx = (ctx.ksq << 6) | from;
|
|
814
|
+
result = {
|
|
815
|
+
lo: (result.lo & attacks.LINE_RAY_LO[pIdx]) >>> 0,
|
|
816
|
+
hi: (result.hi & attacks.LINE_RAY_HI[pIdx]) >>> 0,
|
|
817
|
+
};
|
|
818
|
+
}
|
|
738
819
|
if (epLegal && epSquare !== null)
|
|
739
820
|
result = sq.or(result, sq.singleton(epSquare));
|
|
740
821
|
return result;
|
|
@@ -748,15 +829,30 @@ export function dests(pos, from) {
|
|
|
748
829
|
export function allDests(pos) {
|
|
749
830
|
const m = new Map();
|
|
750
831
|
const ctx = analyzeCheckContext(pos);
|
|
751
|
-
const
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
const
|
|
832
|
+
const isWhite = pos.turn === Color.White;
|
|
833
|
+
const own = isWhite ? pos.board.white : pos.board.black;
|
|
834
|
+
// 1. King
|
|
835
|
+
const ksq = ctx.ksq;
|
|
836
|
+
if (ksq >= 0) {
|
|
837
|
+
const kingPiece = isWhite ? P_W_K : P_B_K;
|
|
838
|
+
const d = destsFast(pos, ksq, kingPiece, ctx);
|
|
757
839
|
if (!sq.isEmpty(d))
|
|
758
|
-
m.set(
|
|
759
|
-
}
|
|
840
|
+
m.set(ksq, d);
|
|
841
|
+
}
|
|
842
|
+
if (ctx.doubleCheck)
|
|
843
|
+
return m;
|
|
844
|
+
const addRole = (roleBB, piece) => {
|
|
845
|
+
sq.forEachSquare(sq.and(own, roleBB), (sqIdx) => {
|
|
846
|
+
const d = destsFast(pos, sqIdx, piece, ctx);
|
|
847
|
+
if (!sq.isEmpty(d))
|
|
848
|
+
m.set(sqIdx, d);
|
|
849
|
+
});
|
|
850
|
+
};
|
|
851
|
+
addRole(pos.board.pawn, isWhite ? P_W_P : P_B_P);
|
|
852
|
+
addRole(pos.board.knight, isWhite ? P_W_N : P_B_N);
|
|
853
|
+
addRole(pos.board.bishop, isWhite ? P_W_B : P_B_B);
|
|
854
|
+
addRole(pos.board.rook, isWhite ? P_W_R : P_B_R);
|
|
855
|
+
addRole(pos.board.queen, isWhite ? P_W_Q : P_B_Q);
|
|
760
856
|
return m;
|
|
761
857
|
}
|
|
762
858
|
export function isLegal(pos, move) {
|
|
@@ -784,9 +880,35 @@ export function isLegal(pos, move) {
|
|
|
784
880
|
}
|
|
785
881
|
// helpers for checkmate/stalemate
|
|
786
882
|
function hasAnyLegalMove(pos) {
|
|
883
|
+
const ctx = analyzeCheckContext(pos);
|
|
884
|
+
const ksq = ctx.ksq;
|
|
885
|
+
if (ksq >= 0) {
|
|
886
|
+
const kd = destsFast(pos, ksq, { color: pos.turn, role: Role.King }, ctx);
|
|
887
|
+
if (!sq.isEmpty(kd))
|
|
888
|
+
return true;
|
|
889
|
+
}
|
|
890
|
+
if (ctx.doubleCheck)
|
|
891
|
+
return false;
|
|
787
892
|
const own = pos.turn === Color.White ? pos.board.white : pos.board.black;
|
|
788
|
-
|
|
789
|
-
|
|
893
|
+
let ownLo = own.lo >>> 0, ownHi = own.hi >>> 0;
|
|
894
|
+
while (ownLo !== 0 || ownHi !== 0) {
|
|
895
|
+
let from;
|
|
896
|
+
if (ownLo !== 0) {
|
|
897
|
+
const lsb = (ownLo & -ownLo) >>> 0;
|
|
898
|
+
from = Math.clz32(lsb) ^ 31;
|
|
899
|
+
ownLo = (ownLo ^ lsb) >>> 0;
|
|
900
|
+
}
|
|
901
|
+
else {
|
|
902
|
+
const lsb = (ownHi & -ownHi) >>> 0;
|
|
903
|
+
from = 32 + (Math.clz32(lsb) ^ 31);
|
|
904
|
+
ownHi = (ownHi ^ lsb) >>> 0;
|
|
905
|
+
}
|
|
906
|
+
if (from === ksq)
|
|
907
|
+
continue;
|
|
908
|
+
const piece = board.pieceAt(pos.board, from);
|
|
909
|
+
if (!piece)
|
|
910
|
+
continue;
|
|
911
|
+
const d = destsFast(pos, from, piece, ctx);
|
|
790
912
|
if (!sq.isEmpty(d))
|
|
791
913
|
return true;
|
|
792
914
|
}
|
|
@@ -813,141 +935,391 @@ export function perft(pos, depth) {
|
|
|
813
935
|
}
|
|
814
936
|
return nodes;
|
|
815
937
|
}
|
|
816
|
-
|
|
938
|
+
export class MoveCounter {
|
|
939
|
+
constructor() {
|
|
940
|
+
this.count = 0;
|
|
941
|
+
}
|
|
942
|
+
add(set) {
|
|
943
|
+
this.count += sq.popcount(set);
|
|
944
|
+
}
|
|
945
|
+
addPawns(legal, isWhite) {
|
|
946
|
+
const total = sq.popcount(legal);
|
|
947
|
+
const promos = isWhite
|
|
948
|
+
? sq.popcount({ lo: 0, hi: (legal.hi & 0xff000000) >>> 0 })
|
|
949
|
+
: sq.popcount({ lo: (legal.lo & 0x000000ff) >>> 0, hi: 0 });
|
|
950
|
+
this.count += total + promos * 3;
|
|
951
|
+
}
|
|
952
|
+
reset() {
|
|
953
|
+
this.count = 0;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
export function countLegalMoves(pos) {
|
|
957
|
+
const ctx = analyzeCheckContext(pos);
|
|
958
|
+
const us = pos.turn;
|
|
959
|
+
const isWhite = us === Color.White;
|
|
960
|
+
const own = isWhite ? pos.board.white : pos.board.black;
|
|
961
|
+
let count = 0;
|
|
962
|
+
// 1. King
|
|
963
|
+
const ksq = ctx.ksq;
|
|
964
|
+
if (ksq >= 0) {
|
|
965
|
+
const kingPiece = isWhite ? P_W_K : P_B_K;
|
|
966
|
+
count += sq.popcount(destsFast(pos, ksq, kingPiece, ctx));
|
|
967
|
+
}
|
|
968
|
+
if (ctx.doubleCheck)
|
|
969
|
+
return count;
|
|
970
|
+
// 2. Pawns (bulk popcount without inner destination loops)
|
|
971
|
+
const pawnPiece = isWhite ? P_W_P : P_B_P;
|
|
972
|
+
let pLo = (own.lo & pos.board.pawn.lo) >>> 0;
|
|
973
|
+
let pHi = (own.hi & pos.board.pawn.hi) >>> 0;
|
|
974
|
+
while (pLo !== 0) {
|
|
975
|
+
const lsb = (pLo & -pLo) >>> 0;
|
|
976
|
+
const from = Math.clz32(lsb) ^ 31;
|
|
977
|
+
pLo = (pLo ^ lsb) >>> 0;
|
|
978
|
+
const legal = destsFast(pos, from, pawnPiece, ctx);
|
|
979
|
+
const total = sq.popcount(legal);
|
|
980
|
+
const promos = isWhite
|
|
981
|
+
? sq.popcount({ lo: 0, hi: (legal.hi & 0xff000000) >>> 0 })
|
|
982
|
+
: sq.popcount({ lo: (legal.lo & 0x000000ff) >>> 0, hi: 0 });
|
|
983
|
+
count += total + promos * 3;
|
|
984
|
+
}
|
|
985
|
+
while (pHi !== 0) {
|
|
986
|
+
const lsb = (pHi & -pHi) >>> 0;
|
|
987
|
+
const from = 32 + (Math.clz32(lsb) ^ 31);
|
|
988
|
+
pHi = (pHi ^ lsb) >>> 0;
|
|
989
|
+
const legal = destsFast(pos, from, pawnPiece, ctx);
|
|
990
|
+
const total = sq.popcount(legal);
|
|
991
|
+
const promos = isWhite
|
|
992
|
+
? sq.popcount({ lo: 0, hi: (legal.hi & 0xff000000) >>> 0 })
|
|
993
|
+
: sq.popcount({ lo: (legal.lo & 0x000000ff) >>> 0, hi: 0 });
|
|
994
|
+
count += total + promos * 3;
|
|
995
|
+
}
|
|
996
|
+
// 3. Knights, Bishops, Rooks, Queens (bulk popcount on legal dests)
|
|
997
|
+
const countPieces = (roleBB, piece) => {
|
|
998
|
+
let rLo = (own.lo & roleBB.lo) >>> 0;
|
|
999
|
+
let rHi = (own.hi & roleBB.hi) >>> 0;
|
|
1000
|
+
while (rLo !== 0) {
|
|
1001
|
+
const lsb = (rLo & -rLo) >>> 0;
|
|
1002
|
+
const from = Math.clz32(lsb) ^ 31;
|
|
1003
|
+
rLo = (rLo ^ lsb) >>> 0;
|
|
1004
|
+
count += sq.popcount(destsFast(pos, from, piece, ctx));
|
|
1005
|
+
}
|
|
1006
|
+
while (rHi !== 0) {
|
|
1007
|
+
const lsb = (rHi & -rHi) >>> 0;
|
|
1008
|
+
const from = 32 + (Math.clz32(lsb) ^ 31);
|
|
1009
|
+
rHi = (rHi ^ lsb) >>> 0;
|
|
1010
|
+
count += sq.popcount(destsFast(pos, from, piece, ctx));
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
countPieces(pos.board.knight, isWhite ? P_W_N : P_B_N);
|
|
1014
|
+
countPieces(pos.board.bishop, isWhite ? P_W_B : P_B_B);
|
|
1015
|
+
countPieces(pos.board.rook, isWhite ? P_W_R : P_B_R);
|
|
1016
|
+
countPieces(pos.board.queen, isWhite ? P_W_Q : P_B_Q);
|
|
1017
|
+
return count;
|
|
1018
|
+
}
|
|
1019
|
+
export function legalMovesInto(pos, out) {
|
|
817
1020
|
let count = 0;
|
|
818
1021
|
const ctx = analyzeCheckContext(pos);
|
|
819
|
-
const
|
|
820
|
-
const isWhite =
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
1022
|
+
const us = pos.turn;
|
|
1023
|
+
const isWhite = us === Color.White;
|
|
1024
|
+
const own = isWhite ? pos.board.white : pos.board.black;
|
|
1025
|
+
// 1. King
|
|
1026
|
+
const ksq = ctx.ksq;
|
|
1027
|
+
if (ksq >= 0) {
|
|
1028
|
+
const kingPiece = isWhite ? P_W_K : P_B_K;
|
|
1029
|
+
const kd = destsFast(pos, ksq, kingPiece, ctx);
|
|
1030
|
+
let dLo = kd.lo >>> 0, dHi = kd.hi >>> 0;
|
|
1031
|
+
while (dLo !== 0) {
|
|
1032
|
+
const lsb = (dLo & -dLo) >>> 0;
|
|
1033
|
+
const to = Math.clz32(lsb) ^ 31;
|
|
1034
|
+
dLo = (dLo ^ lsb) >>> 0;
|
|
1035
|
+
out[count++] = (ksq | (to << 6)) >>> 0;
|
|
828
1036
|
}
|
|
829
|
-
|
|
830
|
-
const lsb = (
|
|
831
|
-
|
|
832
|
-
|
|
1037
|
+
while (dHi !== 0) {
|
|
1038
|
+
const lsb = (dHi & -dHi) >>> 0;
|
|
1039
|
+
const to = 32 + (Math.clz32(lsb) ^ 31);
|
|
1040
|
+
dHi = (dHi ^ lsb) >>> 0;
|
|
1041
|
+
out[count++] = (ksq | (to << 6)) >>> 0;
|
|
833
1042
|
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
if ((isWhite && destRank === 7) || (!isWhite && destRank === 0)) {
|
|
858
|
-
count += 4;
|
|
859
|
-
}
|
|
860
|
-
else {
|
|
861
|
-
count += 1;
|
|
862
|
-
}
|
|
1043
|
+
}
|
|
1044
|
+
if (ctx.doubleCheck)
|
|
1045
|
+
return count;
|
|
1046
|
+
// 2. Pawns
|
|
1047
|
+
const pawnPiece = isWhite ? P_W_P : P_B_P;
|
|
1048
|
+
let pLo = (own.lo & pos.board.pawn.lo) >>> 0;
|
|
1049
|
+
let pHi = (own.hi & pos.board.pawn.hi) >>> 0;
|
|
1050
|
+
const promoRank = isWhite ? 7 : 0;
|
|
1051
|
+
const emitPawnDests = (from) => {
|
|
1052
|
+
const legal = destsFast(pos, from, pawnPiece, ctx);
|
|
1053
|
+
let dLo = legal.lo >>> 0, dHi = legal.hi >>> 0;
|
|
1054
|
+
while (dLo !== 0) {
|
|
1055
|
+
const lsb = (dLo & -dLo) >>> 0;
|
|
1056
|
+
const to = Math.clz32(lsb) ^ 31;
|
|
1057
|
+
dLo = (dLo ^ lsb) >>> 0;
|
|
1058
|
+
if ((to >> 3) === promoRank) {
|
|
1059
|
+
out[count++] = (from | (to << 6) | (PROMO_QUEEN << 12)) >>> 0;
|
|
1060
|
+
out[count++] = (from | (to << 6) | (PROMO_ROOK << 12)) >>> 0;
|
|
1061
|
+
out[count++] = (from | (to << 6) | (PROMO_BISHOP << 12)) >>> 0;
|
|
1062
|
+
out[count++] = (from | (to << 6) | (PROMO_KNIGHT << 12)) >>> 0;
|
|
1063
|
+
}
|
|
1064
|
+
else {
|
|
1065
|
+
out[count++] = (from | (to << 6)) >>> 0;
|
|
863
1066
|
}
|
|
864
1067
|
}
|
|
865
|
-
|
|
866
|
-
|
|
1068
|
+
while (dHi !== 0) {
|
|
1069
|
+
const lsb = (dHi & -dHi) >>> 0;
|
|
1070
|
+
const to = 32 + (Math.clz32(lsb) ^ 31);
|
|
1071
|
+
dHi = (dHi ^ lsb) >>> 0;
|
|
1072
|
+
if ((to >> 3) === promoRank) {
|
|
1073
|
+
out[count++] = (from | (to << 6) | (PROMO_QUEEN << 12)) >>> 0;
|
|
1074
|
+
out[count++] = (from | (to << 6) | (PROMO_ROOK << 12)) >>> 0;
|
|
1075
|
+
out[count++] = (from | (to << 6) | (PROMO_BISHOP << 12)) >>> 0;
|
|
1076
|
+
out[count++] = (from | (to << 6) | (PROMO_KNIGHT << 12)) >>> 0;
|
|
1077
|
+
}
|
|
1078
|
+
else {
|
|
1079
|
+
out[count++] = (from | (to << 6)) >>> 0;
|
|
1080
|
+
}
|
|
867
1081
|
}
|
|
868
|
-
}
|
|
1082
|
+
};
|
|
1083
|
+
while (pLo !== 0) {
|
|
1084
|
+
const lsb = (pLo & -pLo) >>> 0;
|
|
1085
|
+
const from = Math.clz32(lsb) ^ 31;
|
|
1086
|
+
pLo = (pLo ^ lsb) >>> 0;
|
|
1087
|
+
emitPawnDests(from);
|
|
1088
|
+
}
|
|
1089
|
+
while (pHi !== 0) {
|
|
1090
|
+
const lsb = (pHi & -pHi) >>> 0;
|
|
1091
|
+
const from = 32 + (Math.clz32(lsb) ^ 31);
|
|
1092
|
+
pHi = (pHi ^ lsb) >>> 0;
|
|
1093
|
+
emitPawnDests(from);
|
|
1094
|
+
}
|
|
1095
|
+
// 3. Knights, Bishops, Rooks, Queens
|
|
1096
|
+
const emitRole = (roleBB, piece) => {
|
|
1097
|
+
let rLo = (own.lo & roleBB.lo) >>> 0;
|
|
1098
|
+
let rHi = (own.hi & roleBB.hi) >>> 0;
|
|
1099
|
+
while (rLo !== 0) {
|
|
1100
|
+
const lsb = (rLo & -rLo) >>> 0;
|
|
1101
|
+
const from = Math.clz32(lsb) ^ 31;
|
|
1102
|
+
rLo = (rLo ^ lsb) >>> 0;
|
|
1103
|
+
const d = destsFast(pos, from, piece, ctx);
|
|
1104
|
+
let dLo = d.lo >>> 0, dHi = d.hi >>> 0;
|
|
1105
|
+
while (dLo !== 0) {
|
|
1106
|
+
const lsbD = (dLo & -dLo) >>> 0;
|
|
1107
|
+
const to = Math.clz32(lsbD) ^ 31;
|
|
1108
|
+
dLo = (dLo ^ lsbD) >>> 0;
|
|
1109
|
+
out[count++] = (from | (to << 6)) >>> 0;
|
|
1110
|
+
}
|
|
1111
|
+
while (dHi !== 0) {
|
|
1112
|
+
const lsbD = (dHi & -dHi) >>> 0;
|
|
1113
|
+
const to = 32 + (Math.clz32(lsbD) ^ 31);
|
|
1114
|
+
dHi = (dHi ^ lsbD) >>> 0;
|
|
1115
|
+
out[count++] = (from | (to << 6)) >>> 0;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
while (rHi !== 0) {
|
|
1119
|
+
const lsb = (rHi & -rHi) >>> 0;
|
|
1120
|
+
const from = 32 + (Math.clz32(lsb) ^ 31);
|
|
1121
|
+
rHi = (rHi ^ lsb) >>> 0;
|
|
1122
|
+
const d = destsFast(pos, from, piece, ctx);
|
|
1123
|
+
let dLo = d.lo >>> 0, dHi = d.hi >>> 0;
|
|
1124
|
+
while (dLo !== 0) {
|
|
1125
|
+
const lsbD = (dLo & -dLo) >>> 0;
|
|
1126
|
+
const to = Math.clz32(lsbD) ^ 31;
|
|
1127
|
+
dLo = (dLo ^ lsbD) >>> 0;
|
|
1128
|
+
out[count++] = (from | (to << 6)) >>> 0;
|
|
1129
|
+
}
|
|
1130
|
+
while (dHi !== 0) {
|
|
1131
|
+
const lsbD = (dHi & -dHi) >>> 0;
|
|
1132
|
+
const to = 32 + (Math.clz32(lsbD) ^ 31);
|
|
1133
|
+
dHi = (dHi ^ lsbD) >>> 0;
|
|
1134
|
+
out[count++] = (from | (to << 6)) >>> 0;
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
};
|
|
1138
|
+
emitRole(pos.board.knight, isWhite ? P_W_N : P_B_N);
|
|
1139
|
+
emitRole(pos.board.bishop, isWhite ? P_W_B : P_B_B);
|
|
1140
|
+
emitRole(pos.board.rook, isWhite ? P_W_R : P_B_R);
|
|
1141
|
+
emitRole(pos.board.queen, isWhite ? P_W_Q : P_B_Q);
|
|
869
1142
|
return count;
|
|
870
1143
|
}
|
|
1144
|
+
export function forEachLegalMove(pos, fn) {
|
|
1145
|
+
const ctx = analyzeCheckContext(pos);
|
|
1146
|
+
const us = pos.turn;
|
|
1147
|
+
const isWhite = us === Color.White;
|
|
1148
|
+
const own = isWhite ? pos.board.white : pos.board.black;
|
|
1149
|
+
// 1. King
|
|
1150
|
+
const ksq = ctx.ksq;
|
|
1151
|
+
if (ksq >= 0) {
|
|
1152
|
+
const kingPiece = isWhite ? P_W_K : P_B_K;
|
|
1153
|
+
const kd = destsFast(pos, ksq, kingPiece, ctx);
|
|
1154
|
+
let dLo = kd.lo >>> 0, dHi = kd.hi >>> 0;
|
|
1155
|
+
while (dLo !== 0) {
|
|
1156
|
+
const lsb = (dLo & -dLo) >>> 0;
|
|
1157
|
+
const to = Math.clz32(lsb) ^ 31;
|
|
1158
|
+
dLo = (dLo ^ lsb) >>> 0;
|
|
1159
|
+
fn(ksq, to, PROMO_NONE);
|
|
1160
|
+
}
|
|
1161
|
+
while (dHi !== 0) {
|
|
1162
|
+
const lsb = (dHi & -dHi) >>> 0;
|
|
1163
|
+
const to = 32 + (Math.clz32(lsb) ^ 31);
|
|
1164
|
+
dHi = (dHi ^ lsb) >>> 0;
|
|
1165
|
+
fn(ksq, to, PROMO_NONE);
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
if (ctx.doubleCheck)
|
|
1169
|
+
return;
|
|
1170
|
+
// 2. Pawns
|
|
1171
|
+
const pawnPiece = isWhite ? P_W_P : P_B_P;
|
|
1172
|
+
let pLo = (own.lo & pos.board.pawn.lo) >>> 0;
|
|
1173
|
+
let pHi = (own.hi & pos.board.pawn.hi) >>> 0;
|
|
1174
|
+
const promoRank = isWhite ? 7 : 0;
|
|
1175
|
+
const visitPawnDests = (from) => {
|
|
1176
|
+
const legal = destsFast(pos, from, pawnPiece, ctx);
|
|
1177
|
+
let dLo = legal.lo >>> 0, dHi = legal.hi >>> 0;
|
|
1178
|
+
while (dLo !== 0) {
|
|
1179
|
+
const lsb = (dLo & -dLo) >>> 0;
|
|
1180
|
+
const to = Math.clz32(lsb) ^ 31;
|
|
1181
|
+
dLo = (dLo ^ lsb) >>> 0;
|
|
1182
|
+
if ((to >> 3) === promoRank) {
|
|
1183
|
+
fn(from, to, PROMO_QUEEN);
|
|
1184
|
+
fn(from, to, PROMO_ROOK);
|
|
1185
|
+
fn(from, to, PROMO_BISHOP);
|
|
1186
|
+
fn(from, to, PROMO_KNIGHT);
|
|
1187
|
+
}
|
|
1188
|
+
else {
|
|
1189
|
+
fn(from, to, PROMO_NONE);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
while (dHi !== 0) {
|
|
1193
|
+
const lsb = (dHi & -dHi) >>> 0;
|
|
1194
|
+
const to = 32 + (Math.clz32(lsb) ^ 31);
|
|
1195
|
+
dHi = (dHi ^ lsb) >>> 0;
|
|
1196
|
+
if ((to >> 3) === promoRank) {
|
|
1197
|
+
fn(from, to, PROMO_QUEEN);
|
|
1198
|
+
fn(from, to, PROMO_ROOK);
|
|
1199
|
+
fn(from, to, PROMO_BISHOP);
|
|
1200
|
+
fn(from, to, PROMO_KNIGHT);
|
|
1201
|
+
}
|
|
1202
|
+
else {
|
|
1203
|
+
fn(from, to, PROMO_NONE);
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
};
|
|
1207
|
+
while (pLo !== 0) {
|
|
1208
|
+
const lsb = (pLo & -pLo) >>> 0;
|
|
1209
|
+
const from = Math.clz32(lsb) ^ 31;
|
|
1210
|
+
pLo = (pLo ^ lsb) >>> 0;
|
|
1211
|
+
visitPawnDests(from);
|
|
1212
|
+
}
|
|
1213
|
+
while (pHi !== 0) {
|
|
1214
|
+
const lsb = (pHi & -pHi) >>> 0;
|
|
1215
|
+
const from = 32 + (Math.clz32(lsb) ^ 31);
|
|
1216
|
+
pHi = (pHi ^ lsb) >>> 0;
|
|
1217
|
+
visitPawnDests(from);
|
|
1218
|
+
}
|
|
1219
|
+
// 3. Knights, Bishops, Rooks, Queens
|
|
1220
|
+
const visitRole = (roleBB, piece) => {
|
|
1221
|
+
let rLo = (own.lo & roleBB.lo) >>> 0;
|
|
1222
|
+
let rHi = (own.hi & roleBB.hi) >>> 0;
|
|
1223
|
+
while (rLo !== 0) {
|
|
1224
|
+
const lsb = (rLo & -rLo) >>> 0;
|
|
1225
|
+
const from = Math.clz32(lsb) ^ 31;
|
|
1226
|
+
rLo = (rLo ^ lsb) >>> 0;
|
|
1227
|
+
const d = destsFast(pos, from, piece, ctx);
|
|
1228
|
+
let dLo = d.lo >>> 0, dHi = d.hi >>> 0;
|
|
1229
|
+
while (dLo !== 0) {
|
|
1230
|
+
const lsbD = (dLo & -dLo) >>> 0;
|
|
1231
|
+
const to = Math.clz32(lsbD) ^ 31;
|
|
1232
|
+
dLo = (dLo ^ lsbD) >>> 0;
|
|
1233
|
+
fn(from, to, PROMO_NONE);
|
|
1234
|
+
}
|
|
1235
|
+
while (dHi !== 0) {
|
|
1236
|
+
const lsbD = (dHi & -dHi) >>> 0;
|
|
1237
|
+
const to = 32 + (Math.clz32(lsbD) ^ 31);
|
|
1238
|
+
dHi = (dHi ^ lsbD) >>> 0;
|
|
1239
|
+
fn(from, to, PROMO_NONE);
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
while (rHi !== 0) {
|
|
1243
|
+
const lsb = (rHi & -rHi) >>> 0;
|
|
1244
|
+
const from = 32 + (Math.clz32(lsb) ^ 31);
|
|
1245
|
+
rHi = (rHi ^ lsb) >>> 0;
|
|
1246
|
+
const d = destsFast(pos, from, piece, ctx);
|
|
1247
|
+
let dLo = d.lo >>> 0, dHi = d.hi >>> 0;
|
|
1248
|
+
while (dLo !== 0) {
|
|
1249
|
+
const lsbD = (dLo & -dLo) >>> 0;
|
|
1250
|
+
const to = Math.clz32(lsbD) ^ 31;
|
|
1251
|
+
dLo = (dLo ^ lsbD) >>> 0;
|
|
1252
|
+
fn(from, to, PROMO_NONE);
|
|
1253
|
+
}
|
|
1254
|
+
while (dHi !== 0) {
|
|
1255
|
+
const lsbD = (dHi & -dHi) >>> 0;
|
|
1256
|
+
const to = 32 + (Math.clz32(lsbD) ^ 31);
|
|
1257
|
+
dHi = (dHi ^ lsbD) >>> 0;
|
|
1258
|
+
fn(from, to, PROMO_NONE);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
};
|
|
1262
|
+
visitRole(pos.board.knight, isWhite ? P_W_N : P_B_N);
|
|
1263
|
+
visitRole(pos.board.bishop, isWhite ? P_W_B : P_B_B);
|
|
1264
|
+
visitRole(pos.board.rook, isWhite ? P_W_R : P_B_R);
|
|
1265
|
+
visitRole(pos.board.queen, isWhite ? P_W_Q : P_B_Q);
|
|
1266
|
+
}
|
|
871
1267
|
// Promotion pieces in generation order (hoisted module constant — the old
|
|
872
1268
|
// per-call array literal allocated on every promotion move in the hot loop).
|
|
873
1269
|
const PROMO_ROLES = [Role.Queen, Role.Rook, Role.Bishop, Role.Knight];
|
|
874
1270
|
function genLegalMoves(pos) {
|
|
875
1271
|
const moves = [];
|
|
876
|
-
// Per-position check/pin-mask analysis replaces the per-move play-and-test;
|
|
877
|
-
// only ep (validated inside destsFast) keeps exact semantics.
|
|
878
1272
|
const ctx = analyzeCheckContext(pos);
|
|
879
|
-
const
|
|
880
|
-
|
|
881
|
-
|
|
1273
|
+
const us = pos.turn;
|
|
1274
|
+
const isWhite = us === Color.White;
|
|
1275
|
+
const own = isWhite ? pos.board.white : pos.board.black;
|
|
882
1276
|
const plans = ctx.castlingPlans;
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
const
|
|
889
|
-
from
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
const
|
|
898
|
-
|
|
899
|
-
continue;
|
|
900
|
-
// A non-king move can never resolve a double check.
|
|
901
|
-
if (piece.role !== Role.King && ctx.doubleCheck)
|
|
902
|
-
continue;
|
|
903
|
-
const legal = destsFast(pos, from, piece, ctx);
|
|
904
|
-
const isPawn = piece.role === Role.Pawn;
|
|
905
|
-
const isKing = piece.role === Role.King;
|
|
906
|
-
let lo = legal.lo >>> 0, hi = legal.hi >>> 0;
|
|
907
|
-
while (lo !== 0 || hi !== 0) {
|
|
908
|
-
let to;
|
|
909
|
-
if (lo !== 0) {
|
|
910
|
-
const lsb = (lo & -lo) >>> 0;
|
|
911
|
-
to = 31 - Math.clz32(lsb);
|
|
912
|
-
lo ^= lsb;
|
|
913
|
-
}
|
|
914
|
-
else {
|
|
915
|
-
const lsb = (hi & -hi) >>> 0;
|
|
916
|
-
to = 32 + (31 - Math.clz32(lsb));
|
|
917
|
-
hi ^= lsb;
|
|
918
|
-
}
|
|
1277
|
+
// 1. King
|
|
1278
|
+
const ksq = ctx.ksq;
|
|
1279
|
+
if (ksq >= 0) {
|
|
1280
|
+
const kingPiece = isWhite ? P_W_K : P_B_K;
|
|
1281
|
+
sq.forEachSquare(destsFast(pos, ksq, kingPiece, ctx), (to) => {
|
|
1282
|
+
const isCastling = plans.length > 0 && planForDest(plans, to) !== null;
|
|
1283
|
+
moves.push({ from: ksq, to, promotion: null, isEnPassant: false, isCastling, isPromotion: false });
|
|
1284
|
+
});
|
|
1285
|
+
}
|
|
1286
|
+
if (ctx.doubleCheck)
|
|
1287
|
+
return moves;
|
|
1288
|
+
// 2. Pawns
|
|
1289
|
+
const pawnPiece = isWhite ? P_W_P : P_B_P;
|
|
1290
|
+
sq.forEachSquare(sq.and(own, pos.board.pawn), (from) => {
|
|
1291
|
+
const legal = destsFast(pos, from, pawnPiece, ctx);
|
|
1292
|
+
sq.forEachSquare(legal, (to) => {
|
|
919
1293
|
const destRank = to >> 3;
|
|
920
|
-
if (
|
|
921
|
-
// generate 4 promotions (legality is promotion-independent: every
|
|
922
|
-
// promoted piece lands on the same square, so king safety is identical)
|
|
1294
|
+
if ((isWhite && destRank === 7) || (!isWhite && destRank === 0)) {
|
|
923
1295
|
for (const promo of PROMO_ROLES) {
|
|
924
|
-
// promotion capture can't be en passant (ep rank not back rank)
|
|
925
1296
|
moves.push({ from, to, promotion: promo, isPromotion: true, isEnPassant: false, isCastling: false });
|
|
926
1297
|
}
|
|
927
1298
|
}
|
|
928
1299
|
else {
|
|
929
|
-
// determine flags
|
|
930
1300
|
let isEnPassant = false;
|
|
931
|
-
|
|
932
|
-
if (isPawn && pos.epSquare !== null && to === pos.epSquare) {
|
|
933
|
-
// check if pawn capture to ep square
|
|
1301
|
+
if (pos.epSquare !== null && to === pos.epSquare) {
|
|
934
1302
|
const fileDiff = Math.abs((to & 7) - (from & 7));
|
|
935
1303
|
const dir = isWhite ? 1 : -1;
|
|
936
1304
|
if (fileDiff === 1 && destRank - (from >> 3) === dir)
|
|
937
1305
|
isEnPassant = true;
|
|
938
1306
|
}
|
|
939
|
-
|
|
940
|
-
// Precomputed per-position plans (design D2) — replaces the old
|
|
941
|
-
// dest-matches-a-right heuristic, which flagged ordinary king steps
|
|
942
|
-
// to {6,2,62,58} as castling whenever any color held rights.
|
|
943
|
-
isCastling = planForDest(plans, to) !== null;
|
|
944
|
-
}
|
|
945
|
-
// Legality is already guaranteed by destsFast (exact play-and-test for
|
|
946
|
-
// the ep/castling trap cases, check/pin masks otherwise) — no re-test.
|
|
947
|
-
moves.push({ from, to, promotion: null, isEnPassant, isCastling, isPromotion: false });
|
|
1307
|
+
moves.push({ from, to, promotion: null, isEnPassant, isCastling: false, isPromotion: false });
|
|
948
1308
|
}
|
|
949
|
-
}
|
|
950
|
-
}
|
|
1309
|
+
});
|
|
1310
|
+
});
|
|
1311
|
+
// 3. Knights, Bishops, Rooks, Queens
|
|
1312
|
+
const addPieceMoves = (roleBB, piece) => {
|
|
1313
|
+
sq.forEachSquare(sq.and(own, roleBB), (from) => {
|
|
1314
|
+
sq.forEachSquare(destsFast(pos, from, piece, ctx), (to) => {
|
|
1315
|
+
moves.push({ from, to, promotion: null, isEnPassant: false, isCastling: false, isPromotion: false });
|
|
1316
|
+
});
|
|
1317
|
+
});
|
|
1318
|
+
};
|
|
1319
|
+
addPieceMoves(pos.board.knight, isWhite ? P_W_N : P_B_N);
|
|
1320
|
+
addPieceMoves(pos.board.bishop, isWhite ? P_W_B : P_B_B);
|
|
1321
|
+
addPieceMoves(pos.board.rook, isWhite ? P_W_R : P_B_R);
|
|
1322
|
+
addPieceMoves(pos.board.queen, isWhite ? P_W_Q : P_B_Q);
|
|
951
1323
|
return moves;
|
|
952
1324
|
}
|
|
953
1325
|
// Promotion pieces in generation order (hoisted module constant — the old
|
|
@@ -974,21 +1346,7 @@ const PROMO_ORDER = [4, 3, 2, 1]; // Role.Queen, Role.Rook, Role.Bishop, Role.Kn
|
|
|
974
1346
|
function colorName(turn) {
|
|
975
1347
|
return turn === Color.White ? "w" : "b";
|
|
976
1348
|
}
|
|
977
|
-
|
|
978
|
-
function* iterSet(set) {
|
|
979
|
-
let lo = set.lo >>> 0;
|
|
980
|
-
let hi = set.hi >>> 0;
|
|
981
|
-
while (lo !== 0) {
|
|
982
|
-
const lsb = (lo & -lo) >>> 0;
|
|
983
|
-
yield 31 - Math.clz32(lsb);
|
|
984
|
-
lo ^= lsb;
|
|
985
|
-
}
|
|
986
|
-
while (hi !== 0) {
|
|
987
|
-
const lsb = (hi & -hi) >>> 0;
|
|
988
|
-
yield 32 + (31 - Math.clz32(lsb));
|
|
989
|
-
hi ^= lsb;
|
|
990
|
-
}
|
|
991
|
-
}
|
|
1349
|
+
export { Board };
|
|
992
1350
|
/** Expands pawn back-rank destinations into the four promotions. */
|
|
993
1351
|
function buildMoves(pos, role, from, to) {
|
|
994
1352
|
const toRank = to >> 3;
|
|
@@ -1025,10 +1383,8 @@ function hasLegalEpCapture(pos) {
|
|
|
1025
1383
|
const p = pieceAt(pos.board, from);
|
|
1026
1384
|
if (!p || p.role !== 0)
|
|
1027
1385
|
continue;
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
return true; // allDests is fully legal, no re-test needed
|
|
1031
|
-
}
|
|
1386
|
+
if (sq.has(set, ep))
|
|
1387
|
+
return true;
|
|
1032
1388
|
}
|
|
1033
1389
|
return false;
|
|
1034
1390
|
}
|
|
@@ -1037,31 +1393,23 @@ function hasLegalEpCapture(pos) {
|
|
|
1037
1393
|
* over the immutable engine core, extended with the high-performance
|
|
1038
1394
|
* bitboard surface, zero-alloc Zobrist hashing and 16-bit packed moves2, and
|
|
1039
1395
|
* native tree navigation. Lives at the ROOT entrypoint so the tree-shakeable
|
|
1040
|
-
* `
|
|
1396
|
+
* `gigachess/core` graph stays free of FEN/PGN/tree facade code.
|
|
1041
1397
|
*/
|
|
1042
1398
|
export class Chess {
|
|
1043
1399
|
constructor(fen = INITIAL_FEN) {
|
|
1044
1400
|
_Chess_instances.add(this);
|
|
1401
|
+
_Chess_board.set(this, void 0);
|
|
1045
1402
|
_Chess_startFen.set(this, void 0);
|
|
1046
1403
|
_Chess_history.set(this, []);
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
const r = parseFen(fen);
|
|
1050
|
-
if (!r.ok)
|
|
1051
|
-
throw new Error(`Invalid FEN: ${fen}`);
|
|
1052
|
-
__classPrivateFieldSet(this, _Chess_pos, r.value, "f");
|
|
1053
|
-
}
|
|
1054
|
-
else {
|
|
1055
|
-
__classPrivateFieldSet(this, _Chess_pos, fen, "f");
|
|
1056
|
-
}
|
|
1057
|
-
__classPrivateFieldSet(this, _Chess_startFen, __classPrivateFieldGet(this, _Chess_instances, "m", _Chess_fenOf).call(this, __classPrivateFieldGet(this, _Chess_pos, "f")), "f");
|
|
1404
|
+
__classPrivateFieldSet(this, _Chess_board, new Board(fen), "f");
|
|
1405
|
+
__classPrivateFieldSet(this, _Chess_startFen, __classPrivateFieldGet(this, _Chess_instances, "m", _Chess_fenOf).call(this, __classPrivateFieldGet(this, _Chess_board, "f").pos), "f");
|
|
1058
1406
|
}
|
|
1059
1407
|
/** Replaces the current position (throws on an invalid FEN). */
|
|
1060
1408
|
load(fen) {
|
|
1061
1409
|
const r = parseFen(fen);
|
|
1062
1410
|
if (!r.ok)
|
|
1063
1411
|
throw new Error(`Invalid FEN: ${fen}`);
|
|
1064
|
-
__classPrivateFieldSet(this,
|
|
1412
|
+
__classPrivateFieldSet(this, _Chess_board, new Board(r.value), "f");
|
|
1065
1413
|
__classPrivateFieldSet(this, _Chess_startFen, __classPrivateFieldGet(this, _Chess_instances, "m", _Chess_fenOf).call(this, r.value), "f");
|
|
1066
1414
|
__classPrivateFieldSet(this, _Chess_history, [], "f");
|
|
1067
1415
|
}
|
|
@@ -1071,15 +1419,15 @@ export class Chess {
|
|
|
1071
1419
|
}
|
|
1072
1420
|
/** Current FEN (byte-identical to the engine's makeFen modulo ep filtering). */
|
|
1073
1421
|
fen() {
|
|
1074
|
-
return __classPrivateFieldGet(this, _Chess_instances, "m", _Chess_fenOf).call(this, __classPrivateFieldGet(this,
|
|
1422
|
+
return __classPrivateFieldGet(this, _Chess_instances, "m", _Chess_fenOf).call(this, __classPrivateFieldGet(this, _Chess_board, "f").pos);
|
|
1075
1423
|
}
|
|
1076
1424
|
/** Side to move: "w" | "b". */
|
|
1077
1425
|
turn() {
|
|
1078
|
-
return colorName(__classPrivateFieldGet(this,
|
|
1426
|
+
return colorName(__classPrivateFieldGet(this, _Chess_board, "f").turn);
|
|
1079
1427
|
}
|
|
1080
1428
|
/** Current fullmove number (starts at 1). */
|
|
1081
1429
|
moveNumber() {
|
|
1082
|
-
return __classPrivateFieldGet(this,
|
|
1430
|
+
return __classPrivateFieldGet(this, _Chess_board, "f").fullmoves;
|
|
1083
1431
|
}
|
|
1084
1432
|
/**
|
|
1085
1433
|
* Plays a move given as SAN (tolerant: `+`/`#` suffixes, `x` on quiet moves,
|
|
@@ -1087,24 +1435,28 @@ export class Chess {
|
|
|
1087
1435
|
* object. Returns the verbose move, or null when the move is illegal.
|
|
1088
1436
|
*/
|
|
1089
1437
|
move(input) {
|
|
1090
|
-
const pos = __classPrivateFieldGet(this,
|
|
1438
|
+
const pos = __classPrivateFieldGet(this, _Chess_board, "f").pos;
|
|
1091
1439
|
let mv = null;
|
|
1440
|
+
let moveWord = null;
|
|
1092
1441
|
if (typeof input === "string") {
|
|
1093
1442
|
const sanRes = parseSan(input, pos);
|
|
1094
1443
|
if (sanRes.ok) {
|
|
1095
1444
|
mv = sanRes.value;
|
|
1445
|
+
moveWord = packOf(mv);
|
|
1096
1446
|
}
|
|
1097
1447
|
else {
|
|
1098
1448
|
const uciRes = parseUci(input);
|
|
1099
|
-
if (uciRes.ok && isLegal(pos, uciRes.value))
|
|
1449
|
+
if (uciRes.ok && isLegal(pos, uciRes.value)) {
|
|
1100
1450
|
mv = uciRes.value;
|
|
1451
|
+
moveWord = packOf(mv);
|
|
1452
|
+
}
|
|
1101
1453
|
}
|
|
1102
1454
|
}
|
|
1103
1455
|
else {
|
|
1104
1456
|
const from = parseSquare(input.from);
|
|
1105
1457
|
const to = parseSquare(input.to);
|
|
1106
1458
|
if (from !== undefined && to !== undefined) {
|
|
1107
|
-
const piece = pieceAt(
|
|
1459
|
+
const piece = pieceAt(__classPrivateFieldGet(this, _Chess_board, "f").board, from);
|
|
1108
1460
|
const promotion = input.promotion
|
|
1109
1461
|
? { q: 4, r: 3, b: 2, n: 1 }[input.promotion.toLowerCase()]
|
|
1110
1462
|
: undefined;
|
|
@@ -1113,27 +1465,37 @@ export class Chess {
|
|
|
1113
1465
|
continue;
|
|
1114
1466
|
if (isLegal(pos, cand)) {
|
|
1115
1467
|
mv = cand;
|
|
1468
|
+
moveWord = packOf(mv);
|
|
1116
1469
|
break;
|
|
1117
1470
|
}
|
|
1118
1471
|
}
|
|
1119
1472
|
}
|
|
1120
1473
|
}
|
|
1121
|
-
if (!mv)
|
|
1474
|
+
if (!mv || moveWord === null)
|
|
1122
1475
|
return null;
|
|
1123
|
-
const
|
|
1124
|
-
const
|
|
1125
|
-
|
|
1126
|
-
__classPrivateFieldGet(this,
|
|
1127
|
-
|
|
1476
|
+
const before = __classPrivateFieldGet(this, _Chess_board, "f").toPosition();
|
|
1477
|
+
const san = makeSan(mv, before);
|
|
1478
|
+
const boardUndo = __classPrivateFieldGet(this, _Chess_board, "f").makeMove(moveWord);
|
|
1479
|
+
const after = __classPrivateFieldGet(this, _Chess_board, "f").toPosition();
|
|
1480
|
+
__classPrivateFieldGet(this, _Chess_history, "f").push({
|
|
1481
|
+
before,
|
|
1482
|
+
after,
|
|
1483
|
+
move: mv,
|
|
1484
|
+
san,
|
|
1485
|
+
boardUndo,
|
|
1486
|
+
prev_checkers: before.checkers,
|
|
1487
|
+
prev_zobrist: before.zobristLo !== undefined && before.zobristHi !== undefined ? { lo: before.zobristLo, hi: before.zobristHi } : undefined,
|
|
1488
|
+
});
|
|
1489
|
+
return __classPrivateFieldGet(this, _Chess_instances, "m", _Chess_describe).call(this, before, mv, after, san);
|
|
1128
1490
|
}
|
|
1129
1491
|
moves(options) {
|
|
1130
|
-
const pos = __classPrivateFieldGet(this,
|
|
1492
|
+
const pos = __classPrivateFieldGet(this, _Chess_board, "f").pos;
|
|
1131
1493
|
const out = [];
|
|
1132
1494
|
const emit = (from, set) => {
|
|
1133
|
-
const piece = pieceAt(
|
|
1495
|
+
const piece = pieceAt(__classPrivateFieldGet(this, _Chess_board, "f").board, from);
|
|
1134
1496
|
if (!piece)
|
|
1135
1497
|
return;
|
|
1136
|
-
|
|
1498
|
+
sq.forEachSquare(set, (to) => {
|
|
1137
1499
|
for (const mv of buildMoves(pos, piece.role, from, to)) {
|
|
1138
1500
|
const san = makeSan(mv, pos);
|
|
1139
1501
|
if (options?.verbose)
|
|
@@ -1141,7 +1503,7 @@ export class Chess {
|
|
|
1141
1503
|
else
|
|
1142
1504
|
out.push(san);
|
|
1143
1505
|
}
|
|
1144
|
-
}
|
|
1506
|
+
});
|
|
1145
1507
|
};
|
|
1146
1508
|
if (options?.square !== undefined) {
|
|
1147
1509
|
const sqIdx = parseSquare(options.square);
|
|
@@ -1166,26 +1528,32 @@ export class Chess {
|
|
|
1166
1528
|
const last = __classPrivateFieldGet(this, _Chess_history, "f").pop();
|
|
1167
1529
|
if (!last)
|
|
1168
1530
|
return null;
|
|
1169
|
-
|
|
1531
|
+
if (last.boardUndo) {
|
|
1532
|
+
__classPrivateFieldGet(this, _Chess_board, "f").unmakeMove(last.boardUndo);
|
|
1533
|
+
}
|
|
1534
|
+
else {
|
|
1535
|
+
__classPrivateFieldSet(this, _Chess_board, new Board(last.before), "f");
|
|
1536
|
+
}
|
|
1170
1537
|
return __classPrivateFieldGet(this, _Chess_instances, "m", _Chess_describe).call(this, last.before, last.move, last.after, last.san);
|
|
1171
1538
|
}
|
|
1172
|
-
isCheck() { return
|
|
1173
|
-
|
|
1174
|
-
|
|
1539
|
+
isCheck() { return __classPrivateFieldGet(this, _Chess_board, "f").inCheck(); }
|
|
1540
|
+
inCheck() { return __classPrivateFieldGet(this, _Chess_board, "f").inCheck(); }
|
|
1541
|
+
isCheckmate() { return isCheckmate(__classPrivateFieldGet(this, _Chess_board, "f").pos); }
|
|
1542
|
+
isStalemate() { return isStalemate(__classPrivateFieldGet(this, _Chess_board, "f").pos); }
|
|
1175
1543
|
/** Draw by insufficient material, the fifty-move rule, or threefold repetition. */
|
|
1176
1544
|
isDraw() {
|
|
1177
|
-
if (isInsufficientMaterial(__classPrivateFieldGet(this,
|
|
1545
|
+
if (isInsufficientMaterial(__classPrivateFieldGet(this, _Chess_board, "f").pos))
|
|
1178
1546
|
return true;
|
|
1179
|
-
if (isFiftyMoveDraw(__classPrivateFieldGet(this,
|
|
1547
|
+
if (isFiftyMoveDraw(__classPrivateFieldGet(this, _Chess_board, "f").pos))
|
|
1180
1548
|
return true;
|
|
1181
1549
|
if (isThreefoldRepetition(__classPrivateFieldGet(this, _Chess_instances, "m", _Chess_positions).call(this)))
|
|
1182
1550
|
return true;
|
|
1183
1551
|
return false;
|
|
1184
1552
|
}
|
|
1185
1553
|
/** Draw by the fifty-move rule. */
|
|
1186
|
-
isDrawByFiftyMoves() { return isFiftyMoveDraw(__classPrivateFieldGet(this,
|
|
1554
|
+
isDrawByFiftyMoves() { return isFiftyMoveDraw(__classPrivateFieldGet(this, _Chess_board, "f").pos); }
|
|
1187
1555
|
/** Draw by insufficient material. */
|
|
1188
|
-
isInsufficientMaterial() { return isInsufficientMaterial(__classPrivateFieldGet(this,
|
|
1556
|
+
isInsufficientMaterial() { return isInsufficientMaterial(__classPrivateFieldGet(this, _Chess_board, "f").pos); }
|
|
1189
1557
|
/** Draw by threefold repetition over the game's position history. */
|
|
1190
1558
|
isThreefoldRepetition() { return isThreefoldRepetition(__classPrivateFieldGet(this, _Chess_instances, "m", _Chess_positions).call(this)); }
|
|
1191
1559
|
/** Game over: checkmate, stalemate, or draw. */
|
|
@@ -1195,7 +1563,7 @@ export class Chess {
|
|
|
1195
1563
|
const sqIdx = parseSquare(square);
|
|
1196
1564
|
if (sqIdx === undefined)
|
|
1197
1565
|
return undefined;
|
|
1198
|
-
const p = pieceAt(__classPrivateFieldGet(this,
|
|
1566
|
+
const p = pieceAt(__classPrivateFieldGet(this, _Chess_board, "f").board, sqIdx);
|
|
1199
1567
|
if (!p)
|
|
1200
1568
|
return undefined;
|
|
1201
1569
|
return { type: ROLE_CHARS[p.role], color: colorName(p.color) };
|
|
@@ -1207,7 +1575,7 @@ export class Chess {
|
|
|
1207
1575
|
const row = [];
|
|
1208
1576
|
for (let file = 0; file < 8; file++) {
|
|
1209
1577
|
const sqIdx = rank * 8 + file;
|
|
1210
|
-
const p = pieceAt(__classPrivateFieldGet(this,
|
|
1578
|
+
const p = pieceAt(__classPrivateFieldGet(this, _Chess_board, "f").board, sqIdx);
|
|
1211
1579
|
row.push(p ? { square: squareName(sqIdx), type: ROLE_CHARS[p.role], color: colorName(p.color) } : null);
|
|
1212
1580
|
}
|
|
1213
1581
|
rows.push(row);
|
|
@@ -1249,31 +1617,38 @@ export class Chess {
|
|
|
1249
1617
|
const sqIdx = typeof square === "number" ? square : parseSquare(square);
|
|
1250
1618
|
if (sqIdx === undefined)
|
|
1251
1619
|
return { lo: 0, hi: 0 };
|
|
1252
|
-
return dests(__classPrivateFieldGet(this,
|
|
1620
|
+
return dests(__classPrivateFieldGet(this, _Chess_board, "f").pos, sqIdx);
|
|
1253
1621
|
}
|
|
1254
1622
|
/** Legal destinations for every piece of the side to move. */
|
|
1255
|
-
allDests() { return allDests(__classPrivateFieldGet(this,
|
|
1623
|
+
allDests() { return allDests(__classPrivateFieldGet(this, _Chess_board, "f").pos); }
|
|
1256
1624
|
/** True when `move` is legal in the current position. */
|
|
1257
|
-
isLegal(move) { return isLegal(__classPrivateFieldGet(this,
|
|
1625
|
+
isLegal(move) { return isLegal(__classPrivateFieldGet(this, _Chess_board, "f").pos, move); }
|
|
1258
1626
|
/** Genuine recursive perft node count (no shortcuts). */
|
|
1259
|
-
perft(depth) { return
|
|
1627
|
+
perft(depth) { return __classPrivateFieldGet(this, _Chess_board, "f").perft(depth); }
|
|
1260
1628
|
/** Applies an engine move in place (immutable core stays pure underneath). */
|
|
1261
1629
|
play(move) {
|
|
1262
|
-
const
|
|
1263
|
-
const
|
|
1264
|
-
|
|
1265
|
-
|
|
1630
|
+
const before = __classPrivateFieldGet(this, _Chess_board, "f").toPosition();
|
|
1631
|
+
const san = makeSan(move, before);
|
|
1632
|
+
const moveWord = packOf(move);
|
|
1633
|
+
const boardUndo = __classPrivateFieldGet(this, _Chess_board, "f").makeMove(moveWord);
|
|
1634
|
+
const after = __classPrivateFieldGet(this, _Chess_board, "f").toPosition();
|
|
1635
|
+
__classPrivateFieldGet(this, _Chess_history, "f").push({
|
|
1636
|
+
before,
|
|
1637
|
+
after,
|
|
1638
|
+
move,
|
|
1639
|
+
san,
|
|
1640
|
+
boardUndo,
|
|
1641
|
+
prev_checkers: before.checkers,
|
|
1642
|
+
prev_zobrist: before.zobristLo !== undefined && before.zobristHi !== undefined ? { lo: before.zobristLo, hi: before.zobristHi } : undefined,
|
|
1643
|
+
});
|
|
1266
1644
|
return this;
|
|
1267
1645
|
}
|
|
1268
1646
|
/** Current 64-bit Zobrist key (zero-BigInt {lo, hi} halves). */
|
|
1269
1647
|
zobrist() {
|
|
1270
|
-
|
|
1271
|
-
return { lo: __classPrivateFieldGet(this, _Chess_pos, "f").zobristLo >>> 0, hi: __classPrivateFieldGet(this, _Chess_pos, "f").zobristHi >>> 0 };
|
|
1272
|
-
}
|
|
1273
|
-
return calculateZobrist(__classPrivateFieldGet(this, _Chess_pos, "f"));
|
|
1648
|
+
return __classPrivateFieldGet(this, _Chess_board, "f").zobrist();
|
|
1274
1649
|
}
|
|
1275
1650
|
/** Current Zobrist key as 16 zero-padded hex digits (hi first). */
|
|
1276
|
-
zobristHex() { return
|
|
1651
|
+
zobristHex() { return __classPrivateFieldGet(this, _Chess_board, "f").zobristHex(); }
|
|
1277
1652
|
/** Packs the game's move history into a 16-bit moves2 stream. */
|
|
1278
1653
|
toMoves2() {
|
|
1279
1654
|
const words = new Uint16Array(__classPrivateFieldGet(this, _Chess_history, "f").length);
|
|
@@ -1286,7 +1661,7 @@ export class Chess {
|
|
|
1286
1661
|
this.load(startFen);
|
|
1287
1662
|
__classPrivateFieldSet(this, _Chess_startFen, startFen, "f");
|
|
1288
1663
|
for (const mv of packedToMoves(buffer)) {
|
|
1289
|
-
if (!isLegal(__classPrivateFieldGet(this,
|
|
1664
|
+
if (!isLegal(__classPrivateFieldGet(this, _Chess_board, "f").pos, mv))
|
|
1290
1665
|
throw new Error(`moves2 replay: illegal move ${mv.from}->${mv.to}`);
|
|
1291
1666
|
this.play(mv);
|
|
1292
1667
|
}
|
|
@@ -1308,7 +1683,9 @@ export class Chess {
|
|
|
1308
1683
|
/** FEN the game started from (for tree roots / PGN headers). */
|
|
1309
1684
|
get startFen() { return __classPrivateFieldGet(this, _Chess_startFen, "f"); }
|
|
1310
1685
|
/** Live engine position (read-only view over the internal state). */
|
|
1311
|
-
get pos() { return __classPrivateFieldGet(this,
|
|
1686
|
+
get pos() { return __classPrivateFieldGet(this, _Chess_board, "f").pos; }
|
|
1687
|
+
get _pos() { return __classPrivateFieldGet(this, _Chess_board, "f").pos; }
|
|
1688
|
+
get boardInstance() { return __classPrivateFieldGet(this, _Chess_board, "f"); }
|
|
1312
1689
|
/** Internal history log (read-only view; used by toTree). */
|
|
1313
1690
|
get historyEntries() { return __classPrivateFieldGet(this, _Chess_history, "f"); }
|
|
1314
1691
|
/**
|
|
@@ -1356,7 +1733,7 @@ export class Chess {
|
|
|
1356
1733
|
return buildTreeWrapper(root ?? { id: "", ply: 0, fen: INITIAL_FEN, uci: "", children: [] });
|
|
1357
1734
|
}
|
|
1358
1735
|
}
|
|
1359
|
-
|
|
1736
|
+
_Chess_board = new WeakMap(), _Chess_startFen = new WeakMap(), _Chess_history = new WeakMap(), _Chess_instances = new WeakSet(), _Chess_movetext = function _Chess_movetext() {
|
|
1360
1737
|
const sans = __classPrivateFieldGet(this, _Chess_history, "f").map((h) => h.san);
|
|
1361
1738
|
let fullmove = parseFullmove(__classPrivateFieldGet(this, _Chess_startFen, "f"));
|
|
1362
1739
|
const blackToStart = __classPrivateFieldGet(this, _Chess_startFen, "f").split(/\s+/)[1] === "b";
|