gigachess 0.3.0 → 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 +102 -125
- package/dist/attacks.d.ts +3 -3
- package/dist/attacks.d.ts.map +1 -1
- package/dist/attacks.js +1 -1
- package/dist/board.d.ts +99 -44
- package/dist/board.d.ts.map +1 -1
- package/dist/board.js +726 -15
- package/dist/castling.d.ts +11 -1
- package/dist/castling.d.ts.map +1 -1
- package/dist/castling.js +122 -0
- package/dist/chess.d.ts +11 -23
- package/dist/chess.d.ts.map +1 -1
- package/dist/chess.js +81 -178
- 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/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.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -6
- package/dist/packedMove.js +1 -1
- package/dist/squareSet.js +1 -1
- package/dist/types.d.ts +1 -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 +89 -33
- package/dist/zobristBlob.js +1 -1
- package/package.json +5 -5
package/dist/board.js
CHANGED
|
@@ -1,6 +1,728 @@
|
|
|
1
|
-
// src/board.ts —
|
|
1
|
+
// src/board.ts — stateful high-performance Board class and bitboard operations
|
|
2
|
+
// Mirrors gigachess::Board in Rust (ADR-001, ADR-003).
|
|
2
3
|
import * as sq from "./squareSet.js";
|
|
3
4
|
import { Color, Role } from "./types.js";
|
|
5
|
+
import { opposite } from "./util.js";
|
|
6
|
+
import { kingAttackers } 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, } from "./packedMove.js";
|
|
10
|
+
import { parseFen, makeFen } from "./fen.js";
|
|
11
|
+
import { parseSan, 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
|
+
makeMove(moveWord) {
|
|
400
|
+
const word = typeof moveWord === "number" ? moveWord : packOf(moveWord);
|
|
401
|
+
const from = word & 0x3f;
|
|
402
|
+
const to = (word >> 6) & 0x3f;
|
|
403
|
+
const promoCode = (word >> 12) & 0x0f;
|
|
404
|
+
const promoRole = promoCodeToRole(promoCode) ?? null;
|
|
405
|
+
const movingRole = this.roleAt(from);
|
|
406
|
+
if (movingRole === undefined) {
|
|
407
|
+
throw new Error(`no piece at from square ${from}`);
|
|
408
|
+
}
|
|
409
|
+
const us = this.turn;
|
|
410
|
+
const them = opposite(us);
|
|
411
|
+
const prevCheckers = this.checkers;
|
|
412
|
+
const prevZobristLo = this._zobristLo;
|
|
413
|
+
const prevZobristHi = this._zobristHi;
|
|
414
|
+
const prevCastling = this.castling;
|
|
415
|
+
const prevCastlingMask = this.castlingMask;
|
|
416
|
+
const prevEpSquare = this.epSquare;
|
|
417
|
+
const prevHalfmoves = this.halfmoves;
|
|
418
|
+
const prevFullmoves = this.fullmoves;
|
|
419
|
+
const prevKingSq = [this.kingSq[0], this.kingSq[1]];
|
|
420
|
+
const prevEpHashable = prevEpSquare !== null && zobristTablesLoaded() && epIsHashable(this, prevEpSquare, us);
|
|
421
|
+
let plan = null;
|
|
422
|
+
if (movingRole === Role.King) {
|
|
423
|
+
plan = detectCastling(this, from, to);
|
|
424
|
+
}
|
|
425
|
+
const isCastling = plan !== null;
|
|
426
|
+
let isEnPassant = false;
|
|
427
|
+
if (movingRole === Role.Pawn &&
|
|
428
|
+
this.epSquare !== null &&
|
|
429
|
+
to === this.epSquare &&
|
|
430
|
+
(from & 7) !== (to & 7)) {
|
|
431
|
+
isEnPassant = true;
|
|
432
|
+
}
|
|
433
|
+
let capturedRole = -1;
|
|
434
|
+
let capturedSq = -1;
|
|
435
|
+
if (isEnPassant) {
|
|
436
|
+
capturedRole = Role.Pawn;
|
|
437
|
+
capturedSq = us === Color.White ? to - 8 : to + 8;
|
|
438
|
+
}
|
|
439
|
+
else if (!isCastling) {
|
|
440
|
+
const capRole = this.roleAt(to);
|
|
441
|
+
if (capRole !== undefined) {
|
|
442
|
+
capturedRole = capRole;
|
|
443
|
+
capturedSq = to;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
this._clearSquare(from);
|
|
447
|
+
if (capturedSq >= 0) {
|
|
448
|
+
this._clearSquare(capturedSq);
|
|
449
|
+
}
|
|
450
|
+
if (isCastling && plan !== null) {
|
|
451
|
+
this._clearSquare(plan.rookFrom);
|
|
452
|
+
this._putPiece(plan.rookTo, us, Role.Rook);
|
|
453
|
+
this._putPiece(plan.kingTo, us, Role.King);
|
|
454
|
+
this.kingSq[us] = plan.kingTo;
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
const finalRole = promoRole ?? movingRole;
|
|
458
|
+
this._putPiece(to, us, finalRole);
|
|
459
|
+
if (movingRole === Role.King) {
|
|
460
|
+
this.kingSq[us] = to;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
let newCastling;
|
|
464
|
+
let newMask;
|
|
465
|
+
if (!this.isChess960) {
|
|
466
|
+
const mask = this.castlingMask ?? getCastlingMask(this);
|
|
467
|
+
newMask = (mask & CASTLE_CLEAR_STD[from] & CASTLE_CLEAR_STD[to]) >>> 0;
|
|
468
|
+
newCastling = CASTLING_RIGHTS_TABLE[newMask];
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
let newWhite = this.castling.white;
|
|
472
|
+
let newBlack = this.castling.black;
|
|
473
|
+
if (movingRole === Role.King) {
|
|
474
|
+
if (us === Color.White) {
|
|
475
|
+
if (newWhite.size > 0)
|
|
476
|
+
newWhite = new Set();
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
if (newBlack.size > 0)
|
|
480
|
+
newBlack = new Set();
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (movingRole === Role.Rook) {
|
|
484
|
+
if (us === Color.White && newWhite.has(from)) {
|
|
485
|
+
const next = new Set(newWhite);
|
|
486
|
+
next.delete(from);
|
|
487
|
+
newWhite = next;
|
|
488
|
+
}
|
|
489
|
+
else if (us === Color.Black && newBlack.has(from)) {
|
|
490
|
+
const next = new Set(newBlack);
|
|
491
|
+
next.delete(from);
|
|
492
|
+
newBlack = next;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
if (capturedRole === Role.Rook && capturedSq >= 0) {
|
|
496
|
+
if (them === Color.White && newWhite.has(capturedSq)) {
|
|
497
|
+
const next = new Set(newWhite);
|
|
498
|
+
next.delete(capturedSq);
|
|
499
|
+
newWhite = next;
|
|
500
|
+
}
|
|
501
|
+
else if (them === Color.Black && newBlack.has(capturedSq)) {
|
|
502
|
+
const next = new Set(newBlack);
|
|
503
|
+
next.delete(capturedSq);
|
|
504
|
+
newBlack = next;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
newCastling = {
|
|
508
|
+
white: newWhite,
|
|
509
|
+
black: newBlack,
|
|
510
|
+
whiteKing: newWhite.has(7),
|
|
511
|
+
whiteQueen: newWhite.has(0),
|
|
512
|
+
blackKing: newBlack.has(63),
|
|
513
|
+
blackQueen: newBlack.has(56),
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
this.castling = newCastling;
|
|
517
|
+
this.castlingMask = newMask;
|
|
518
|
+
let newEp = null;
|
|
519
|
+
if (movingRole === Role.Pawn && Math.abs((to >> 3) - (from >> 3)) === 2) {
|
|
520
|
+
const epRank = us === Color.White ? (from >> 3) + 1 : (from >> 3) - 1;
|
|
521
|
+
newEp = (epRank << 3) | (from & 7);
|
|
522
|
+
}
|
|
523
|
+
this.epSquare = newEp;
|
|
524
|
+
const isCapture = capturedSq >= 0;
|
|
525
|
+
const isPawnMove = movingRole === Role.Pawn;
|
|
526
|
+
this.halfmoves = isCapture || isPawnMove ? 0 : prevHalfmoves + 1;
|
|
527
|
+
this.fullmoves = us === Color.Black ? prevFullmoves + 1 : prevFullmoves;
|
|
528
|
+
this.turn = them;
|
|
529
|
+
this.checkers = kingAttackers(this, them);
|
|
530
|
+
if (zobristTablesLoaded() && keyLo && keyHi && castleKeyLo && castleKeyHi) {
|
|
531
|
+
if (this._zobristLo === 0 && this._zobristHi === 0) {
|
|
532
|
+
this.refreshZobrist();
|
|
533
|
+
}
|
|
534
|
+
let zLo = this._zobristLo;
|
|
535
|
+
let zHi = this._zobristHi;
|
|
536
|
+
// 1. Moving piece leaves from
|
|
537
|
+
const kFrom = pieceKeyIdx(us, movingRole, from);
|
|
538
|
+
zLo = (zLo ^ keyLo[kFrom]) >>> 0;
|
|
539
|
+
zHi = (zHi ^ keyHi[kFrom]) >>> 0;
|
|
540
|
+
// 2. Moving piece lands on to (or castling / promo)
|
|
541
|
+
if (plan !== null) {
|
|
542
|
+
const kRookFrom = pieceKeyIdx(us, Role.Rook, plan.rookFrom);
|
|
543
|
+
const kRookTo = pieceKeyIdx(us, Role.Rook, plan.rookTo);
|
|
544
|
+
const kKingTo = pieceKeyIdx(us, Role.King, plan.kingTo);
|
|
545
|
+
zLo = (zLo ^ keyLo[kRookFrom] ^ keyLo[kRookTo] ^ keyLo[kKingTo]) >>> 0;
|
|
546
|
+
zHi = (zHi ^ keyHi[kRookFrom] ^ keyHi[kRookTo] ^ keyHi[kKingTo]) >>> 0;
|
|
547
|
+
}
|
|
548
|
+
else if (promoRole !== null) {
|
|
549
|
+
const kPromo = pieceKeyIdx(us, promoRole, to);
|
|
550
|
+
zLo = (zLo ^ keyLo[kPromo]) >>> 0;
|
|
551
|
+
zHi = (zHi ^ keyHi[kPromo]) >>> 0;
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
const kTo = pieceKeyIdx(us, movingRole, to);
|
|
555
|
+
zLo = (zLo ^ keyLo[kTo]) >>> 0;
|
|
556
|
+
zHi = (zHi ^ keyHi[kTo]) >>> 0;
|
|
557
|
+
}
|
|
558
|
+
// 3. Captured piece leaves board
|
|
559
|
+
if (isEnPassant) {
|
|
560
|
+
const capSq = us === Color.White ? to - 8 : to + 8;
|
|
561
|
+
const kCap = pieceKeyIdx(them, Role.Pawn, capSq);
|
|
562
|
+
zLo = (zLo ^ keyLo[kCap]) >>> 0;
|
|
563
|
+
zHi = (zHi ^ keyHi[kCap]) >>> 0;
|
|
564
|
+
}
|
|
565
|
+
else if (capturedRole !== -1) {
|
|
566
|
+
const kCap = pieceKeyIdx(them, capturedRole, to);
|
|
567
|
+
zLo = (zLo ^ keyLo[kCap]) >>> 0;
|
|
568
|
+
zHi = (zHi ^ keyHi[kCap]) >>> 0;
|
|
569
|
+
}
|
|
570
|
+
// 4. Castling rights diff
|
|
571
|
+
if (!this.isChess960) {
|
|
572
|
+
const diff = (prevCastlingMask ?? 0) ^ (newMask ?? 0);
|
|
573
|
+
if (diff !== 0) {
|
|
574
|
+
if (diff & 1) {
|
|
575
|
+
zLo = (zLo ^ castleKeyLo[15]) >>> 0;
|
|
576
|
+
zHi = (zHi ^ castleKeyHi[15]) >>> 0;
|
|
577
|
+
}
|
|
578
|
+
if (diff & 2) {
|
|
579
|
+
zLo = (zLo ^ castleKeyLo[8]) >>> 0;
|
|
580
|
+
zHi = (zHi ^ castleKeyHi[8]) >>> 0;
|
|
581
|
+
}
|
|
582
|
+
if (diff & 4) {
|
|
583
|
+
zLo = (zLo ^ castleKeyLo[7]) >>> 0;
|
|
584
|
+
zHi = (zHi ^ castleKeyHi[7]) >>> 0;
|
|
585
|
+
}
|
|
586
|
+
if (diff & 8) {
|
|
587
|
+
zLo = (zLo ^ castleKeyLo[0]) >>> 0;
|
|
588
|
+
zHi = (zHi ^ castleKeyHi[0]) >>> 0;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
else {
|
|
593
|
+
for (const rs of prevCastling.white) {
|
|
594
|
+
const idx = castlingKeyIdx(Color.White, rs);
|
|
595
|
+
zLo = (zLo ^ castleKeyLo[idx]) >>> 0;
|
|
596
|
+
zHi = (zHi ^ castleKeyHi[idx]) >>> 0;
|
|
597
|
+
}
|
|
598
|
+
for (const rs of prevCastling.black) {
|
|
599
|
+
const idx = castlingKeyIdx(Color.Black, rs);
|
|
600
|
+
zLo = (zLo ^ castleKeyLo[idx]) >>> 0;
|
|
601
|
+
zHi = (zHi ^ castleKeyHi[idx]) >>> 0;
|
|
602
|
+
}
|
|
603
|
+
for (const rs of newCastling.white) {
|
|
604
|
+
const idx = castlingKeyIdx(Color.White, rs);
|
|
605
|
+
zLo = (zLo ^ castleKeyLo[idx]) >>> 0;
|
|
606
|
+
zHi = (zHi ^ castleKeyHi[idx]) >>> 0;
|
|
607
|
+
}
|
|
608
|
+
for (const rs of newCastling.black) {
|
|
609
|
+
const idx = castlingKeyIdx(Color.Black, rs);
|
|
610
|
+
zLo = (zLo ^ castleKeyLo[idx]) >>> 0;
|
|
611
|
+
zHi = (zHi ^ castleKeyHi[idx]) >>> 0;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
// 5. En passant square diff
|
|
615
|
+
if (prevEpHashable) {
|
|
616
|
+
const kEp = IDX_EP + (prevEpSquare & 7);
|
|
617
|
+
zLo = (zLo ^ keyLo[kEp]) >>> 0;
|
|
618
|
+
zHi = (zHi ^ keyHi[kEp]) >>> 0;
|
|
619
|
+
}
|
|
620
|
+
if (newEp !== null && epIsHashable(this, newEp, them)) {
|
|
621
|
+
const kEp = IDX_EP + (newEp & 7);
|
|
622
|
+
zLo = (zLo ^ keyLo[kEp]) >>> 0;
|
|
623
|
+
zHi = (zHi ^ keyHi[kEp]) >>> 0;
|
|
624
|
+
}
|
|
625
|
+
// 6. Turn flip
|
|
626
|
+
zLo = (zLo ^ keyLo[IDX_SIDE]) >>> 0;
|
|
627
|
+
zHi = (zHi ^ keyHi[IDX_SIDE]) >>> 0;
|
|
628
|
+
this._zobristLo = zLo;
|
|
629
|
+
this._zobristHi = zHi;
|
|
630
|
+
}
|
|
631
|
+
return {
|
|
632
|
+
move: word,
|
|
633
|
+
movingRole,
|
|
634
|
+
capturedRole,
|
|
635
|
+
capturedSq,
|
|
636
|
+
castlingPlan: plan,
|
|
637
|
+
isEnPassant,
|
|
638
|
+
promoRole,
|
|
639
|
+
epSquare: prevEpSquare,
|
|
640
|
+
castling: prevCastling,
|
|
641
|
+
castlingMask: prevCastlingMask,
|
|
642
|
+
halfmoves: prevHalfmoves,
|
|
643
|
+
fullmoves: prevFullmoves,
|
|
644
|
+
kingSq: prevKingSq,
|
|
645
|
+
checkers: prevCheckers,
|
|
646
|
+
zobristLo: prevZobristLo,
|
|
647
|
+
zobristHi: prevZobristHi,
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
unmakeMove(undo) {
|
|
651
|
+
const from = undo.move & 0x3f;
|
|
652
|
+
const to = (undo.move >> 6) & 0x3f;
|
|
653
|
+
const us = opposite(this.turn);
|
|
654
|
+
this.turn = us;
|
|
655
|
+
this.halfmoves = undo.halfmoves;
|
|
656
|
+
this.fullmoves = undo.fullmoves;
|
|
657
|
+
this.epSquare = undo.epSquare;
|
|
658
|
+
this.castling = undo.castling;
|
|
659
|
+
this.castlingMask = undo.castlingMask;
|
|
660
|
+
this.kingSq[0] = undo.kingSq[0];
|
|
661
|
+
this.kingSq[1] = undo.kingSq[1];
|
|
662
|
+
this.checkers = undo.checkers;
|
|
663
|
+
this._zobristLo = undo.zobristLo;
|
|
664
|
+
this._zobristHi = undo.zobristHi;
|
|
665
|
+
if (undo.castlingPlan !== null) {
|
|
666
|
+
const plan = undo.castlingPlan;
|
|
667
|
+
this._clearSquare(plan.kingTo);
|
|
668
|
+
this._clearSquare(plan.rookTo);
|
|
669
|
+
this._putPiece(plan.kingFrom, us, Role.King);
|
|
670
|
+
this._putPiece(plan.rookFrom, us, Role.Rook);
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
this._clearSquare(to);
|
|
674
|
+
this._putPiece(from, us, undo.movingRole);
|
|
675
|
+
if (undo.capturedSq >= 0 && undo.capturedRole >= 0) {
|
|
676
|
+
const them = opposite(us);
|
|
677
|
+
this._putPiece(undo.capturedSq, them, undo.capturedRole);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
legalMoves(outBuffer) {
|
|
682
|
+
const buf = outBuffer ?? new Uint16Array(256);
|
|
683
|
+
const count = legalMovesInto(this, buf);
|
|
684
|
+
return buf.subarray(0, count);
|
|
685
|
+
}
|
|
686
|
+
forEachLegalMove(fn) {
|
|
687
|
+
forEachLegalMove(this, (from, to, promo) => {
|
|
688
|
+
fn(((from & 0x3f) | ((to & 0x3f) << 6) | ((promo & 0x0f) << 12)) >>> 0);
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
toSan(moveWord) {
|
|
692
|
+
const move = unpackToMove(moveWord);
|
|
693
|
+
return makeSan(move, this);
|
|
694
|
+
}
|
|
695
|
+
toUci(moveWord) {
|
|
696
|
+
const move = unpackToMove(moveWord);
|
|
697
|
+
return makeUci(move);
|
|
698
|
+
}
|
|
699
|
+
toFen() {
|
|
700
|
+
return makeFen(this);
|
|
701
|
+
}
|
|
702
|
+
parseSan(san) {
|
|
703
|
+
const res = parseSan(san, this);
|
|
704
|
+
return res.ok ? packOf(res.value) : null;
|
|
705
|
+
}
|
|
706
|
+
parseUci(uci) {
|
|
707
|
+
const res = parseUci(uci);
|
|
708
|
+
return res.ok && isLegal(this, res.value) ? packOf(res.value) : null;
|
|
709
|
+
}
|
|
710
|
+
perft(depth) {
|
|
711
|
+
if (depth <= 0)
|
|
712
|
+
return 1;
|
|
713
|
+
if (depth === 1)
|
|
714
|
+
return countLegalMoves(this);
|
|
715
|
+
const buf = new Uint16Array(256);
|
|
716
|
+
const count = legalMovesInto(this, buf);
|
|
717
|
+
let nodes = 0;
|
|
718
|
+
for (let i = 0; i < count; i++) {
|
|
719
|
+
const undo = this.makeMove(buf[i]);
|
|
720
|
+
nodes += this.perft(depth - 1);
|
|
721
|
+
this.unmakeMove(undo);
|
|
722
|
+
}
|
|
723
|
+
return nodes;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
4
726
|
export function emptyBoard() {
|
|
5
727
|
const e = sq.empty();
|
|
6
728
|
return {
|
|
@@ -18,7 +740,6 @@ export function emptyBoard() {
|
|
|
18
740
|
};
|
|
19
741
|
}
|
|
20
742
|
export function cloneBoard(board) {
|
|
21
|
-
// pure clone — SquareSet are immutable values, but we return new object
|
|
22
743
|
return {
|
|
23
744
|
white: { lo: board.white.lo >>> 0, hi: board.white.hi >>> 0 },
|
|
24
745
|
black: { lo: board.black.lo >>> 0, hi: board.black.hi >>> 0 },
|
|
@@ -30,12 +751,10 @@ export function cloneBoard(board) {
|
|
|
30
751
|
king: { lo: board.king.lo >>> 0, hi: board.king.hi >>> 0 },
|
|
31
752
|
occupied: { lo: board.occupied.lo >>> 0, hi: board.occupied.hi >>> 0 },
|
|
32
753
|
promoted: { lo: board.promoted.lo >>> 0, hi: board.promoted.hi >>> 0 },
|
|
33
|
-
kingSq: board.kingSq ? [board.kingSq[0], board.kingSq[1]] :
|
|
754
|
+
kingSq: board.kingSq ? [board.kingSq[0], board.kingSq[1]] : [-1, -1],
|
|
34
755
|
};
|
|
35
756
|
}
|
|
36
757
|
export function newScratchBoard() {
|
|
37
|
-
// Each field gets its OWN object: the fields are mutable now, so sharing one
|
|
38
|
-
// empty object across all ten would alias them together.
|
|
39
758
|
return {
|
|
40
759
|
white: { lo: 0, hi: 0 },
|
|
41
760
|
black: { lo: 0, hi: 0 },
|
|
@@ -51,8 +770,6 @@ export function newScratchBoard() {
|
|
|
51
770
|
};
|
|
52
771
|
}
|
|
53
772
|
export function cloneAsWritable(board) {
|
|
54
|
-
// fresh mutable objects (the result escapes as a new Board); SquareSet
|
|
55
|
-
// values are immutable so a field-bit copy suffices
|
|
56
773
|
return {
|
|
57
774
|
white: { lo: board.white.lo >>> 0, hi: board.white.hi >>> 0 },
|
|
58
775
|
black: { lo: board.black.lo >>> 0, hi: board.black.hi >>> 0 },
|
|
@@ -67,8 +784,6 @@ export function cloneAsWritable(board) {
|
|
|
67
784
|
kingSq: board.kingSq ? [board.kingSq[0], board.kingSq[1]] : [-1, -1],
|
|
68
785
|
};
|
|
69
786
|
}
|
|
70
|
-
/** Copy `src`'s ten fields into `dst`. Zero-allocation: the scratch owns its
|
|
71
|
-
* field objects, so only the 64-bit values are assigned (no {lo,hi} churn). */
|
|
72
787
|
export function copyBoardInto(dst, src) {
|
|
73
788
|
dst.white.lo = src.white.lo;
|
|
74
789
|
dst.white.hi = src.white.hi;
|
|
@@ -95,8 +810,6 @@ export function copyBoardInto(dst, src) {
|
|
|
95
810
|
dst.kingSq[1] = src.kingSq[1];
|
|
96
811
|
}
|
|
97
812
|
}
|
|
98
|
-
/** In-place: remove the piece (any color/role) at sqIdx. Hot-loop only.
|
|
99
|
-
* Zero-allocation: raw bitmasks instead of sq.not/singleton/and objects. */
|
|
100
813
|
export function clearSquareInPlace(b, sqIdx) {
|
|
101
814
|
if (sqIdx < 32) {
|
|
102
815
|
const inv = ~(1 << sqIdx);
|
|
@@ -131,7 +844,6 @@ export function clearSquareInPlace(b, sqIdx) {
|
|
|
131
844
|
b.kingSq[1] = -1;
|
|
132
845
|
}
|
|
133
846
|
}
|
|
134
|
-
/** In-place: place a piece at sqIdx. Hot-loop only. Zero-allocation. */
|
|
135
847
|
export function putPieceInPlace(b, sqIdx, piece) {
|
|
136
848
|
if (sqIdx < 32) {
|
|
137
849
|
const bit = (1 << sqIdx) >>> 0;
|
|
@@ -194,12 +906,12 @@ export function putPieceInPlace(b, sqIdx, piece) {
|
|
|
194
906
|
}
|
|
195
907
|
}
|
|
196
908
|
export function setPiece(board, sqIdx, piece) {
|
|
197
|
-
const nb =
|
|
909
|
+
const nb = new Board(board);
|
|
198
910
|
putPieceInPlace(nb, sqIdx, piece);
|
|
199
911
|
return nb;
|
|
200
912
|
}
|
|
201
913
|
export function removePiece(board, sqIdx) {
|
|
202
|
-
const nb =
|
|
914
|
+
const nb = new Board(board);
|
|
203
915
|
clearSquareInPlace(nb, sqIdx);
|
|
204
916
|
return nb;
|
|
205
917
|
}
|
|
@@ -244,7 +956,6 @@ export function rolePartitionEqualsOccupied(board) {
|
|
|
244
956
|
const roles = sq.or(sq.or(sq.or(board.pawn, board.knight), sq.or(board.bishop, board.rook)), sq.or(board.queen, board.king));
|
|
245
957
|
return sq.equals(roles, board.occupied);
|
|
246
958
|
}
|
|
247
|
-
// For testing immutability
|
|
248
959
|
export function boardEquals(a, b) {
|
|
249
960
|
return (sq.equals(a.white, b.white) &&
|
|
250
961
|
sq.equals(a.black, b.black) &&
|