gigachess 0.5.0 → 0.7.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 +142 -54
- package/dist/attacks.d.ts +18 -7
- package/dist/attacks.d.ts.map +1 -1
- package/dist/attacks.js +197 -89
- package/dist/bishopSplit32Blob.d.ts +2 -0
- package/dist/bishopSplit32Blob.d.ts.map +1 -0
- package/dist/bishopSplit32Blob.js +2 -0
- package/dist/castling.d.ts +2 -0
- package/dist/castling.d.ts.map +1 -1
- package/dist/castling.js +27 -12
- package/dist/chess960.d.ts +2 -9
- package/dist/chess960.d.ts.map +1 -1
- package/dist/chess960.js +2 -18
- package/dist/chessjs.d.ts +1 -1
- package/dist/chessjs.d.ts.map +1 -1
- package/dist/chessjs.js +1 -1
- package/dist/chessops/board.d.ts +1 -1
- package/dist/chessops/board.d.ts.map +1 -1
- package/dist/chessops/board.js +1 -1
- package/dist/chessops/chess.d.ts +6 -10
- package/dist/chessops/chess.d.ts.map +1 -1
- package/dist/chessops/chess.js +73 -97
- package/dist/chessops/convert.d.ts +1 -1
- package/dist/chessops/convert.d.ts.map +1 -1
- package/dist/chessops/convert.js +1 -1
- package/dist/chessops/debug.d.ts.map +1 -1
- package/dist/chessops/debug.js +7 -0
- package/dist/chessops/fen.d.ts.map +1 -1
- package/dist/chessops/fen.js +13 -2
- package/dist/chessops/fenInternal.d.ts +1 -1
- package/dist/chessops/fenInternal.d.ts.map +1 -1
- package/dist/chessops/fenInternal.js +1 -1
- package/dist/chessops/nativeBridge.d.ts +14 -0
- package/dist/chessops/nativeBridge.d.ts.map +1 -0
- package/dist/chessops/nativeBridge.js +87 -0
- package/dist/chessops/san.d.ts.map +1 -1
- package/dist/chessops/san.js +12 -10
- package/dist/chesstree.d.ts.map +1 -1
- package/dist/chesstree.js +23 -62
- package/dist/core.d.ts +6 -8
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +5 -10
- 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 +2 -0
- package/dist/fast.d.ts.map +1 -0
- package/dist/fast.js +2 -0
- package/dist/index.d.ts +1 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -19
- package/dist/native96.d.ts +16 -0
- package/dist/native96.d.ts.map +1 -0
- package/dist/native96.js +6 -0
- package/dist/nativeBoard96.d.ts +118 -0
- package/dist/nativeBoard96.d.ts.map +1 -0
- package/dist/nativeBoard96.js +1077 -0
- package/dist/nativeChess.d.ts +97 -0
- package/dist/nativeChess.d.ts.map +1 -0
- package/dist/nativeChess.js +201 -0
- package/dist/nativeMovegen96.d.ts +2 -0
- package/dist/nativeMovegen96.d.ts.map +1 -0
- package/dist/nativeMovegen96.js +282 -0
- package/dist/nativePublicTypes.d.ts +3 -0
- package/dist/nativePublicTypes.d.ts.map +1 -0
- package/dist/nativePublicTypes.js +1 -0
- package/dist/perftCompact.d.ts +10 -0
- package/dist/perftCompact.d.ts.map +1 -0
- package/dist/perftCompact.js +420 -0
- package/dist/pgn.d.ts +2 -3
- package/dist/pgn.d.ts.map +1 -1
- package/dist/pgn.js +2 -5
- package/dist/rookSplit32Blob.d.ts +2 -0
- package/dist/rookSplit32Blob.d.ts.map +1 -0
- package/dist/rookSplit32Blob.js +2 -0
- package/dist/setupBoard.d.ts +54 -0
- package/dist/setupBoard.d.ts.map +1 -0
- package/dist/setupBoard.js +249 -0
- package/dist/{fen.d.ts → setupFen.d.ts} +1 -1
- package/dist/setupFen.d.ts.map +1 -0
- package/dist/{fen.js → setupFen.js} +140 -125
- package/dist/squareSet.d.ts +2 -0
- package/dist/squareSet.d.ts.map +1 -1
- package/dist/squareSet.js +6 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/zobrist.d.ts +2 -2
- package/dist/zobrist.d.ts.map +1 -1
- package/dist/zobrist.js +17 -15
- package/package.json +18 -6
- package/dist/bishopMagicBlob.d.ts +0 -2
- package/dist/bishopMagicBlob.d.ts.map +0 -1
- package/dist/bishopMagicBlob.js +0 -11
- package/dist/board.d.ts +0 -216
- package/dist/board.d.ts.map +0 -1
- package/dist/board.js +0 -1088
- package/dist/chess.d.ts +0 -252
- package/dist/chess.d.ts.map +0 -1
- package/dist/chess.js +0 -1930
- package/dist/fen.d.ts.map +0 -1
- package/dist/rookMagicBlob.d.ts +0 -2
- package/dist/rookMagicBlob.d.ts.map +0 -1
- package/dist/rookMagicBlob.js +0 -11
- package/dist/san.d.ts +0 -38
- package/dist/san.d.ts.map +0 -1
- package/dist/san.js +0 -549
package/dist/board.js
DELETED
|
@@ -1,1088 +0,0 @@
|
|
|
1
|
-
// src/board.ts — stateful high-performance Board class and bitboard operations
|
|
2
|
-
// Mirrors gigachess::Board in Rust (ADR-001, ADR-003).
|
|
3
|
-
import * as sq from "./squareSet.js";
|
|
4
|
-
import { Color, Role } from "./types.js";
|
|
5
|
-
import { opposite } from "./util.js";
|
|
6
|
-
import { kingAttackers, attackersTo } from "./attacks.js";
|
|
7
|
-
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";
|
|
9
|
-
import { packOf, unpackToMove, promoCodeToRole, isNull, } from "./packedMove.js";
|
|
10
|
-
import { parseFen, makeFen } from "./fen.js";
|
|
11
|
-
import { parseSan, parseSanCandidates, makeSan, parseUci, makeUci } from "./san.js";
|
|
12
|
-
import { legalMovesInto, forEachLegalMove, countLegalMoves, isLegal, INITIAL_FEN } from "./chess.js";
|
|
13
|
-
export class Board {
|
|
14
|
-
constructor(input) {
|
|
15
|
-
this.white = { lo: 0, hi: 0 };
|
|
16
|
-
this.black = { lo: 0, hi: 0 };
|
|
17
|
-
this.pawn = { lo: 0, hi: 0 };
|
|
18
|
-
this.knight = { lo: 0, hi: 0 };
|
|
19
|
-
this.bishop = { lo: 0, hi: 0 };
|
|
20
|
-
this.rook = { lo: 0, hi: 0 };
|
|
21
|
-
this.queen = { lo: 0, hi: 0 };
|
|
22
|
-
this.king = { lo: 0, hi: 0 };
|
|
23
|
-
this.occupied = { lo: 0, hi: 0 };
|
|
24
|
-
this.promoted = { lo: 0, hi: 0 };
|
|
25
|
-
this.kingSq = [-1, -1];
|
|
26
|
-
this.turn = Color.White;
|
|
27
|
-
this.castling = {
|
|
28
|
-
white: new Set(),
|
|
29
|
-
black: new Set(),
|
|
30
|
-
whiteKing: false,
|
|
31
|
-
whiteQueen: false,
|
|
32
|
-
blackKing: false,
|
|
33
|
-
blackQueen: false,
|
|
34
|
-
};
|
|
35
|
-
this.castlingMask = 0;
|
|
36
|
-
this.epSquare = null;
|
|
37
|
-
this.halfmoves = 0;
|
|
38
|
-
this.fullmoves = 1;
|
|
39
|
-
this.checkers = { lo: 0, hi: 0 };
|
|
40
|
-
this._zobristLo = 0;
|
|
41
|
-
this._zobristHi = 0;
|
|
42
|
-
this.isChess960 = false;
|
|
43
|
-
if (input === undefined) {
|
|
44
|
-
this.loadFen(INITIAL_FEN);
|
|
45
|
-
}
|
|
46
|
-
else if (typeof input === "string") {
|
|
47
|
-
this.loadFen(input);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
this.copyFrom(input);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
static startpos() {
|
|
54
|
-
return new Board();
|
|
55
|
-
}
|
|
56
|
-
static empty() {
|
|
57
|
-
const b = new Board();
|
|
58
|
-
b.clear();
|
|
59
|
-
return b;
|
|
60
|
-
}
|
|
61
|
-
static fromFen(fen) {
|
|
62
|
-
return new Board(fen);
|
|
63
|
-
}
|
|
64
|
-
static fromMoves2(buffer, startFen = INITIAL_FEN, isChess960 = false) {
|
|
65
|
-
const b = new Board(startFen);
|
|
66
|
-
b.isChess960 = isChess960;
|
|
67
|
-
const moves = buffer instanceof Uint16Array ? buffer : new Uint16Array(buffer.buffer, buffer.byteOffset, buffer.byteLength >> 1);
|
|
68
|
-
for (let i = 0; i < moves.length; i++) {
|
|
69
|
-
b.makeMove(moves[i]);
|
|
70
|
-
}
|
|
71
|
-
return b;
|
|
72
|
-
}
|
|
73
|
-
get board() {
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
get pos() {
|
|
77
|
-
return this;
|
|
78
|
-
}
|
|
79
|
-
toPosition() {
|
|
80
|
-
return {
|
|
81
|
-
board: {
|
|
82
|
-
white: { lo: this.white.lo >>> 0, hi: this.white.hi >>> 0 },
|
|
83
|
-
black: { lo: this.black.lo >>> 0, hi: this.black.hi >>> 0 },
|
|
84
|
-
pawn: { lo: this.pawn.lo >>> 0, hi: this.pawn.hi >>> 0 },
|
|
85
|
-
knight: { lo: this.knight.lo >>> 0, hi: this.knight.hi >>> 0 },
|
|
86
|
-
bishop: { lo: this.bishop.lo >>> 0, hi: this.bishop.hi >>> 0 },
|
|
87
|
-
rook: { lo: this.rook.lo >>> 0, hi: this.rook.hi >>> 0 },
|
|
88
|
-
queen: { lo: this.queen.lo >>> 0, hi: this.queen.hi >>> 0 },
|
|
89
|
-
king: { lo: this.king.lo >>> 0, hi: this.king.hi >>> 0 },
|
|
90
|
-
occupied: { lo: this.occupied.lo >>> 0, hi: this.occupied.hi >>> 0 },
|
|
91
|
-
promoted: { lo: this.promoted.lo >>> 0, hi: this.promoted.hi >>> 0 },
|
|
92
|
-
kingSq: [this.kingSq[0], this.kingSq[1]],
|
|
93
|
-
},
|
|
94
|
-
turn: this.turn,
|
|
95
|
-
castling: this.castling,
|
|
96
|
-
castlingMask: this.castlingMask,
|
|
97
|
-
isChess960: this.isChess960,
|
|
98
|
-
epSquare: this.epSquare,
|
|
99
|
-
halfmoves: this.halfmoves,
|
|
100
|
-
fullmoves: this.fullmoves,
|
|
101
|
-
halfmove: this.halfmoves,
|
|
102
|
-
fullmove: this.fullmoves,
|
|
103
|
-
kingSq: [this.kingSq[0], this.kingSq[1]],
|
|
104
|
-
checkers: { lo: this.checkers.lo >>> 0, hi: this.checkers.hi >>> 0 },
|
|
105
|
-
zobristLo: this.zobristLo,
|
|
106
|
-
zobristHi: this.zobristHi,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
get halfmove() {
|
|
110
|
-
return this.halfmoves;
|
|
111
|
-
}
|
|
112
|
-
get fullmove() {
|
|
113
|
-
return this.fullmoves;
|
|
114
|
-
}
|
|
115
|
-
get zobristLo() {
|
|
116
|
-
if (this._zobristLo === 0 && this._zobristHi === 0 && zobristTablesLoaded()) {
|
|
117
|
-
this.refreshZobrist();
|
|
118
|
-
}
|
|
119
|
-
return this._zobristLo;
|
|
120
|
-
}
|
|
121
|
-
get zobristHi() {
|
|
122
|
-
if (this._zobristLo === 0 && this._zobristHi === 0 && zobristTablesLoaded()) {
|
|
123
|
-
this.refreshZobrist();
|
|
124
|
-
}
|
|
125
|
-
return this._zobristHi;
|
|
126
|
-
}
|
|
127
|
-
zobrist() {
|
|
128
|
-
return { lo: this.zobristLo, hi: this.zobristHi };
|
|
129
|
-
}
|
|
130
|
-
zobristBigInt() {
|
|
131
|
-
return (BigInt(this.zobristHi >>> 0) << 32n) | BigInt(this.zobristLo >>> 0);
|
|
132
|
-
}
|
|
133
|
-
zobristHex() {
|
|
134
|
-
return zobristHex({ lo: this.zobristLo, hi: this.zobristHi });
|
|
135
|
-
}
|
|
136
|
-
inCheck() {
|
|
137
|
-
return ((this.checkers.lo | this.checkers.hi) !== 0);
|
|
138
|
-
}
|
|
139
|
-
refreshZobrist() {
|
|
140
|
-
if (zobristTablesLoaded()) {
|
|
141
|
-
const zk = calculateZobrist(this);
|
|
142
|
-
this._zobristLo = zk.lo;
|
|
143
|
-
this._zobristHi = zk.hi;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
clear() {
|
|
147
|
-
this.white.lo = 0;
|
|
148
|
-
this.white.hi = 0;
|
|
149
|
-
this.black.lo = 0;
|
|
150
|
-
this.black.hi = 0;
|
|
151
|
-
this.pawn.lo = 0;
|
|
152
|
-
this.pawn.hi = 0;
|
|
153
|
-
this.knight.lo = 0;
|
|
154
|
-
this.knight.hi = 0;
|
|
155
|
-
this.bishop.lo = 0;
|
|
156
|
-
this.bishop.hi = 0;
|
|
157
|
-
this.rook.lo = 0;
|
|
158
|
-
this.rook.hi = 0;
|
|
159
|
-
this.queen.lo = 0;
|
|
160
|
-
this.queen.hi = 0;
|
|
161
|
-
this.king.lo = 0;
|
|
162
|
-
this.king.hi = 0;
|
|
163
|
-
this.occupied.lo = 0;
|
|
164
|
-
this.occupied.hi = 0;
|
|
165
|
-
this.promoted.lo = 0;
|
|
166
|
-
this.promoted.hi = 0;
|
|
167
|
-
this.kingSq[0] = -1;
|
|
168
|
-
this.kingSq[1] = -1;
|
|
169
|
-
this.turn = Color.White;
|
|
170
|
-
this.castling = {
|
|
171
|
-
white: new Set(),
|
|
172
|
-
black: new Set(),
|
|
173
|
-
whiteKing: false,
|
|
174
|
-
whiteQueen: false,
|
|
175
|
-
blackKing: false,
|
|
176
|
-
blackQueen: false,
|
|
177
|
-
};
|
|
178
|
-
this.castlingMask = 0;
|
|
179
|
-
this.epSquare = null;
|
|
180
|
-
this.halfmoves = 0;
|
|
181
|
-
this.fullmoves = 1;
|
|
182
|
-
this.checkers = { lo: 0, hi: 0 };
|
|
183
|
-
this._zobristLo = 0;
|
|
184
|
-
this._zobristHi = 0;
|
|
185
|
-
this.isChess960 = false;
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
loadFen(fen) {
|
|
189
|
-
const res = parseFen(fen);
|
|
190
|
-
if (!res.ok)
|
|
191
|
-
throw new Error(`Invalid FEN: ${fen}`);
|
|
192
|
-
this.copyFrom(res.value);
|
|
193
|
-
}
|
|
194
|
-
copyFrom(src) {
|
|
195
|
-
const b = "board" in src && src.board ? src.board : src;
|
|
196
|
-
this.white.lo = b.white.lo;
|
|
197
|
-
this.white.hi = b.white.hi;
|
|
198
|
-
this.black.lo = b.black.lo;
|
|
199
|
-
this.black.hi = b.black.hi;
|
|
200
|
-
this.pawn.lo = b.pawn.lo;
|
|
201
|
-
this.pawn.hi = b.pawn.hi;
|
|
202
|
-
this.knight.lo = b.knight.lo;
|
|
203
|
-
this.knight.hi = b.knight.hi;
|
|
204
|
-
this.bishop.lo = b.bishop.lo;
|
|
205
|
-
this.bishop.hi = b.bishop.hi;
|
|
206
|
-
this.rook.lo = b.rook.lo;
|
|
207
|
-
this.rook.hi = b.rook.hi;
|
|
208
|
-
this.queen.lo = b.queen.lo;
|
|
209
|
-
this.queen.hi = b.queen.hi;
|
|
210
|
-
this.king.lo = b.king.lo;
|
|
211
|
-
this.king.hi = b.king.hi;
|
|
212
|
-
this.occupied.lo = b.occupied.lo;
|
|
213
|
-
this.occupied.hi = b.occupied.hi;
|
|
214
|
-
this.promoted.lo = b.promoted.lo;
|
|
215
|
-
this.promoted.hi = b.promoted.hi;
|
|
216
|
-
const pos = src;
|
|
217
|
-
this.turn = pos.turn ?? Color.White;
|
|
218
|
-
this.castling = pos.castling ?? {
|
|
219
|
-
white: new Set(),
|
|
220
|
-
black: new Set(),
|
|
221
|
-
whiteKing: false,
|
|
222
|
-
whiteQueen: false,
|
|
223
|
-
blackKing: false,
|
|
224
|
-
blackQueen: false,
|
|
225
|
-
};
|
|
226
|
-
this.castlingMask = pos.castlingMask;
|
|
227
|
-
this.epSquare = pos.epSquare ?? null;
|
|
228
|
-
this.halfmoves = pos.halfmoves ?? pos.halfmove ?? 0;
|
|
229
|
-
this.fullmoves = pos.fullmoves ?? pos.fullmove ?? 1;
|
|
230
|
-
this.isChess960 = !!pos.isChess960;
|
|
231
|
-
if (b.kingSq) {
|
|
232
|
-
this.kingSq[0] = b.kingSq[0];
|
|
233
|
-
this.kingSq[1] = b.kingSq[1];
|
|
234
|
-
}
|
|
235
|
-
else if (pos.kingSq) {
|
|
236
|
-
this.kingSq[0] = pos.kingSq[0];
|
|
237
|
-
this.kingSq[1] = pos.kingSq[1];
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
this.kingSq[0] = kingSquare(this, Color.White) ?? -1;
|
|
241
|
-
this.kingSq[1] = kingSquare(this, Color.Black) ?? -1;
|
|
242
|
-
}
|
|
243
|
-
if (pos.checkers) {
|
|
244
|
-
this.checkers = { lo: pos.checkers.lo, hi: pos.checkers.hi };
|
|
245
|
-
}
|
|
246
|
-
else {
|
|
247
|
-
this.checkers = kingAttackers(this, this.turn);
|
|
248
|
-
}
|
|
249
|
-
this._zobristLo = pos.zobristLo ?? 0;
|
|
250
|
-
this._zobristHi = pos.zobristHi ?? 0;
|
|
251
|
-
if (this._zobristLo === 0 && this._zobristHi === 0 && zobristTablesLoaded()) {
|
|
252
|
-
this.refreshZobrist();
|
|
253
|
-
}
|
|
254
|
-
return this;
|
|
255
|
-
}
|
|
256
|
-
clone() {
|
|
257
|
-
const nb = Board.empty();
|
|
258
|
-
nb.copyFrom(this);
|
|
259
|
-
return nb;
|
|
260
|
-
}
|
|
261
|
-
roleAt(sqIdx) {
|
|
262
|
-
const bit = sqIdx < 32 ? (1 << sqIdx) >>> 0 : (1 << (sqIdx - 32)) >>> 0;
|
|
263
|
-
const occ = sqIdx < 32 ? this.occupied.lo : this.occupied.hi;
|
|
264
|
-
if (((occ & bit) >>> 0) === 0)
|
|
265
|
-
return undefined;
|
|
266
|
-
const pawnW = sqIdx < 32 ? this.pawn.lo : this.pawn.hi;
|
|
267
|
-
if (((pawnW & bit) >>> 0) !== 0)
|
|
268
|
-
return Role.Pawn;
|
|
269
|
-
const knightW = sqIdx < 32 ? this.knight.lo : this.knight.hi;
|
|
270
|
-
if (((knightW & bit) >>> 0) !== 0)
|
|
271
|
-
return Role.Knight;
|
|
272
|
-
const bishopW = sqIdx < 32 ? this.bishop.lo : this.bishop.hi;
|
|
273
|
-
if (((bishopW & bit) >>> 0) !== 0)
|
|
274
|
-
return Role.Bishop;
|
|
275
|
-
const rookW = sqIdx < 32 ? this.rook.lo : this.rook.hi;
|
|
276
|
-
if (((rookW & bit) >>> 0) !== 0)
|
|
277
|
-
return Role.Rook;
|
|
278
|
-
const queenW = sqIdx < 32 ? this.queen.lo : this.queen.hi;
|
|
279
|
-
if (((queenW & bit) >>> 0) !== 0)
|
|
280
|
-
return Role.Queen;
|
|
281
|
-
return Role.King;
|
|
282
|
-
}
|
|
283
|
-
colorAt(sqIdx) {
|
|
284
|
-
const bit = sqIdx < 32 ? (1 << sqIdx) >>> 0 : (1 << (sqIdx - 32)) >>> 0;
|
|
285
|
-
const occ = sqIdx < 32 ? this.occupied.lo : this.occupied.hi;
|
|
286
|
-
if (((occ & bit) >>> 0) === 0)
|
|
287
|
-
return undefined;
|
|
288
|
-
const whiteW = sqIdx < 32 ? this.white.lo : this.white.hi;
|
|
289
|
-
return ((whiteW & bit) >>> 0) !== 0 ? Color.White : Color.Black;
|
|
290
|
-
}
|
|
291
|
-
pieceAt(sqIdx) {
|
|
292
|
-
const role = this.roleAt(sqIdx);
|
|
293
|
-
if (role === undefined)
|
|
294
|
-
return undefined;
|
|
295
|
-
const color = this.colorAt(sqIdx);
|
|
296
|
-
return { color, role };
|
|
297
|
-
}
|
|
298
|
-
hasPiece(sqIdx) {
|
|
299
|
-
const bit = sqIdx < 32 ? (1 << sqIdx) >>> 0 : (1 << (sqIdx - 32)) >>> 0;
|
|
300
|
-
const occ = sqIdx < 32 ? this.occupied.lo : this.occupied.hi;
|
|
301
|
-
return ((occ & bit) >>> 0) !== 0;
|
|
302
|
-
}
|
|
303
|
-
kingSquare(color) {
|
|
304
|
-
const k = this.kingSq[color];
|
|
305
|
-
if (k >= 0)
|
|
306
|
-
return k;
|
|
307
|
-
const ks = color === Color.White ? sq.and(this.white, this.king) : sq.and(this.black, this.king);
|
|
308
|
-
return sq.first(ks);
|
|
309
|
-
}
|
|
310
|
-
_clearSquare(sqIdx) {
|
|
311
|
-
if (sqIdx < 32) {
|
|
312
|
-
const inv = ~(1 << sqIdx);
|
|
313
|
-
this.white.lo &= inv;
|
|
314
|
-
this.black.lo &= inv;
|
|
315
|
-
this.pawn.lo &= inv;
|
|
316
|
-
this.knight.lo &= inv;
|
|
317
|
-
this.bishop.lo &= inv;
|
|
318
|
-
this.rook.lo &= inv;
|
|
319
|
-
this.queen.lo &= inv;
|
|
320
|
-
this.king.lo &= inv;
|
|
321
|
-
this.occupied.lo &= inv;
|
|
322
|
-
this.promoted.lo &= inv;
|
|
323
|
-
}
|
|
324
|
-
else {
|
|
325
|
-
const inv = ~(1 << (sqIdx - 32));
|
|
326
|
-
this.white.hi &= inv;
|
|
327
|
-
this.black.hi &= inv;
|
|
328
|
-
this.pawn.hi &= inv;
|
|
329
|
-
this.knight.hi &= inv;
|
|
330
|
-
this.bishop.hi &= inv;
|
|
331
|
-
this.rook.hi &= inv;
|
|
332
|
-
this.queen.hi &= inv;
|
|
333
|
-
this.king.hi &= inv;
|
|
334
|
-
this.occupied.hi &= inv;
|
|
335
|
-
this.promoted.hi &= inv;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
_putPiece(sqIdx, color, role) {
|
|
339
|
-
if (sqIdx < 32) {
|
|
340
|
-
const bit = (1 << sqIdx) >>> 0;
|
|
341
|
-
if (color === Color.White)
|
|
342
|
-
this.white.lo |= bit;
|
|
343
|
-
else
|
|
344
|
-
this.black.lo |= bit;
|
|
345
|
-
switch (role) {
|
|
346
|
-
case Role.Pawn:
|
|
347
|
-
this.pawn.lo |= bit;
|
|
348
|
-
break;
|
|
349
|
-
case Role.Knight:
|
|
350
|
-
this.knight.lo |= bit;
|
|
351
|
-
break;
|
|
352
|
-
case Role.Bishop:
|
|
353
|
-
this.bishop.lo |= bit;
|
|
354
|
-
break;
|
|
355
|
-
case Role.Rook:
|
|
356
|
-
this.rook.lo |= bit;
|
|
357
|
-
break;
|
|
358
|
-
case Role.Queen:
|
|
359
|
-
this.queen.lo |= bit;
|
|
360
|
-
break;
|
|
361
|
-
case Role.King:
|
|
362
|
-
this.king.lo |= bit;
|
|
363
|
-
break;
|
|
364
|
-
}
|
|
365
|
-
this.occupied.lo |= bit;
|
|
366
|
-
}
|
|
367
|
-
else {
|
|
368
|
-
const bit = (1 << (sqIdx - 32)) >>> 0;
|
|
369
|
-
if (color === Color.White)
|
|
370
|
-
this.white.hi |= bit;
|
|
371
|
-
else
|
|
372
|
-
this.black.hi |= bit;
|
|
373
|
-
switch (role) {
|
|
374
|
-
case Role.Pawn:
|
|
375
|
-
this.pawn.hi |= bit;
|
|
376
|
-
break;
|
|
377
|
-
case Role.Knight:
|
|
378
|
-
this.knight.hi |= bit;
|
|
379
|
-
break;
|
|
380
|
-
case Role.Bishop:
|
|
381
|
-
this.bishop.hi |= bit;
|
|
382
|
-
break;
|
|
383
|
-
case Role.Rook:
|
|
384
|
-
this.rook.hi |= bit;
|
|
385
|
-
break;
|
|
386
|
-
case Role.Queen:
|
|
387
|
-
this.queen.hi |= bit;
|
|
388
|
-
break;
|
|
389
|
-
case Role.King:
|
|
390
|
-
this.king.hi |= bit;
|
|
391
|
-
break;
|
|
392
|
-
}
|
|
393
|
-
this.occupied.hi |= bit;
|
|
394
|
-
}
|
|
395
|
-
if (role === Role.King) {
|
|
396
|
-
this.kingSq[color] = sqIdx;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Plays a null move (pass): the position stays, the side to move flips.
|
|
401
|
-
*
|
|
402
|
-
* Castling rights are untouched, the en-passant square clears, the halfmove
|
|
403
|
-
* clock advances and the move always completes a full move (the number
|
|
404
|
-
* advances whoever passed). The Polyglot key and the cached `checkers` are
|
|
405
|
-
* both maintained.
|
|
406
|
-
*
|
|
407
|
-
* Throws when the side to move is in check: a pass answers no check. The test
|
|
408
|
-
* is computed from the bitboards (never from the cached `checkers`, which a
|
|
409
|
-
* cache-free make may have left stale). Mirrors gigachess-rs
|
|
410
|
-
* `Board::make_null_move`.
|
|
411
|
-
*/
|
|
412
|
-
makeNullMove() {
|
|
413
|
-
const us = this.turn;
|
|
414
|
-
const them = opposite(us);
|
|
415
|
-
// Fresh in-check scan — the cache may describe an earlier position.
|
|
416
|
-
const ksq = this.kingSq[us === Color.White ? 0 : 1];
|
|
417
|
-
if (ksq >= 0 && !sq.isEmpty(attackersTo(this, ksq, them, this.occupied))) {
|
|
418
|
-
throw new Error("illegal null move: the side to move is in check");
|
|
419
|
-
}
|
|
420
|
-
const undo = {
|
|
421
|
-
isNull: true,
|
|
422
|
-
epSquare: this.epSquare,
|
|
423
|
-
castling: this.castling,
|
|
424
|
-
castlingMask: this.castlingMask,
|
|
425
|
-
halfmoves: this.halfmoves,
|
|
426
|
-
fullmoves: this.fullmoves,
|
|
427
|
-
kingSq: [this.kingSq[0], this.kingSq[1]],
|
|
428
|
-
checkers: this.checkers,
|
|
429
|
-
zobristLo: this._zobristLo,
|
|
430
|
-
zobristHi: this._zobristHi,
|
|
431
|
-
};
|
|
432
|
-
// Hash out the old en-passant contribution (relevance uses the mover's
|
|
433
|
-
// pawns, which a pass does not move) before clearing it.
|
|
434
|
-
if (zobristTablesLoaded() && keyLo && keyHi) {
|
|
435
|
-
if (this._zobristLo === 0 && this._zobristHi === 0)
|
|
436
|
-
this.refreshZobrist();
|
|
437
|
-
if (this.epSquare !== null && epIsHashable(this, this.epSquare, us)) {
|
|
438
|
-
const kEp = IDX_EP + (this.epSquare & 7);
|
|
439
|
-
this._zobristLo = (this._zobristLo ^ keyLo[kEp]) >>> 0;
|
|
440
|
-
this._zobristHi = (this._zobristHi ^ keyHi[kEp]) >>> 0;
|
|
441
|
-
}
|
|
442
|
-
// a pass is a full move — the side key always flips
|
|
443
|
-
this._zobristLo = (this._zobristLo ^ keyLo[IDX_SIDE]) >>> 0;
|
|
444
|
-
this._zobristHi = (this._zobristHi ^ keyHi[IDX_SIDE]) >>> 0;
|
|
445
|
-
}
|
|
446
|
-
this.epSquare = null;
|
|
447
|
-
this.halfmoves = this.halfmoves + 1;
|
|
448
|
-
this.fullmoves = this.fullmoves + 1;
|
|
449
|
-
this.turn = them;
|
|
450
|
-
this.checkers = kingAttackers(this, them);
|
|
451
|
-
return undo;
|
|
452
|
-
}
|
|
453
|
-
/**
|
|
454
|
-
* Reverts the most recent null move, restoring the exact prior position.
|
|
455
|
-
*
|
|
456
|
-
* Each null must be unmade before an older one, with no ordinary moves
|
|
457
|
-
* interleaved past the matching make — exactly like `unmakeMove`.
|
|
458
|
-
*/
|
|
459
|
-
unmakeNullMove(undo) {
|
|
460
|
-
this.turn = opposite(this.turn);
|
|
461
|
-
this.fullmoves = undo.fullmoves;
|
|
462
|
-
this.epSquare = undo.epSquare;
|
|
463
|
-
this.castling = undo.castling;
|
|
464
|
-
this.castlingMask = undo.castlingMask;
|
|
465
|
-
this.halfmoves = undo.halfmoves;
|
|
466
|
-
this.kingSq[0] = undo.kingSq[0];
|
|
467
|
-
this.kingSq[1] = undo.kingSq[1];
|
|
468
|
-
this.checkers = undo.checkers;
|
|
469
|
-
this._zobristLo = undo.zobristLo;
|
|
470
|
-
this._zobristHi = undo.zobristHi;
|
|
471
|
-
}
|
|
472
|
-
makeMove(moveWord) {
|
|
473
|
-
const word = typeof moveWord === "number" ? moveWord : packOf(moveWord);
|
|
474
|
-
// The null sentinel is a first-class pass, not a from/to/promo word.
|
|
475
|
-
if (isNull(word))
|
|
476
|
-
return this.makeNullMove();
|
|
477
|
-
const from = word & 0x3f;
|
|
478
|
-
const to = (word >> 6) & 0x3f;
|
|
479
|
-
const promoCode = (word >> 12) & 0x0f;
|
|
480
|
-
const promoRole = promoCodeToRole(promoCode) ?? null;
|
|
481
|
-
const movingRole = this.roleAt(from);
|
|
482
|
-
if (movingRole === undefined) {
|
|
483
|
-
throw new Error(`no piece at from square ${from}`);
|
|
484
|
-
}
|
|
485
|
-
const us = this.turn;
|
|
486
|
-
const them = opposite(us);
|
|
487
|
-
const prevCheckers = this.checkers;
|
|
488
|
-
const prevZobristLo = this._zobristLo;
|
|
489
|
-
const prevZobristHi = this._zobristHi;
|
|
490
|
-
const prevCastling = this.castling;
|
|
491
|
-
const prevCastlingMask = this.castlingMask;
|
|
492
|
-
const prevEpSquare = this.epSquare;
|
|
493
|
-
const prevHalfmoves = this.halfmoves;
|
|
494
|
-
const prevFullmoves = this.fullmoves;
|
|
495
|
-
const prevKingSq = [this.kingSq[0], this.kingSq[1]];
|
|
496
|
-
const prevEpHashable = prevEpSquare !== null && zobristTablesLoaded() && epIsHashable(this, prevEpSquare, us);
|
|
497
|
-
let plan = null;
|
|
498
|
-
if (movingRole === Role.King) {
|
|
499
|
-
plan = detectCastling(this, from, to);
|
|
500
|
-
}
|
|
501
|
-
const isCastling = plan !== null;
|
|
502
|
-
let isEnPassant = false;
|
|
503
|
-
if (movingRole === Role.Pawn &&
|
|
504
|
-
this.epSquare !== null &&
|
|
505
|
-
to === this.epSquare &&
|
|
506
|
-
(from & 7) !== (to & 7)) {
|
|
507
|
-
isEnPassant = true;
|
|
508
|
-
}
|
|
509
|
-
let capturedRole = -1;
|
|
510
|
-
let capturedSq = -1;
|
|
511
|
-
if (isEnPassant) {
|
|
512
|
-
capturedRole = Role.Pawn;
|
|
513
|
-
capturedSq = us === Color.White ? to - 8 : to + 8;
|
|
514
|
-
}
|
|
515
|
-
else if (!isCastling) {
|
|
516
|
-
const capRole = this.roleAt(to);
|
|
517
|
-
if (capRole !== undefined) {
|
|
518
|
-
capturedRole = capRole;
|
|
519
|
-
capturedSq = to;
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
this._clearSquare(from);
|
|
523
|
-
if (capturedSq >= 0) {
|
|
524
|
-
this._clearSquare(capturedSq);
|
|
525
|
-
}
|
|
526
|
-
if (isCastling && plan !== null) {
|
|
527
|
-
this._clearSquare(plan.rookFrom);
|
|
528
|
-
this._putPiece(plan.rookTo, us, Role.Rook);
|
|
529
|
-
this._putPiece(plan.kingTo, us, Role.King);
|
|
530
|
-
this.kingSq[us] = plan.kingTo;
|
|
531
|
-
}
|
|
532
|
-
else {
|
|
533
|
-
const finalRole = promoRole ?? movingRole;
|
|
534
|
-
this._putPiece(to, us, finalRole);
|
|
535
|
-
if (movingRole === Role.King) {
|
|
536
|
-
this.kingSq[us] = to;
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
let newCastling;
|
|
540
|
-
let newMask;
|
|
541
|
-
if (!this.isChess960) {
|
|
542
|
-
const mask = this.castlingMask ?? getCastlingMask(this);
|
|
543
|
-
newMask = (mask & CASTLE_CLEAR_STD[from] & CASTLE_CLEAR_STD[to]) >>> 0;
|
|
544
|
-
newCastling = CASTLING_RIGHTS_TABLE[newMask];
|
|
545
|
-
}
|
|
546
|
-
else {
|
|
547
|
-
let newWhite = this.castling.white;
|
|
548
|
-
let newBlack = this.castling.black;
|
|
549
|
-
if (movingRole === Role.King) {
|
|
550
|
-
if (us === Color.White) {
|
|
551
|
-
if (newWhite.size > 0)
|
|
552
|
-
newWhite = new Set();
|
|
553
|
-
}
|
|
554
|
-
else {
|
|
555
|
-
if (newBlack.size > 0)
|
|
556
|
-
newBlack = new Set();
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
if (movingRole === Role.Rook) {
|
|
560
|
-
if (us === Color.White && newWhite.has(from)) {
|
|
561
|
-
const next = new Set(newWhite);
|
|
562
|
-
next.delete(from);
|
|
563
|
-
newWhite = next;
|
|
564
|
-
}
|
|
565
|
-
else if (us === Color.Black && newBlack.has(from)) {
|
|
566
|
-
const next = new Set(newBlack);
|
|
567
|
-
next.delete(from);
|
|
568
|
-
newBlack = next;
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
if (capturedRole === Role.Rook && capturedSq >= 0) {
|
|
572
|
-
if (them === Color.White && newWhite.has(capturedSq)) {
|
|
573
|
-
const next = new Set(newWhite);
|
|
574
|
-
next.delete(capturedSq);
|
|
575
|
-
newWhite = next;
|
|
576
|
-
}
|
|
577
|
-
else if (them === Color.Black && newBlack.has(capturedSq)) {
|
|
578
|
-
const next = new Set(newBlack);
|
|
579
|
-
next.delete(capturedSq);
|
|
580
|
-
newBlack = next;
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
newCastling = {
|
|
584
|
-
white: newWhite,
|
|
585
|
-
black: newBlack,
|
|
586
|
-
whiteKing: newWhite.has(7),
|
|
587
|
-
whiteQueen: newWhite.has(0),
|
|
588
|
-
blackKing: newBlack.has(63),
|
|
589
|
-
blackQueen: newBlack.has(56),
|
|
590
|
-
};
|
|
591
|
-
}
|
|
592
|
-
this.castling = newCastling;
|
|
593
|
-
this.castlingMask = newMask;
|
|
594
|
-
let newEp = null;
|
|
595
|
-
if (movingRole === Role.Pawn && Math.abs((to >> 3) - (from >> 3)) === 2) {
|
|
596
|
-
const epRank = us === Color.White ? (from >> 3) + 1 : (from >> 3) - 1;
|
|
597
|
-
newEp = (epRank << 3) | (from & 7);
|
|
598
|
-
}
|
|
599
|
-
this.epSquare = newEp;
|
|
600
|
-
const isCapture = capturedSq >= 0;
|
|
601
|
-
const isPawnMove = movingRole === Role.Pawn;
|
|
602
|
-
this.halfmoves = isCapture || isPawnMove ? 0 : prevHalfmoves + 1;
|
|
603
|
-
this.fullmoves = us === Color.Black ? prevFullmoves + 1 : prevFullmoves;
|
|
604
|
-
this.turn = them;
|
|
605
|
-
this.checkers = kingAttackers(this, them);
|
|
606
|
-
if (zobristTablesLoaded() && keyLo && keyHi && castleKeyLo && castleKeyHi) {
|
|
607
|
-
if (this._zobristLo === 0 && this._zobristHi === 0) {
|
|
608
|
-
this.refreshZobrist();
|
|
609
|
-
}
|
|
610
|
-
let zLo = this._zobristLo;
|
|
611
|
-
let zHi = this._zobristHi;
|
|
612
|
-
// 1. Moving piece leaves from
|
|
613
|
-
const kFrom = pieceKeyIdx(us, movingRole, from);
|
|
614
|
-
zLo = (zLo ^ keyLo[kFrom]) >>> 0;
|
|
615
|
-
zHi = (zHi ^ keyHi[kFrom]) >>> 0;
|
|
616
|
-
// 2. Moving piece lands on to (or castling / promo)
|
|
617
|
-
if (plan !== null) {
|
|
618
|
-
const kRookFrom = pieceKeyIdx(us, Role.Rook, plan.rookFrom);
|
|
619
|
-
const kRookTo = pieceKeyIdx(us, Role.Rook, plan.rookTo);
|
|
620
|
-
const kKingTo = pieceKeyIdx(us, Role.King, plan.kingTo);
|
|
621
|
-
zLo = (zLo ^ keyLo[kRookFrom] ^ keyLo[kRookTo] ^ keyLo[kKingTo]) >>> 0;
|
|
622
|
-
zHi = (zHi ^ keyHi[kRookFrom] ^ keyHi[kRookTo] ^ keyHi[kKingTo]) >>> 0;
|
|
623
|
-
}
|
|
624
|
-
else if (promoRole !== null) {
|
|
625
|
-
const kPromo = pieceKeyIdx(us, promoRole, to);
|
|
626
|
-
zLo = (zLo ^ keyLo[kPromo]) >>> 0;
|
|
627
|
-
zHi = (zHi ^ keyHi[kPromo]) >>> 0;
|
|
628
|
-
}
|
|
629
|
-
else {
|
|
630
|
-
const kTo = pieceKeyIdx(us, movingRole, to);
|
|
631
|
-
zLo = (zLo ^ keyLo[kTo]) >>> 0;
|
|
632
|
-
zHi = (zHi ^ keyHi[kTo]) >>> 0;
|
|
633
|
-
}
|
|
634
|
-
// 3. Captured piece leaves board
|
|
635
|
-
if (isEnPassant) {
|
|
636
|
-
const capSq = us === Color.White ? to - 8 : to + 8;
|
|
637
|
-
const kCap = pieceKeyIdx(them, Role.Pawn, capSq);
|
|
638
|
-
zLo = (zLo ^ keyLo[kCap]) >>> 0;
|
|
639
|
-
zHi = (zHi ^ keyHi[kCap]) >>> 0;
|
|
640
|
-
}
|
|
641
|
-
else if (capturedRole !== -1) {
|
|
642
|
-
const kCap = pieceKeyIdx(them, capturedRole, to);
|
|
643
|
-
zLo = (zLo ^ keyLo[kCap]) >>> 0;
|
|
644
|
-
zHi = (zHi ^ keyHi[kCap]) >>> 0;
|
|
645
|
-
}
|
|
646
|
-
// 4. Castling rights diff
|
|
647
|
-
if (!this.isChess960) {
|
|
648
|
-
const diff = (prevCastlingMask ?? 0) ^ (newMask ?? 0);
|
|
649
|
-
if (diff !== 0) {
|
|
650
|
-
if (diff & 1) {
|
|
651
|
-
zLo = (zLo ^ castleKeyLo[15]) >>> 0;
|
|
652
|
-
zHi = (zHi ^ castleKeyHi[15]) >>> 0;
|
|
653
|
-
}
|
|
654
|
-
if (diff & 2) {
|
|
655
|
-
zLo = (zLo ^ castleKeyLo[8]) >>> 0;
|
|
656
|
-
zHi = (zHi ^ castleKeyHi[8]) >>> 0;
|
|
657
|
-
}
|
|
658
|
-
if (diff & 4) {
|
|
659
|
-
zLo = (zLo ^ castleKeyLo[7]) >>> 0;
|
|
660
|
-
zHi = (zHi ^ castleKeyHi[7]) >>> 0;
|
|
661
|
-
}
|
|
662
|
-
if (diff & 8) {
|
|
663
|
-
zLo = (zLo ^ castleKeyLo[0]) >>> 0;
|
|
664
|
-
zHi = (zHi ^ castleKeyHi[0]) >>> 0;
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
else {
|
|
669
|
-
for (const rs of prevCastling.white) {
|
|
670
|
-
const idx = castlingKeyIdx(Color.White, rs);
|
|
671
|
-
zLo = (zLo ^ castleKeyLo[idx]) >>> 0;
|
|
672
|
-
zHi = (zHi ^ castleKeyHi[idx]) >>> 0;
|
|
673
|
-
}
|
|
674
|
-
for (const rs of prevCastling.black) {
|
|
675
|
-
const idx = castlingKeyIdx(Color.Black, rs);
|
|
676
|
-
zLo = (zLo ^ castleKeyLo[idx]) >>> 0;
|
|
677
|
-
zHi = (zHi ^ castleKeyHi[idx]) >>> 0;
|
|
678
|
-
}
|
|
679
|
-
for (const rs of newCastling.white) {
|
|
680
|
-
const idx = castlingKeyIdx(Color.White, rs);
|
|
681
|
-
zLo = (zLo ^ castleKeyLo[idx]) >>> 0;
|
|
682
|
-
zHi = (zHi ^ castleKeyHi[idx]) >>> 0;
|
|
683
|
-
}
|
|
684
|
-
for (const rs of newCastling.black) {
|
|
685
|
-
const idx = castlingKeyIdx(Color.Black, rs);
|
|
686
|
-
zLo = (zLo ^ castleKeyLo[idx]) >>> 0;
|
|
687
|
-
zHi = (zHi ^ castleKeyHi[idx]) >>> 0;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
// 5. En passant square diff
|
|
691
|
-
if (prevEpHashable) {
|
|
692
|
-
const kEp = IDX_EP + (prevEpSquare & 7);
|
|
693
|
-
zLo = (zLo ^ keyLo[kEp]) >>> 0;
|
|
694
|
-
zHi = (zHi ^ keyHi[kEp]) >>> 0;
|
|
695
|
-
}
|
|
696
|
-
if (newEp !== null && epIsHashable(this, newEp, them)) {
|
|
697
|
-
const kEp = IDX_EP + (newEp & 7);
|
|
698
|
-
zLo = (zLo ^ keyLo[kEp]) >>> 0;
|
|
699
|
-
zHi = (zHi ^ keyHi[kEp]) >>> 0;
|
|
700
|
-
}
|
|
701
|
-
// 6. Turn flip
|
|
702
|
-
zLo = (zLo ^ keyLo[IDX_SIDE]) >>> 0;
|
|
703
|
-
zHi = (zHi ^ keyHi[IDX_SIDE]) >>> 0;
|
|
704
|
-
this._zobristLo = zLo;
|
|
705
|
-
this._zobristHi = zHi;
|
|
706
|
-
}
|
|
707
|
-
return {
|
|
708
|
-
move: word,
|
|
709
|
-
movingRole,
|
|
710
|
-
capturedRole,
|
|
711
|
-
capturedSq,
|
|
712
|
-
castlingPlan: plan,
|
|
713
|
-
isEnPassant,
|
|
714
|
-
promoRole,
|
|
715
|
-
epSquare: prevEpSquare,
|
|
716
|
-
castling: prevCastling,
|
|
717
|
-
castlingMask: prevCastlingMask,
|
|
718
|
-
halfmoves: prevHalfmoves,
|
|
719
|
-
fullmoves: prevFullmoves,
|
|
720
|
-
kingSq: prevKingSq,
|
|
721
|
-
checkers: prevCheckers,
|
|
722
|
-
zobristLo: prevZobristLo,
|
|
723
|
-
zobristHi: prevZobristHi,
|
|
724
|
-
};
|
|
725
|
-
}
|
|
726
|
-
unmakeMove(undo) {
|
|
727
|
-
// A pass records no piece edits — restore clocks/turn/caches only.
|
|
728
|
-
if (undo.isNull === true) {
|
|
729
|
-
this.unmakeNullMove(undo);
|
|
730
|
-
return;
|
|
731
|
-
}
|
|
732
|
-
const normal = undo;
|
|
733
|
-
const from = normal.move & 0x3f;
|
|
734
|
-
const to = (normal.move >> 6) & 0x3f;
|
|
735
|
-
const us = opposite(this.turn);
|
|
736
|
-
this.turn = us;
|
|
737
|
-
this.halfmoves = normal.halfmoves;
|
|
738
|
-
this.fullmoves = normal.fullmoves;
|
|
739
|
-
this.epSquare = normal.epSquare;
|
|
740
|
-
this.castling = normal.castling;
|
|
741
|
-
this.castlingMask = normal.castlingMask;
|
|
742
|
-
this.kingSq[0] = normal.kingSq[0];
|
|
743
|
-
this.kingSq[1] = normal.kingSq[1];
|
|
744
|
-
this.checkers = normal.checkers;
|
|
745
|
-
this._zobristLo = normal.zobristLo;
|
|
746
|
-
this._zobristHi = normal.zobristHi;
|
|
747
|
-
if (normal.castlingPlan !== null) {
|
|
748
|
-
const plan = normal.castlingPlan;
|
|
749
|
-
this._clearSquare(plan.kingTo);
|
|
750
|
-
this._clearSquare(plan.rookTo);
|
|
751
|
-
this._putPiece(plan.kingFrom, us, Role.King);
|
|
752
|
-
this._putPiece(plan.rookFrom, us, Role.Rook);
|
|
753
|
-
}
|
|
754
|
-
else {
|
|
755
|
-
this._clearSquare(to);
|
|
756
|
-
this._putPiece(from, us, normal.movingRole);
|
|
757
|
-
if (normal.capturedSq >= 0 && normal.capturedRole >= 0) {
|
|
758
|
-
const them = opposite(us);
|
|
759
|
-
this._putPiece(normal.capturedSq, them, normal.capturedRole);
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
legalMoves(outBuffer) {
|
|
764
|
-
const buf = outBuffer ?? new Uint16Array(256);
|
|
765
|
-
const count = legalMovesInto(this, buf);
|
|
766
|
-
return buf.subarray(0, count);
|
|
767
|
-
}
|
|
768
|
-
forEachLegalMove(fn) {
|
|
769
|
-
forEachLegalMove(this, (from, to, promo) => {
|
|
770
|
-
fn(((from & 0x3f) | ((to & 0x3f) << 6) | ((promo & 0x0f) << 12)) >>> 0);
|
|
771
|
-
});
|
|
772
|
-
}
|
|
773
|
-
toSan(moveWord) {
|
|
774
|
-
const move = unpackToMove(moveWord);
|
|
775
|
-
return makeSan(move, this);
|
|
776
|
-
}
|
|
777
|
-
toUci(moveWord) {
|
|
778
|
-
const move = unpackToMove(moveWord);
|
|
779
|
-
return makeUci(move);
|
|
780
|
-
}
|
|
781
|
-
toFen() {
|
|
782
|
-
return makeFen(this);
|
|
783
|
-
}
|
|
784
|
-
parseSan(san) {
|
|
785
|
-
const res = parseSan(san, this);
|
|
786
|
-
return res.ok ? packOf(res.value) : null;
|
|
787
|
-
}
|
|
788
|
-
/**
|
|
789
|
-
* `parseSan` with the failure reason kept.
|
|
790
|
-
*
|
|
791
|
-
* `parseSan` maps every error — `san/ambiguous`, `san/noLegalMove`,
|
|
792
|
-
* `san/invalidSan` — onto the same `null`, so a caller cannot tell "no such
|
|
793
|
-
* move" from "several moves answer to this and you must disambiguate". Those
|
|
794
|
-
* are different answers for a UI: one is an error, the other needs the
|
|
795
|
-
* candidates named (BlindBase ADR-042, field report #11).
|
|
796
|
-
*
|
|
797
|
-
* The error code is returned rather than a richer object because the codes
|
|
798
|
-
* are the library's own error vocabulary (`SanError["code"]`), already
|
|
799
|
-
* documented and already i18n-keyed upstream.
|
|
800
|
-
*/
|
|
801
|
-
parseSanResult(san) {
|
|
802
|
-
const res = parseSan(san, this);
|
|
803
|
-
return res.ok
|
|
804
|
-
? { ok: true, moveWord: packOf(res.value) }
|
|
805
|
-
: { ok: false, code: res.error.code };
|
|
806
|
-
}
|
|
807
|
-
parseUci(uci) {
|
|
808
|
-
const res = parseUci(uci);
|
|
809
|
-
return res.ok && isLegal(this, res.value) ? packOf(res.value) : null;
|
|
810
|
-
}
|
|
811
|
-
/**
|
|
812
|
-
* Every packed move word `san` could denote here, or the reason it names none.
|
|
813
|
-
*
|
|
814
|
-
* `parseSan` reports `san/ambiguous` and discards the matches, so a caller
|
|
815
|
-
* can tell the user the input was ambiguous but not which moves it meant.
|
|
816
|
-
* This returns them, letting a caller present the disambiguated spellings
|
|
817
|
-
* rather than making the user guess (BlindBase ADR-042, field report #11).
|
|
818
|
-
*
|
|
819
|
-
* A resolved move comes back as a one-element array, so this can replace
|
|
820
|
-
* `parseSan` wherever the caller only needs "one move, or a reason".
|
|
821
|
-
*/
|
|
822
|
-
parseSanCandidates(san) {
|
|
823
|
-
const res = parseSanCandidates(san, this);
|
|
824
|
-
return res.ok
|
|
825
|
-
? { ok: true, moveWords: res.value.map(packOf) }
|
|
826
|
-
: { ok: false, code: res.error.code };
|
|
827
|
-
}
|
|
828
|
-
perft(depth) {
|
|
829
|
-
if (depth <= 0)
|
|
830
|
-
return 1;
|
|
831
|
-
if (depth === 1)
|
|
832
|
-
return countLegalMoves(this);
|
|
833
|
-
const buf = new Uint16Array(256);
|
|
834
|
-
const count = legalMovesInto(this, buf);
|
|
835
|
-
let nodes = 0;
|
|
836
|
-
for (let i = 0; i < count; i++) {
|
|
837
|
-
const undo = this.makeMove(buf[i]);
|
|
838
|
-
nodes += this.perft(depth - 1);
|
|
839
|
-
this.unmakeMove(undo);
|
|
840
|
-
}
|
|
841
|
-
return nodes;
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
export function emptyBoard() {
|
|
845
|
-
const e = sq.empty();
|
|
846
|
-
return {
|
|
847
|
-
white: e,
|
|
848
|
-
black: e,
|
|
849
|
-
pawn: e,
|
|
850
|
-
knight: e,
|
|
851
|
-
bishop: e,
|
|
852
|
-
rook: e,
|
|
853
|
-
queen: e,
|
|
854
|
-
king: e,
|
|
855
|
-
occupied: e,
|
|
856
|
-
promoted: e,
|
|
857
|
-
kingSq: [-1, -1],
|
|
858
|
-
};
|
|
859
|
-
}
|
|
860
|
-
export function cloneBoard(board) {
|
|
861
|
-
return {
|
|
862
|
-
white: { lo: board.white.lo >>> 0, hi: board.white.hi >>> 0 },
|
|
863
|
-
black: { lo: board.black.lo >>> 0, hi: board.black.hi >>> 0 },
|
|
864
|
-
pawn: { lo: board.pawn.lo >>> 0, hi: board.pawn.hi >>> 0 },
|
|
865
|
-
knight: { lo: board.knight.lo >>> 0, hi: board.knight.hi >>> 0 },
|
|
866
|
-
bishop: { lo: board.bishop.lo >>> 0, hi: board.bishop.hi >>> 0 },
|
|
867
|
-
rook: { lo: board.rook.lo >>> 0, hi: board.rook.hi >>> 0 },
|
|
868
|
-
queen: { lo: board.queen.lo >>> 0, hi: board.queen.hi >>> 0 },
|
|
869
|
-
king: { lo: board.king.lo >>> 0, hi: board.king.hi >>> 0 },
|
|
870
|
-
occupied: { lo: board.occupied.lo >>> 0, hi: board.occupied.hi >>> 0 },
|
|
871
|
-
promoted: { lo: board.promoted.lo >>> 0, hi: board.promoted.hi >>> 0 },
|
|
872
|
-
kingSq: board.kingSq ? [board.kingSq[0], board.kingSq[1]] : [-1, -1],
|
|
873
|
-
};
|
|
874
|
-
}
|
|
875
|
-
export function newScratchBoard() {
|
|
876
|
-
return {
|
|
877
|
-
white: { lo: 0, hi: 0 },
|
|
878
|
-
black: { lo: 0, hi: 0 },
|
|
879
|
-
pawn: { lo: 0, hi: 0 },
|
|
880
|
-
knight: { lo: 0, hi: 0 },
|
|
881
|
-
bishop: { lo: 0, hi: 0 },
|
|
882
|
-
rook: { lo: 0, hi: 0 },
|
|
883
|
-
queen: { lo: 0, hi: 0 },
|
|
884
|
-
king: { lo: 0, hi: 0 },
|
|
885
|
-
occupied: { lo: 0, hi: 0 },
|
|
886
|
-
promoted: { lo: 0, hi: 0 },
|
|
887
|
-
kingSq: [-1, -1],
|
|
888
|
-
};
|
|
889
|
-
}
|
|
890
|
-
export function cloneAsWritable(board) {
|
|
891
|
-
return {
|
|
892
|
-
white: { lo: board.white.lo >>> 0, hi: board.white.hi >>> 0 },
|
|
893
|
-
black: { lo: board.black.lo >>> 0, hi: board.black.hi >>> 0 },
|
|
894
|
-
pawn: { lo: board.pawn.lo >>> 0, hi: board.pawn.hi >>> 0 },
|
|
895
|
-
knight: { lo: board.knight.lo >>> 0, hi: board.knight.hi >>> 0 },
|
|
896
|
-
bishop: { lo: board.bishop.lo >>> 0, hi: board.bishop.hi >>> 0 },
|
|
897
|
-
rook: { lo: board.rook.lo >>> 0, hi: board.rook.hi >>> 0 },
|
|
898
|
-
queen: { lo: board.queen.lo >>> 0, hi: board.queen.hi >>> 0 },
|
|
899
|
-
king: { lo: board.king.lo >>> 0, hi: board.king.hi >>> 0 },
|
|
900
|
-
occupied: { lo: board.occupied.lo >>> 0, hi: board.occupied.hi >>> 0 },
|
|
901
|
-
promoted: { lo: board.promoted.lo >>> 0, hi: board.promoted.hi >>> 0 },
|
|
902
|
-
kingSq: board.kingSq ? [board.kingSq[0], board.kingSq[1]] : [-1, -1],
|
|
903
|
-
};
|
|
904
|
-
}
|
|
905
|
-
export function copyBoardInto(dst, src) {
|
|
906
|
-
dst.white.lo = src.white.lo;
|
|
907
|
-
dst.white.hi = src.white.hi;
|
|
908
|
-
dst.black.lo = src.black.lo;
|
|
909
|
-
dst.black.hi = src.black.hi;
|
|
910
|
-
dst.pawn.lo = src.pawn.lo;
|
|
911
|
-
dst.pawn.hi = src.pawn.hi;
|
|
912
|
-
dst.knight.lo = src.knight.lo;
|
|
913
|
-
dst.knight.hi = src.knight.hi;
|
|
914
|
-
dst.bishop.lo = src.bishop.lo;
|
|
915
|
-
dst.bishop.hi = src.bishop.hi;
|
|
916
|
-
dst.rook.lo = src.rook.lo;
|
|
917
|
-
dst.rook.hi = src.rook.hi;
|
|
918
|
-
dst.queen.lo = src.queen.lo;
|
|
919
|
-
dst.queen.hi = src.queen.hi;
|
|
920
|
-
dst.king.lo = src.king.lo;
|
|
921
|
-
dst.king.hi = src.king.hi;
|
|
922
|
-
dst.occupied.lo = src.occupied.lo;
|
|
923
|
-
dst.occupied.hi = src.occupied.hi;
|
|
924
|
-
dst.promoted.lo = src.promoted.lo;
|
|
925
|
-
dst.promoted.hi = src.promoted.hi;
|
|
926
|
-
if (src.kingSq) {
|
|
927
|
-
dst.kingSq[0] = src.kingSq[0];
|
|
928
|
-
dst.kingSq[1] = src.kingSq[1];
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
export function clearSquareInPlace(b, sqIdx) {
|
|
932
|
-
if (sqIdx < 32) {
|
|
933
|
-
const inv = ~(1 << sqIdx);
|
|
934
|
-
b.white.lo &= inv;
|
|
935
|
-
b.black.lo &= inv;
|
|
936
|
-
b.pawn.lo &= inv;
|
|
937
|
-
b.knight.lo &= inv;
|
|
938
|
-
b.bishop.lo &= inv;
|
|
939
|
-
b.rook.lo &= inv;
|
|
940
|
-
b.queen.lo &= inv;
|
|
941
|
-
b.king.lo &= inv;
|
|
942
|
-
b.occupied.lo &= inv;
|
|
943
|
-
b.promoted.lo &= inv;
|
|
944
|
-
}
|
|
945
|
-
else {
|
|
946
|
-
const inv = ~(1 << (sqIdx - 32));
|
|
947
|
-
b.white.hi &= inv;
|
|
948
|
-
b.black.hi &= inv;
|
|
949
|
-
b.pawn.hi &= inv;
|
|
950
|
-
b.knight.hi &= inv;
|
|
951
|
-
b.bishop.hi &= inv;
|
|
952
|
-
b.rook.hi &= inv;
|
|
953
|
-
b.queen.hi &= inv;
|
|
954
|
-
b.king.hi &= inv;
|
|
955
|
-
b.occupied.hi &= inv;
|
|
956
|
-
b.promoted.hi &= inv;
|
|
957
|
-
}
|
|
958
|
-
if (b.kingSq) {
|
|
959
|
-
if (b.kingSq[0] === sqIdx)
|
|
960
|
-
b.kingSq[0] = -1;
|
|
961
|
-
else if (b.kingSq[1] === sqIdx)
|
|
962
|
-
b.kingSq[1] = -1;
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
export function putPieceInPlace(b, sqIdx, piece) {
|
|
966
|
-
if (sqIdx < 32) {
|
|
967
|
-
const bit = (1 << sqIdx) >>> 0;
|
|
968
|
-
if (piece.color === Color.White)
|
|
969
|
-
b.white.lo |= bit;
|
|
970
|
-
else
|
|
971
|
-
b.black.lo |= bit;
|
|
972
|
-
switch (piece.role) {
|
|
973
|
-
case Role.Pawn:
|
|
974
|
-
b.pawn.lo |= bit;
|
|
975
|
-
break;
|
|
976
|
-
case Role.Knight:
|
|
977
|
-
b.knight.lo |= bit;
|
|
978
|
-
break;
|
|
979
|
-
case Role.Bishop:
|
|
980
|
-
b.bishop.lo |= bit;
|
|
981
|
-
break;
|
|
982
|
-
case Role.Rook:
|
|
983
|
-
b.rook.lo |= bit;
|
|
984
|
-
break;
|
|
985
|
-
case Role.Queen:
|
|
986
|
-
b.queen.lo |= bit;
|
|
987
|
-
break;
|
|
988
|
-
case Role.King:
|
|
989
|
-
b.king.lo |= bit;
|
|
990
|
-
break;
|
|
991
|
-
}
|
|
992
|
-
b.occupied.lo |= bit;
|
|
993
|
-
}
|
|
994
|
-
else {
|
|
995
|
-
const bit = (1 << (sqIdx - 32)) >>> 0;
|
|
996
|
-
if (piece.color === Color.White)
|
|
997
|
-
b.white.hi |= bit;
|
|
998
|
-
else
|
|
999
|
-
b.black.hi |= bit;
|
|
1000
|
-
switch (piece.role) {
|
|
1001
|
-
case Role.Pawn:
|
|
1002
|
-
b.pawn.hi |= bit;
|
|
1003
|
-
break;
|
|
1004
|
-
case Role.Knight:
|
|
1005
|
-
b.knight.hi |= bit;
|
|
1006
|
-
break;
|
|
1007
|
-
case Role.Bishop:
|
|
1008
|
-
b.bishop.hi |= bit;
|
|
1009
|
-
break;
|
|
1010
|
-
case Role.Rook:
|
|
1011
|
-
b.rook.hi |= bit;
|
|
1012
|
-
break;
|
|
1013
|
-
case Role.Queen:
|
|
1014
|
-
b.queen.hi |= bit;
|
|
1015
|
-
break;
|
|
1016
|
-
case Role.King:
|
|
1017
|
-
b.king.hi |= bit;
|
|
1018
|
-
break;
|
|
1019
|
-
}
|
|
1020
|
-
b.occupied.hi |= bit;
|
|
1021
|
-
}
|
|
1022
|
-
if (piece.role === Role.King && b.kingSq) {
|
|
1023
|
-
b.kingSq[piece.color] = sqIdx;
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
export function setPiece(board, sqIdx, piece) {
|
|
1027
|
-
const nb = new Board(board);
|
|
1028
|
-
putPieceInPlace(nb, sqIdx, piece);
|
|
1029
|
-
return nb;
|
|
1030
|
-
}
|
|
1031
|
-
export function removePiece(board, sqIdx) {
|
|
1032
|
-
const nb = new Board(board);
|
|
1033
|
-
clearSquareInPlace(nb, sqIdx);
|
|
1034
|
-
return nb;
|
|
1035
|
-
}
|
|
1036
|
-
export function pieceAt(board, sqIdx) {
|
|
1037
|
-
const bit = sq.singleton(sqIdx);
|
|
1038
|
-
if (sq.isEmpty(sq.and(board.occupied, bit)))
|
|
1039
|
-
return undefined;
|
|
1040
|
-
const color = sq.has(board.white, sqIdx) ? Color.White : Color.Black;
|
|
1041
|
-
let role;
|
|
1042
|
-
if (sq.has(board.pawn, sqIdx))
|
|
1043
|
-
role = Role.Pawn;
|
|
1044
|
-
else if (sq.has(board.knight, sqIdx))
|
|
1045
|
-
role = Role.Knight;
|
|
1046
|
-
else if (sq.has(board.bishop, sqIdx))
|
|
1047
|
-
role = Role.Bishop;
|
|
1048
|
-
else if (sq.has(board.rook, sqIdx))
|
|
1049
|
-
role = Role.Rook;
|
|
1050
|
-
else if (sq.has(board.queen, sqIdx))
|
|
1051
|
-
role = Role.Queen;
|
|
1052
|
-
else if (sq.has(board.king, sqIdx))
|
|
1053
|
-
role = Role.King;
|
|
1054
|
-
if (role === undefined)
|
|
1055
|
-
return undefined;
|
|
1056
|
-
return { color, role };
|
|
1057
|
-
}
|
|
1058
|
-
export function hasPiece(board, sqIdx) {
|
|
1059
|
-
return sq.has(board.occupied, sqIdx);
|
|
1060
|
-
}
|
|
1061
|
-
export function kingSquare(board, color) {
|
|
1062
|
-
if (board.kingSq !== undefined) {
|
|
1063
|
-
const k = board.kingSq[color];
|
|
1064
|
-
if (k >= 0)
|
|
1065
|
-
return k;
|
|
1066
|
-
}
|
|
1067
|
-
const ks = color === Color.White ? sq.and(board.white, board.king) : sq.and(board.black, board.king);
|
|
1068
|
-
return sq.first(ks);
|
|
1069
|
-
}
|
|
1070
|
-
export function occupiedEqualsWhiteBlack(board) {
|
|
1071
|
-
return sq.equals(board.occupied, sq.or(board.white, board.black));
|
|
1072
|
-
}
|
|
1073
|
-
export function rolePartitionEqualsOccupied(board) {
|
|
1074
|
-
const roles = sq.or(sq.or(sq.or(board.pawn, board.knight), sq.or(board.bishop, board.rook)), sq.or(board.queen, board.king));
|
|
1075
|
-
return sq.equals(roles, board.occupied);
|
|
1076
|
-
}
|
|
1077
|
-
export function boardEquals(a, b) {
|
|
1078
|
-
return (sq.equals(a.white, b.white) &&
|
|
1079
|
-
sq.equals(a.black, b.black) &&
|
|
1080
|
-
sq.equals(a.pawn, b.pawn) &&
|
|
1081
|
-
sq.equals(a.knight, b.knight) &&
|
|
1082
|
-
sq.equals(a.bishop, b.bishop) &&
|
|
1083
|
-
sq.equals(a.rook, b.rook) &&
|
|
1084
|
-
sq.equals(a.queen, b.queen) &&
|
|
1085
|
-
sq.equals(a.king, b.king) &&
|
|
1086
|
-
sq.equals(a.occupied, b.occupied) &&
|
|
1087
|
-
sq.equals(a.promoted, b.promoted));
|
|
1088
|
-
}
|