gigachess 0.4.3 → 0.6.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 +94 -39
- package/dist/attacks.d.ts +12 -4
- package/dist/attacks.d.ts.map +1 -1
- package/dist/attacks.js +161 -74
- package/dist/bishopSplit32Blob.d.ts +2 -0
- package/dist/bishopSplit32Blob.d.ts.map +1 -0
- package/dist/bishopSplit32Blob.js +2 -0
- package/dist/board.d.ts +27 -1
- package/dist/board.d.ts.map +1 -1
- package/dist/board.js +351 -36
- package/dist/castling.d.ts +2 -0
- package/dist/castling.d.ts.map +1 -1
- package/dist/castling.js +27 -12
- package/dist/chess.d.ts +11 -4
- package/dist/chess.d.ts.map +1 -1
- package/dist/chess.js +521 -137
- package/dist/chesstree.d.ts +9 -0
- package/dist/chesstree.d.ts.map +1 -1
- package/dist/chesstree.js +85 -7
- package/dist/core.d.ts +1 -1
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +1 -1
- package/dist/fast/bishopSplit32Blob-NOHWZEWR.js +1 -0
- package/dist/fast/index.js +1 -0
- package/dist/fast/rookSplit32Blob-C3ANUNTJ.js +1 -0
- package/dist/fast/zobristBlob-HIMMLZLJ.js +1 -0
- package/dist/fast.d.ts +12 -0
- package/dist/fast.d.ts.map +1 -0
- package/dist/fast.js +11 -0
- package/dist/fen.d.ts.map +1 -1
- package/dist/fen.js +139 -124
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/perftCompact.d.ts +3 -0
- package/dist/perftCompact.d.ts.map +1 -0
- package/dist/perftCompact.js +358 -0
- package/dist/pgn.d.ts +4 -0
- package/dist/pgn.d.ts.map +1 -1
- package/dist/pgn.js +7 -2
- package/dist/rookSplit32Blob.d.ts +2 -0
- package/dist/rookSplit32Blob.d.ts.map +1 -0
- package/dist/rookSplit32Blob.js +2 -0
- package/dist/san.d.ts.map +1 -1
- package/dist/san.js +14 -40
- package/dist/squareSet.d.ts +2 -0
- package/dist/squareSet.d.ts.map +1 -1
- package/dist/squareSet.js +6 -1
- package/dist/standardMovegen.d.ts +3 -0
- package/dist/standardMovegen.d.ts.map +1 -0
- package/dist/standardMovegen.js +253 -0
- package/dist/zobrist.d.ts.map +1 -1
- package/dist/zobrist.js +17 -15
- package/package.json +15 -6
- package/dist/bishopMagicBlob.d.ts +0 -2
- package/dist/bishopMagicBlob.d.ts.map +0 -1
- package/dist/bishopMagicBlob.js +0 -11
- package/dist/rookMagicBlob.d.ts +0 -2
- package/dist/rookMagicBlob.d.ts.map +0 -1
- package/dist/rookMagicBlob.js +0 -11
package/dist/board.d.ts
CHANGED
|
@@ -78,8 +78,15 @@ export declare class Board implements BoardLike {
|
|
|
78
78
|
constructor(input?: string | Position | Board | BoardLike);
|
|
79
79
|
static startpos(): Board;
|
|
80
80
|
static empty(): Board;
|
|
81
|
-
static fromFen(fen: string
|
|
81
|
+
static fromFen(fen: string, opts?: {
|
|
82
|
+
chess960?: boolean;
|
|
83
|
+
strict?: boolean;
|
|
84
|
+
}): Board;
|
|
82
85
|
static fromMoves2(buffer: Uint16Array | Uint8Array, startFen?: string, isChess960?: boolean): Board;
|
|
86
|
+
/** Decode a moves2 byte stream, including unaligned Uint8Array views. */
|
|
87
|
+
private static moveWords;
|
|
88
|
+
/** Checked moves2 replay for untrusted files and network input. */
|
|
89
|
+
static fromMoves2Checked(buffer: Uint16Array | Uint8Array, startFen?: string, isChess960?: boolean): Board;
|
|
83
90
|
get board(): Board;
|
|
84
91
|
get pos(): Position;
|
|
85
92
|
toPosition(): Position;
|
|
@@ -127,7 +134,13 @@ export declare class Board implements BoardLike {
|
|
|
127
134
|
* interleaved past the matching make — exactly like `unmakeMove`.
|
|
128
135
|
*/
|
|
129
136
|
unmakeNullMove(undo: NullUndo): void;
|
|
137
|
+
/** Public move execution always maintains the Polyglot hash. */
|
|
130
138
|
makeMove(moveWord: number | Move): Undo | NullUndo;
|
|
139
|
+
/** Internal perft-only fast path; the cached hash is invalid until undo.
|
|
140
|
+
* The method is PRIVATE so ordinary callers cannot corrupt their board.
|
|
141
|
+
* Mirrors gigachess-rs make_move_perft without changing public semantics.
|
|
142
|
+
*/
|
|
143
|
+
private _makeMoveCore;
|
|
131
144
|
unmakeMove(undo: Undo | NullUndo): void;
|
|
132
145
|
legalMoves(outBuffer?: Uint16Array): Uint16Array;
|
|
133
146
|
forEachLegalMove(fn: (moveWord: number) => void): void;
|
|
@@ -174,6 +187,19 @@ export declare class Board implements BoardLike {
|
|
|
174
187
|
ok: false;
|
|
175
188
|
code: string;
|
|
176
189
|
};
|
|
190
|
+
/**
|
|
191
|
+
* Perft with caller-owned per-ply scratch buffers and analysis contexts.
|
|
192
|
+
* Correctness: the parent ply's moves and castling plans remain untouched
|
|
193
|
+
* when the recursive child recalculates check/pin/castling information.
|
|
194
|
+
* This preserves the ordinary fully-maintained make/unmake contract.
|
|
195
|
+
*/
|
|
196
|
+
/** Specialized perft-only make: no clock/hash updates or Undo snapshot array.
|
|
197
|
+
* Public makeMove/unmakeMove remain entirely unchanged.
|
|
198
|
+
* All rule-relevant state (castling, en-passant, checkers, turn, piece boards)
|
|
199
|
+
* is updated exactly, and restored by _unmakePerft.
|
|
200
|
+
*/
|
|
201
|
+
private _makePerft;
|
|
202
|
+
private _unmakePerft;
|
|
177
203
|
perft(depth: number): number;
|
|
178
204
|
}
|
|
179
205
|
export type WritableBoard = {
|
package/dist/board.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"board.d.ts","sourceRoot":"","sources":["../src/board.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"board.d.ts","sourceRoot":"","sources":["../src/board.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,IAAI,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AK,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,SAAS,CAO7E;AAED,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAElE;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAMrE;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,OAAO,CAa/D"}
|
package/dist/board.js
CHANGED
|
@@ -1,17 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
// Mirrors gigachess::Board in Rust (ADR-001, ADR-003).
|
|
1
|
+
import { compactPerft } from "./perftCompact.js";
|
|
3
2
|
import * as sq from "./squareSet.js";
|
|
4
3
|
import { Color, Role } from "./types.js";
|
|
5
4
|
import { opposite } from "./util.js";
|
|
6
5
|
import { kingAttackers, attackersTo } from "./attacks.js";
|
|
7
6
|
import { zobristTablesLoaded, calculateZobrist, zobristHex, pieceKeyIdx, castlingKeyIdx, epIsHashable, keyLo, keyHi, castleKeyLo, castleKeyHi, IDX_EP, IDX_SIDE, } from "./zobrist.js";
|
|
8
|
-
import { CASTLE_CLEAR_STD, CASTLING_RIGHTS_TABLE, getCastlingMask, detectCastling, } from "./castling.js";
|
|
7
|
+
import { CASTLE_CLEAR_STD, CASTLING_RIGHTS_TABLE, fixedRookRights, getCastlingMask, detectCastling, } from "./castling.js";
|
|
9
8
|
import { packOf, unpackToMove, promoCodeToRole, isNull, } from "./packedMove.js";
|
|
10
9
|
import { parseFen, makeFen } from "./fen.js";
|
|
11
10
|
import { parseSan, parseSanCandidates, makeSan, parseUci, makeUci } from "./san.js";
|
|
12
|
-
import { legalMovesInto, forEachLegalMove, countLegalMoves, isLegal, INITIAL_FEN } from "./chess.js";
|
|
11
|
+
import { legalMovesInto, forEachLegalMove, countLegalMoves, analyzeCheckContext, isLegal, INITIAL_FEN } from "./chess.js";
|
|
12
|
+
const PERFT_ADJACENT_FILES = Int32Array.from({ length: 64 }, (_, square) => ((square & 7) !== 0 ? 1 << ((square - 1) & 31) : 0) |
|
|
13
|
+
((square & 7) !== 7 ? 1 << ((square + 1) & 31) : 0));
|
|
14
|
+
// Internal synchronous move generation has no user callbacks. Scratch does
|
|
15
|
+
// not escape: each public result is an independently owned typed array.
|
|
16
|
+
const ownedMoveScratch = new Uint16Array(256);
|
|
17
|
+
let ownedMoveScratchBusy = false;
|
|
18
|
+
// Cache only the canonical initial position, not arbitrary user FENs.
|
|
19
|
+
// Each instance receives independent mutable bitboards and king coordinates.
|
|
20
|
+
let initialTemplate;
|
|
21
|
+
let buildingInitialTemplate = false;
|
|
13
22
|
export class Board {
|
|
14
23
|
constructor(input) {
|
|
24
|
+
// Prepare the single canonical template on first ordinary Board use,
|
|
25
|
+
// even for an arbitrary input FEN. In addition to cheap start-position
|
|
26
|
+
// creation, keeping this reusable instance stabilizes V8's weak
|
|
27
|
+
// optimized-code dependencies across full collections.
|
|
28
|
+
// Avoid eager module evaluation (the board/FEN modules are cyclic).
|
|
29
|
+
if (new.target === Board && initialTemplate === undefined && !buildingInitialTemplate) {
|
|
30
|
+
buildingInitialTemplate = true;
|
|
31
|
+
try {
|
|
32
|
+
initialTemplate = new Board(INITIAL_FEN);
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
buildingInitialTemplate = false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// String construction owns the newly parsed board. Unlike loadFen on an
|
|
39
|
+
// existing Board, there are no prior public bitboard references to retain.
|
|
40
|
+
// Subclass initialization keeps the original virtual loadFen behavior.
|
|
41
|
+
if (new.target === Board && (input === undefined || typeof input === "string")) {
|
|
42
|
+
const fen = input ?? INITIAL_FEN;
|
|
43
|
+
const template = fen === INITIAL_FEN ? initialTemplate : undefined;
|
|
44
|
+
let pos, b;
|
|
45
|
+
if (template) {
|
|
46
|
+
pos = template;
|
|
47
|
+
b = cloneAsWritable(template);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const parsed = parseFen(fen);
|
|
51
|
+
if (!parsed.ok)
|
|
52
|
+
throw new Error(`Invalid FEN: ${fen}`);
|
|
53
|
+
pos = parsed.value;
|
|
54
|
+
b = pos.board;
|
|
55
|
+
}
|
|
56
|
+
this.white = b.white;
|
|
57
|
+
this.black = b.black;
|
|
58
|
+
this.pawn = b.pawn;
|
|
59
|
+
this.knight = b.knight;
|
|
60
|
+
this.bishop = b.bishop;
|
|
61
|
+
this.rook = b.rook;
|
|
62
|
+
this.queen = b.queen;
|
|
63
|
+
this.king = b.king;
|
|
64
|
+
this.occupied = b.occupied;
|
|
65
|
+
this.promoted = b.promoted;
|
|
66
|
+
this.kingSq = b.kingSq;
|
|
67
|
+
this.turn = pos.turn;
|
|
68
|
+
this.castling = pos.castling;
|
|
69
|
+
this.castlingMask = pos.castlingMask;
|
|
70
|
+
this.epSquare = pos.epSquare;
|
|
71
|
+
this.halfmoves = pos.halfmoves ?? pos.halfmove ?? 0;
|
|
72
|
+
this.fullmoves = pos.fullmoves ?? pos.fullmove ?? 1;
|
|
73
|
+
this.checkers = template ? { lo: template.checkers.lo, hi: template.checkers.hi } : pos.checkers;
|
|
74
|
+
this.isChess960 = !!pos.isChess960;
|
|
75
|
+
this._zobristLo = pos.zobristLo ?? 0;
|
|
76
|
+
this._zobristHi = pos.zobristHi ?? 0;
|
|
77
|
+
if (this._zobristLo === 0 && this._zobristHi === 0 && zobristTablesLoaded())
|
|
78
|
+
this.refreshZobrist();
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
15
81
|
this.white = { lo: 0, hi: 0 };
|
|
16
82
|
this.black = { lo: 0, hi: 0 };
|
|
17
83
|
this.pawn = { lo: 0, hi: 0 };
|
|
@@ -58,18 +124,52 @@ export class Board {
|
|
|
58
124
|
b.clear();
|
|
59
125
|
return b;
|
|
60
126
|
}
|
|
61
|
-
static fromFen(fen) {
|
|
62
|
-
|
|
127
|
+
static fromFen(fen, opts) {
|
|
128
|
+
if (!opts)
|
|
129
|
+
return new Board(fen);
|
|
130
|
+
const parsed = parseFen(fen, opts);
|
|
131
|
+
if (!parsed.ok)
|
|
132
|
+
throw new Error(`Invalid FEN: ${fen}`);
|
|
133
|
+
return new Board(parsed.value);
|
|
63
134
|
}
|
|
64
135
|
static fromMoves2(buffer, startFen = INITIAL_FEN, isChess960 = false) {
|
|
65
|
-
const b =
|
|
66
|
-
|
|
67
|
-
|
|
136
|
+
const b = Board.fromFen(startFen, isChess960 ? { chess960: true } : undefined);
|
|
137
|
+
// moves2 is little-endian; a Uint8Array slice may have an odd byte offset.
|
|
138
|
+
// Retain zero-copy for aligned words and decode only unaligned input.
|
|
139
|
+
const moves = Board.moveWords(buffer);
|
|
68
140
|
for (let i = 0; i < moves.length; i++) {
|
|
69
141
|
b.makeMove(moves[i]);
|
|
70
142
|
}
|
|
71
143
|
return b;
|
|
72
144
|
}
|
|
145
|
+
/** Decode a moves2 byte stream, including unaligned Uint8Array views. */
|
|
146
|
+
static moveWords(buffer) {
|
|
147
|
+
if (buffer instanceof Uint16Array)
|
|
148
|
+
return buffer;
|
|
149
|
+
const length = buffer.byteLength >>> 1;
|
|
150
|
+
if ((buffer.byteOffset & 1) === 0) {
|
|
151
|
+
return new Uint16Array(buffer.buffer, buffer.byteOffset, length);
|
|
152
|
+
}
|
|
153
|
+
const words = new Uint16Array(length);
|
|
154
|
+
for (let i = 0; i < length; i++)
|
|
155
|
+
words[i] = buffer[i * 2] | (buffer[i * 2 + 1] << 8);
|
|
156
|
+
return words;
|
|
157
|
+
}
|
|
158
|
+
/** Checked moves2 replay for untrusted files and network input. */
|
|
159
|
+
static fromMoves2Checked(buffer, startFen = INITIAL_FEN, isChess960 = false) {
|
|
160
|
+
if (buffer instanceof Uint8Array && !(buffer instanceof Uint16Array) && (buffer.byteLength & 1)) {
|
|
161
|
+
throw new Error("moves2: truncated 16-bit word");
|
|
162
|
+
}
|
|
163
|
+
const b = Board.fromFen(startFen, isChess960 ? { chess960: true } : undefined);
|
|
164
|
+
const moves = Board.moveWords(buffer);
|
|
165
|
+
for (let i = 0; i < moves.length; i++) {
|
|
166
|
+
if (!isLegal(b.pos, unpackToMove(moves[i]))) {
|
|
167
|
+
throw new Error(`moves2: illegal move at ply ${i + 1}`);
|
|
168
|
+
}
|
|
169
|
+
b.makeMove(moves[i]);
|
|
170
|
+
}
|
|
171
|
+
return b;
|
|
172
|
+
}
|
|
73
173
|
get board() {
|
|
74
174
|
return this;
|
|
75
175
|
}
|
|
@@ -215,14 +315,16 @@ export class Board {
|
|
|
215
315
|
this.promoted.hi = b.promoted.hi;
|
|
216
316
|
const pos = src;
|
|
217
317
|
this.turn = pos.turn ?? Color.White;
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
318
|
+
const rights = pos.castling;
|
|
319
|
+
this.castling = rights
|
|
320
|
+
? (!pos.isChess960 && pos.castlingMask !== undefined
|
|
321
|
+
? CASTLING_RIGHTS_TABLE[pos.castlingMask]
|
|
322
|
+
: Object.freeze({
|
|
323
|
+
...rights,
|
|
324
|
+
white: fixedRookRights(rights.white),
|
|
325
|
+
black: fixedRookRights(rights.black),
|
|
326
|
+
}))
|
|
327
|
+
: CASTLING_RIGHTS_TABLE[0];
|
|
226
328
|
this.castlingMask = pos.castlingMask;
|
|
227
329
|
this.epSquare = pos.epSquare ?? null;
|
|
228
330
|
this.halfmoves = pos.halfmoves ?? pos.halfmove ?? 0;
|
|
@@ -410,6 +512,9 @@ export class Board {
|
|
|
410
512
|
* `Board::make_null_move`.
|
|
411
513
|
*/
|
|
412
514
|
makeNullMove() {
|
|
515
|
+
if (zobristTablesLoaded() && this._zobristLo === 0 && this._zobristHi === 0) {
|
|
516
|
+
this.refreshZobrist();
|
|
517
|
+
}
|
|
413
518
|
const us = this.turn;
|
|
414
519
|
const them = opposite(us);
|
|
415
520
|
// Fresh in-check scan — the cache may describe an earlier position.
|
|
@@ -469,7 +574,20 @@ export class Board {
|
|
|
469
574
|
this._zobristLo = undo.zobristLo;
|
|
470
575
|
this._zobristHi = undo.zobristHi;
|
|
471
576
|
}
|
|
577
|
+
/** Public move execution always maintains the Polyglot hash. */
|
|
472
578
|
makeMove(moveWord) {
|
|
579
|
+
return this._makeMoveCore(moveWord, true);
|
|
580
|
+
}
|
|
581
|
+
/** Internal perft-only fast path; the cached hash is invalid until undo.
|
|
582
|
+
* The method is PRIVATE so ordinary callers cannot corrupt their board.
|
|
583
|
+
* Mirrors gigachess-rs make_move_perft without changing public semantics.
|
|
584
|
+
*/
|
|
585
|
+
_makeMoveCore(moveWord, updateHash) {
|
|
586
|
+
// Tables can become ready after this Board was constructed; refresh the
|
|
587
|
+
// pre-move state BEFORE edits, never hash the new state as the old key.
|
|
588
|
+
if (updateHash && zobristTablesLoaded() && this._zobristLo === 0 && this._zobristHi === 0) {
|
|
589
|
+
this.refreshZobrist();
|
|
590
|
+
}
|
|
473
591
|
const word = typeof moveWord === "number" ? moveWord : packOf(moveWord);
|
|
474
592
|
// The null sentinel is a first-class pass, not a from/to/promo word.
|
|
475
593
|
if (isNull(word))
|
|
@@ -493,7 +611,7 @@ export class Board {
|
|
|
493
611
|
const prevHalfmoves = this.halfmoves;
|
|
494
612
|
const prevFullmoves = this.fullmoves;
|
|
495
613
|
const prevKingSq = [this.kingSq[0], this.kingSq[1]];
|
|
496
|
-
const prevEpHashable = prevEpSquare !== null && zobristTablesLoaded() && epIsHashable(this, prevEpSquare, us);
|
|
614
|
+
const prevEpHashable = updateHash && prevEpSquare !== null && zobristTablesLoaded() && epIsHashable(this, prevEpSquare, us);
|
|
497
615
|
let plan = null;
|
|
498
616
|
if (movingRole === Role.King) {
|
|
499
617
|
plan = detectCastling(this, from, to);
|
|
@@ -589,7 +707,13 @@ export class Board {
|
|
|
589
707
|
blackQueen: newBlack.has(56),
|
|
590
708
|
};
|
|
591
709
|
}
|
|
592
|
-
this.castling =
|
|
710
|
+
this.castling = this.isChess960
|
|
711
|
+
? Object.freeze({
|
|
712
|
+
...newCastling,
|
|
713
|
+
white: newCastling.white === prevCastling.white ? prevCastling.white : fixedRookRights(newCastling.white),
|
|
714
|
+
black: newCastling.black === prevCastling.black ? prevCastling.black : fixedRookRights(newCastling.black),
|
|
715
|
+
})
|
|
716
|
+
: newCastling;
|
|
593
717
|
this.castlingMask = newMask;
|
|
594
718
|
let newEp = null;
|
|
595
719
|
if (movingRole === Role.Pawn && Math.abs((to >> 3) - (from >> 3)) === 2) {
|
|
@@ -603,10 +727,7 @@ export class Board {
|
|
|
603
727
|
this.fullmoves = us === Color.Black ? prevFullmoves + 1 : prevFullmoves;
|
|
604
728
|
this.turn = them;
|
|
605
729
|
this.checkers = kingAttackers(this, them);
|
|
606
|
-
if (zobristTablesLoaded() && keyLo && keyHi && castleKeyLo && castleKeyHi) {
|
|
607
|
-
if (this._zobristLo === 0 && this._zobristHi === 0) {
|
|
608
|
-
this.refreshZobrist();
|
|
609
|
-
}
|
|
730
|
+
if (updateHash && zobristTablesLoaded() && keyLo && keyHi && castleKeyLo && castleKeyHi) {
|
|
610
731
|
let zLo = this._zobristLo;
|
|
611
732
|
let zHi = this._zobristHi;
|
|
612
733
|
// 1. Moving piece leaves from
|
|
@@ -761,9 +882,24 @@ export class Board {
|
|
|
761
882
|
}
|
|
762
883
|
}
|
|
763
884
|
legalMoves(outBuffer) {
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
885
|
+
if (outBuffer) {
|
|
886
|
+
const count = legalMovesInto(this, outBuffer);
|
|
887
|
+
return outBuffer.subarray(0, count);
|
|
888
|
+
}
|
|
889
|
+
// Reentrant calls (e.g. through custom property accessors) must not
|
|
890
|
+
// overwrite an in-flight list. Caller-owned buffer contract unchanged.
|
|
891
|
+
if (ownedMoveScratchBusy) {
|
|
892
|
+
const local = new Uint16Array(256);
|
|
893
|
+
return local.subarray(0, legalMovesInto(this, local));
|
|
894
|
+
}
|
|
895
|
+
ownedMoveScratchBusy = true;
|
|
896
|
+
try {
|
|
897
|
+
const count = legalMovesInto(this, ownedMoveScratch);
|
|
898
|
+
return ownedMoveScratch.slice(0, count);
|
|
899
|
+
}
|
|
900
|
+
finally {
|
|
901
|
+
ownedMoveScratchBusy = false;
|
|
902
|
+
}
|
|
767
903
|
}
|
|
768
904
|
forEachLegalMove(fn) {
|
|
769
905
|
forEachLegalMove(this, (from, to, promo) => {
|
|
@@ -825,20 +961,199 @@ export class Board {
|
|
|
825
961
|
? { ok: true, moveWords: res.value.map(packOf) }
|
|
826
962
|
: { ok: false, code: res.error.code };
|
|
827
963
|
}
|
|
964
|
+
/**
|
|
965
|
+
* Perft with caller-owned per-ply scratch buffers and analysis contexts.
|
|
966
|
+
* Correctness: the parent ply's moves and castling plans remain untouched
|
|
967
|
+
* when the recursive child recalculates check/pin/castling information.
|
|
968
|
+
* This preserves the ordinary fully-maintained make/unmake contract.
|
|
969
|
+
*/
|
|
970
|
+
/** Specialized perft-only make: no clock/hash updates or Undo snapshot array.
|
|
971
|
+
* Public makeMove/unmakeMove remain entirely unchanged.
|
|
972
|
+
* All rule-relevant state (castling, en-passant, checkers, turn, piece boards)
|
|
973
|
+
* is updated exactly, and restored by _unmakePerft.
|
|
974
|
+
*/
|
|
975
|
+
_makePerft(word, pieceCodes) {
|
|
976
|
+
const from = word & 63, to = (word >>> 6) & 63;
|
|
977
|
+
const promoRole = promoCodeToRole((word >>> 12) & 15) ?? null;
|
|
978
|
+
const movingRole = (pieceCodes[from] - 1);
|
|
979
|
+
const us = this.turn;
|
|
980
|
+
const them = opposite(us);
|
|
981
|
+
const prevCastling = this.castling;
|
|
982
|
+
const prevMask = this.castlingMask;
|
|
983
|
+
const prevEp = this.epSquare;
|
|
984
|
+
const prevCheckers = this.checkers;
|
|
985
|
+
const plan = movingRole === Role.King ? detectCastling(this, from, to) : null;
|
|
986
|
+
const isEp = movingRole === Role.Pawn && prevEp !== null &&
|
|
987
|
+
to === prevEp && (from & 7) !== (to & 7);
|
|
988
|
+
let capturedRole = -1, capturedSq = -1;
|
|
989
|
+
if (isEp) {
|
|
990
|
+
capturedRole = Role.Pawn;
|
|
991
|
+
capturedSq = us === Color.White ? to - 8 : to + 8;
|
|
992
|
+
}
|
|
993
|
+
else if (plan === null) {
|
|
994
|
+
const cap = pieceCodes[to] === 0 ? undefined : (pieceCodes[to] - 1);
|
|
995
|
+
if (cap !== undefined) {
|
|
996
|
+
capturedRole = cap;
|
|
997
|
+
capturedSq = to;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
// Perft-private exact square cache: eliminates roleAt bitboard probing.
|
|
1001
|
+
// Update in the same order as chess960 overlapping source/dest edits.
|
|
1002
|
+
pieceCodes[from] = 0;
|
|
1003
|
+
if (capturedSq >= 0)
|
|
1004
|
+
pieceCodes[capturedSq] = 0;
|
|
1005
|
+
this._clearSquare(from);
|
|
1006
|
+
if (capturedSq >= 0)
|
|
1007
|
+
this._clearSquare(capturedSq);
|
|
1008
|
+
if (plan !== null) {
|
|
1009
|
+
pieceCodes[plan.rookFrom] = 0;
|
|
1010
|
+
pieceCodes[plan.rookTo] = Role.Rook + 1;
|
|
1011
|
+
pieceCodes[plan.kingTo] = Role.King + 1;
|
|
1012
|
+
this._clearSquare(plan.rookFrom);
|
|
1013
|
+
this._putPiece(plan.rookTo, us, Role.Rook);
|
|
1014
|
+
this._putPiece(plan.kingTo, us, Role.King);
|
|
1015
|
+
this.kingSq[us] = plan.kingTo;
|
|
1016
|
+
}
|
|
1017
|
+
else {
|
|
1018
|
+
pieceCodes[to] = (promoRole ?? movingRole) + 1;
|
|
1019
|
+
this._putPiece(to, us, promoRole ?? movingRole);
|
|
1020
|
+
if (movingRole === Role.King)
|
|
1021
|
+
this.kingSq[us] = to;
|
|
1022
|
+
}
|
|
1023
|
+
if (!this.isChess960) {
|
|
1024
|
+
const mask = prevMask ?? getCastlingMask(this);
|
|
1025
|
+
const updated = (mask & CASTLE_CLEAR_STD[from] & CASTLE_CLEAR_STD[to]) >>> 0;
|
|
1026
|
+
this.castlingMask = updated;
|
|
1027
|
+
this.castling = CASTLING_RIGHTS_TABLE[updated];
|
|
1028
|
+
}
|
|
1029
|
+
else {
|
|
1030
|
+
let nextWhite = prevCastling.white;
|
|
1031
|
+
let nextBlack = prevCastling.black;
|
|
1032
|
+
if (movingRole === Role.King) {
|
|
1033
|
+
if (us === Color.White) {
|
|
1034
|
+
if (nextWhite.size)
|
|
1035
|
+
nextWhite = new Set();
|
|
1036
|
+
}
|
|
1037
|
+
else if (nextBlack.size)
|
|
1038
|
+
nextBlack = new Set();
|
|
1039
|
+
}
|
|
1040
|
+
if (movingRole === Role.Rook) {
|
|
1041
|
+
if (us === Color.White && nextWhite.has(from)) {
|
|
1042
|
+
const next = new Set(nextWhite);
|
|
1043
|
+
next.delete(from);
|
|
1044
|
+
nextWhite = next;
|
|
1045
|
+
}
|
|
1046
|
+
else if (us === Color.Black && nextBlack.has(from)) {
|
|
1047
|
+
const next = new Set(nextBlack);
|
|
1048
|
+
next.delete(from);
|
|
1049
|
+
nextBlack = next;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
if (capturedRole === Role.Rook && capturedSq >= 0) {
|
|
1053
|
+
if (them === Color.White && nextWhite.has(capturedSq)) {
|
|
1054
|
+
const next = new Set(nextWhite);
|
|
1055
|
+
next.delete(capturedSq);
|
|
1056
|
+
nextWhite = next;
|
|
1057
|
+
}
|
|
1058
|
+
else if (them === Color.Black && nextBlack.has(capturedSq)) {
|
|
1059
|
+
const next = new Set(nextBlack);
|
|
1060
|
+
next.delete(capturedSq);
|
|
1061
|
+
nextBlack = next;
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
if (nextWhite !== prevCastling.white || nextBlack !== prevCastling.black) {
|
|
1065
|
+
this.castling = Object.freeze({
|
|
1066
|
+
white: fixedRookRights(nextWhite),
|
|
1067
|
+
black: fixedRookRights(nextBlack),
|
|
1068
|
+
whiteKing: nextWhite.has(7), whiteQueen: nextWhite.has(0),
|
|
1069
|
+
blackKing: nextBlack.has(63), blackQueen: nextBlack.has(56),
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
this.epSquare = null;
|
|
1074
|
+
if (movingRole === Role.Pawn && Math.abs(to - from) === 16) {
|
|
1075
|
+
// The target of a double push is reachable only by an opposing
|
|
1076
|
+
// pawn next to the mover on the same rank. No cross-halfboard shifts.
|
|
1077
|
+
const enemy = them === Color.White ? this.white : this.black;
|
|
1078
|
+
const adjacent = PERFT_ADJACENT_FILES[to];
|
|
1079
|
+
if ((us === Color.White
|
|
1080
|
+
? this.pawn.lo & enemy.lo & adjacent
|
|
1081
|
+
: this.pawn.hi & enemy.hi & adjacent) !== 0) {
|
|
1082
|
+
this.epSquare = (from + to) >>> 1;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
this.turn = them;
|
|
1086
|
+
this.checkers = kingAttackers(this, them);
|
|
1087
|
+
return {
|
|
1088
|
+
word, movingRole, capturedRole, capturedSq, promoRole,
|
|
1089
|
+
castlingPlan: plan, castling: prevCastling, castlingMask: prevMask,
|
|
1090
|
+
epSquare: prevEp, checkers: prevCheckers,
|
|
1091
|
+
};
|
|
1092
|
+
}
|
|
1093
|
+
_unmakePerft(undo, pieceCodes) {
|
|
1094
|
+
const from = undo.word & 63, to = (undo.word >>> 6) & 63;
|
|
1095
|
+
const us = opposite(this.turn);
|
|
1096
|
+
this.turn = us;
|
|
1097
|
+
this.castling = undo.castling;
|
|
1098
|
+
this.castlingMask = undo.castlingMask;
|
|
1099
|
+
this.epSquare = undo.epSquare;
|
|
1100
|
+
this.checkers = undo.checkers;
|
|
1101
|
+
if (undo.castlingPlan !== null) {
|
|
1102
|
+
const plan = undo.castlingPlan;
|
|
1103
|
+
pieceCodes[plan.kingTo] = 0;
|
|
1104
|
+
pieceCodes[plan.rookTo] = 0;
|
|
1105
|
+
pieceCodes[plan.kingFrom] = Role.King + 1;
|
|
1106
|
+
pieceCodes[plan.rookFrom] = Role.Rook + 1;
|
|
1107
|
+
this._clearSquare(plan.kingTo);
|
|
1108
|
+
this._clearSquare(plan.rookTo);
|
|
1109
|
+
this._putPiece(plan.kingFrom, us, Role.King);
|
|
1110
|
+
this._putPiece(plan.rookFrom, us, Role.Rook);
|
|
1111
|
+
this.kingSq[us] = plan.kingFrom;
|
|
1112
|
+
}
|
|
1113
|
+
else {
|
|
1114
|
+
pieceCodes[to] = 0;
|
|
1115
|
+
pieceCodes[from] = undo.movingRole + 1;
|
|
1116
|
+
this._clearSquare(to);
|
|
1117
|
+
this._putPiece(from, us, undo.movingRole);
|
|
1118
|
+
if (undo.capturedSq >= 0) {
|
|
1119
|
+
pieceCodes[undo.capturedSq] = undo.capturedRole + 1;
|
|
1120
|
+
this._putPiece(undo.capturedSq, opposite(us), undo.capturedRole);
|
|
1121
|
+
}
|
|
1122
|
+
if (undo.movingRole === Role.King)
|
|
1123
|
+
this.kingSq[us] = from;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
828
1126
|
perft(depth) {
|
|
829
1127
|
if (depth <= 0)
|
|
830
1128
|
return 1;
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
const
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
this.
|
|
1129
|
+
const compact = compactPerft(this, depth);
|
|
1130
|
+
if (compact !== undefined)
|
|
1131
|
+
return compact;
|
|
1132
|
+
const lists = Array.from({ length: depth }, () => new Uint16Array(256));
|
|
1133
|
+
const contexts = new Array(depth);
|
|
1134
|
+
// Local to this invocation; never observed by public move API.
|
|
1135
|
+
const pieceCodes = new Uint8Array(64);
|
|
1136
|
+
for (let sqIdx = 0; sqIdx < 64; sqIdx++) {
|
|
1137
|
+
const role = this.roleAt(sqIdx);
|
|
1138
|
+
if (role !== undefined)
|
|
1139
|
+
pieceCodes[sqIdx] = role + 1;
|
|
840
1140
|
}
|
|
841
|
-
|
|
1141
|
+
const visit = (ply, remaining) => {
|
|
1142
|
+
const ctx = analyzeCheckContext(this, contexts[ply], true);
|
|
1143
|
+
contexts[ply] = ctx;
|
|
1144
|
+
if (remaining === 1)
|
|
1145
|
+
return countLegalMoves(this, ctx);
|
|
1146
|
+
const moves = lists[ply];
|
|
1147
|
+
const count = legalMovesInto(this, moves, ctx);
|
|
1148
|
+
let nodes = 0;
|
|
1149
|
+
for (let i = 0; i < count; i++) {
|
|
1150
|
+
const undo = this._makePerft(moves[i], pieceCodes);
|
|
1151
|
+
nodes += visit(ply + 1, remaining - 1);
|
|
1152
|
+
this._unmakePerft(undo, pieceCodes);
|
|
1153
|
+
}
|
|
1154
|
+
return nodes;
|
|
1155
|
+
};
|
|
1156
|
+
return visit(0, depth);
|
|
842
1157
|
}
|
|
843
1158
|
}
|
|
844
1159
|
export function emptyBoard() {
|
package/dist/castling.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ export declare const PLAN_BLACK_K: CastlingPlan;
|
|
|
23
23
|
export declare const PLAN_BLACK_Q: CastlingPlan;
|
|
24
24
|
/** Precomputed 16-entry FEN castling string lookup table */
|
|
25
25
|
export declare const CASTLING_FEN_STR: readonly string[];
|
|
26
|
+
/** A genuinely immutable ReadonlySet: Object.freeze(new Set()) is NOT safe. */
|
|
27
|
+
export declare function fixedRookRights(squares: Iterable<number>): ReadonlySet<number>;
|
|
26
28
|
/** Precomputed 16-entry CastlingRights table for standard chess (zero allocations) */
|
|
27
29
|
export declare const CASTLING_RIGHTS_TABLE: readonly CastlingRights[];
|
|
28
30
|
export declare function getCastlingMask(pos: Position): number;
|
package/dist/castling.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"castling.d.ts","sourceRoot":"","sources":["../src/castling.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,KAAK,EAAQ,MAAM,YAAY,CAAC;AAGzC,MAAM,MAAM,YAAY,GAA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", "KQk",
|
|
51
51
|
"q", "Kq", "Qq", "KQq", "kq", "Kkq", "Qkq", "KQkq"
|
|
52
52
|
];
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
/** A genuinely immutable ReadonlySet: Object.freeze(new Set()) is NOT safe. */
|
|
54
|
+
export function fixedRookRights(squares) {
|
|
55
|
+
const entries = Object.freeze(Array.from(squares));
|
|
56
|
+
const rights = Object.freeze({
|
|
57
|
+
get size() { return entries.length; },
|
|
58
|
+
has(square) { return entries.includes(square); },
|
|
59
|
+
keys() { return entries.values(); },
|
|
60
|
+
values() { return entries.values(); },
|
|
61
|
+
entries() { return entries.map((square) => [square, square]).values(); },
|
|
62
|
+
forEach(callback, thisArg) {
|
|
63
|
+
for (const square of entries)
|
|
64
|
+
callback.call(thisArg, square, square, rights);
|
|
65
|
+
},
|
|
66
|
+
[Symbol.iterator]() { return entries.values(); },
|
|
67
|
+
[Symbol.toStringTag]: "Set",
|
|
68
|
+
});
|
|
69
|
+
return rights;
|
|
70
|
+
}
|
|
71
|
+
const WHITE_SETS = [
|
|
72
|
+
fixedRookRights([]), fixedRookRights([7]), fixedRookRights([0]), fixedRookRights([0, 7]),
|
|
73
|
+
];
|
|
74
|
+
const BLACK_SETS = [
|
|
75
|
+
fixedRookRights([]), fixedRookRights([63]), fixedRookRights([56]), fixedRookRights([56, 63]),
|
|
76
|
+
];
|
|
62
77
|
/** Precomputed 16-entry CastlingRights table for standard chess (zero allocations) */
|
|
63
78
|
export const CASTLING_RIGHTS_TABLE = (() => {
|
|
64
79
|
const table = [];
|
|
65
80
|
for (let mask = 0; mask < 16; mask++) {
|
|
66
|
-
table.push({
|
|
81
|
+
table.push(Object.freeze({
|
|
67
82
|
white: WHITE_SETS[mask & 3],
|
|
68
83
|
black: BLACK_SETS[(mask >> 2) & 3],
|
|
69
84
|
whiteKing: (mask & CASTLE_WK) !== 0,
|
|
@@ -71,9 +86,9 @@ export const CASTLING_RIGHTS_TABLE = (() => {
|
|
|
71
86
|
blackKing: (mask & CASTLE_BK) !== 0,
|
|
72
87
|
blackQueen: (mask & CASTLE_BQ) !== 0,
|
|
73
88
|
mask,
|
|
74
|
-
});
|
|
89
|
+
}));
|
|
75
90
|
}
|
|
76
|
-
return table;
|
|
91
|
+
return Object.freeze(table);
|
|
77
92
|
})();
|
|
78
93
|
export function getCastlingMask(pos) {
|
|
79
94
|
if (pos.castlingMask !== undefined)
|