gigachess 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -108
- package/dist/attacks.d.ts +6 -3
- package/dist/attacks.d.ts.map +1 -1
- package/dist/attacks.js +45 -1
- package/dist/board.d.ts +111 -45
- package/dist/board.d.ts.map +1 -1
- package/dist/board.js +747 -14
- package/dist/castling.d.ts +41 -0
- package/dist/castling.d.ts.map +1 -0
- package/dist/castling.js +267 -0
- package/dist/chess.d.ts +30 -28
- package/dist/chess.d.ts.map +1 -1
- package/dist/chess.js +835 -458
- package/dist/chess960.d.ts +1 -1
- package/dist/chess960.d.ts.map +1 -1
- package/dist/chess960.js +2 -2
- package/dist/chessjs.d.ts +13 -0
- package/dist/chessjs.d.ts.map +1 -0
- package/dist/chessjs.js +23 -0
- package/dist/chessops/board.d.ts +1 -1
- package/dist/chessops/board.d.ts.map +1 -1
- package/dist/chessops/chess.js +1 -1
- package/dist/chessops/convert.d.ts +1 -1
- package/dist/chessops/convert.d.ts.map +1 -1
- package/dist/chessops/convert.js +1 -6
- package/dist/chessops/fenInternal.d.ts +1 -1
- package/dist/chessops/fenInternal.d.ts.map +1 -1
- package/dist/chessops/fenInternal.js +2 -2
- package/dist/chessops/index.js +2 -2
- package/dist/chessops/setup.js +1 -1
- package/dist/chessops/types.js +1 -1
- package/dist/chesstree.d.ts +1 -1
- package/dist/chesstree.d.ts.map +1 -1
- package/dist/chesstree.js +5 -5
- package/dist/core.d.ts +1 -1
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +2 -2
- package/dist/fen.d.ts.map +1 -1
- package/dist/fen.js +84 -48
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -8
- package/dist/packedMove.js +2 -2
- package/dist/san.d.ts.map +1 -1
- package/dist/san.js +148 -172
- package/dist/squareSet.js +1 -1
- package/dist/types.d.ts +6 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/zobrist.d.ts +22 -0
- package/dist/zobrist.d.ts.map +1 -1
- package/dist/zobrist.js +107 -36
- package/dist/zobristBlob.js +1 -1
- package/package.json +8 -6
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { CastlingRights, Position } from "./types.js";
|
|
2
|
+
import { Color } from "./types.js";
|
|
3
|
+
export type CastlingPlan = {
|
|
4
|
+
side: "king" | "queen";
|
|
5
|
+
kingFrom: number;
|
|
6
|
+
kingTo: number;
|
|
7
|
+
rookFrom: number;
|
|
8
|
+
rookTo: number;
|
|
9
|
+
};
|
|
10
|
+
export declare const CASTLE_WK = 1;
|
|
11
|
+
export declare const CASTLE_WQ = 2;
|
|
12
|
+
export declare const CASTLE_BK = 4;
|
|
13
|
+
export declare const CASTLE_BQ = 8;
|
|
14
|
+
/**
|
|
15
|
+
* Precomputed 64-entry lookup table for single-cycle rights clearing.
|
|
16
|
+
* Clears rights when king or rook moves or is captured on standard squares.
|
|
17
|
+
*/
|
|
18
|
+
export declare const CASTLE_CLEAR_STD: Uint8Array<ArrayBuffer>;
|
|
19
|
+
/** Standard castling plans (immutable/frozen singletons) */
|
|
20
|
+
export declare const PLAN_WHITE_K: CastlingPlan;
|
|
21
|
+
export declare const PLAN_WHITE_Q: CastlingPlan;
|
|
22
|
+
export declare const PLAN_BLACK_K: CastlingPlan;
|
|
23
|
+
export declare const PLAN_BLACK_Q: CastlingPlan;
|
|
24
|
+
/** Precomputed 16-entry FEN castling string lookup table */
|
|
25
|
+
export declare const CASTLING_FEN_STR: readonly string[];
|
|
26
|
+
/** Precomputed 16-entry CastlingRights table for standard chess (zero allocations) */
|
|
27
|
+
export declare const CASTLING_RIGHTS_TABLE: readonly CastlingRights[];
|
|
28
|
+
export declare function getCastlingMask(pos: Position): number;
|
|
29
|
+
/**
|
|
30
|
+
* Precomputed 128-entry tables for rank-0 (White: 0..63) and rank-7 (Black: 64..127)
|
|
31
|
+
* castling path clearance in Chess960 and standard chess.
|
|
32
|
+
* Single-cycle check: ((CASTLE_PATH_LO[idx] & occ.lo) | (CASTLE_PATH_HI[idx] & occ.hi)) === 0
|
|
33
|
+
*/
|
|
34
|
+
export declare const CASTLE_PATH_LO: Uint32Array<ArrayBuffer>;
|
|
35
|
+
export declare const CASTLE_PATH_HI: Uint32Array<ArrayBuffer>;
|
|
36
|
+
export declare const CASTLE_TRAVERSAL_WHITE: (readonly number[])[];
|
|
37
|
+
export declare const CASTLE_TRAVERSAL_BLACK: (readonly number[])[];
|
|
38
|
+
export declare function castlingPlanFor(color: Color, ks: number, rs: number): CastlingPlan;
|
|
39
|
+
export declare function planForDest(plans: CastlingPlan[], to: number): CastlingPlan | null;
|
|
40
|
+
export declare function detectCastling(pos: Position, from: number, to: number): CastlingPlan | null;
|
|
41
|
+
//# sourceMappingURL=castling.d.ts.map
|
|
@@ -0,0 +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,GAAG;IACzB,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,eAAO,MAAM,SAAS,IAAI,CAAC;AAC3B,eAAO,MAAM,SAAS,IAAI,CAAC;AAC3B,eAAO,MAAM,SAAS,IAAI,CAAC;AAC3B,eAAO,MAAM,SAAS,IAAI,CAAC;AAE3B;;;GAGG;AACH,eAAO,MAAM,gBAAgB,yBAAqB,CAAC;AASnD,4DAA4D;AAC5D,eAAO,MAAM,YAAY,EAAE,YAMzB,CAAC;AAEH,eAAO,MAAM,YAAY,EAAE,YAMzB,CAAC;AAEH,eAAO,MAAM,YAAY,EAAE,YAMzB,CAAC;AAEH,eAAO,MAAM,YAAY,EAAE,YAMzB,CAAC;AAEH,4DAA4D;AAC5D,eAAO,MAAM,gBAAgB,EAAE,SAAS,MAAM,EAG7C,CAAC;AAaF,sFAAsF;AACtF,eAAO,MAAM,qBAAqB,EAAE,SAAS,cAAc,EAcvD,CAAC;AAEL,wBAAgB,eAAe,CAAC,GAAG,EAAE,QAAQ,GAAG,MAAM,CAQrD;AAED;;;;GAIG;AACH,eAAO,MAAM,cAAc,0BAAuB,CAAC;AACnD,eAAO,MAAM,cAAc,0BAAuB,CAAC;AAEnD,eAAO,MAAM,sBAAsB,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,EAAkB,CAAC;AAC3E,eAAO,MAAM,sBAAsB,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,EAAkB,CAAC;AAmD3E,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,YAAY,CAUlF;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,EAAE,EAAE,EAAE,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CAMlF;AAeD,wBAAgB,cAAc,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CA+D3F"}
|
package/dist/castling.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { Color, Role } from "./types.js";
|
|
2
|
+
import { squareRank, squareFile } from "./util.js";
|
|
3
|
+
export const CASTLE_WK = 1; // 0b0001
|
|
4
|
+
export const CASTLE_WQ = 2; // 0b0010
|
|
5
|
+
export const CASTLE_BK = 4; // 0b0100
|
|
6
|
+
export const CASTLE_BQ = 8; // 0b1000
|
|
7
|
+
/**
|
|
8
|
+
* Precomputed 64-entry lookup table for single-cycle rights clearing.
|
|
9
|
+
* Clears rights when king or rook moves or is captured on standard squares.
|
|
10
|
+
*/
|
|
11
|
+
export const CASTLE_CLEAR_STD = new Uint8Array(64);
|
|
12
|
+
CASTLE_CLEAR_STD.fill(0x0F);
|
|
13
|
+
CASTLE_CLEAR_STD[4] = 0x0C; // e1 (White King) -> clears WK | WQ (12)
|
|
14
|
+
CASTLE_CLEAR_STD[60] = 0x03; // e8 (Black King) -> clears BK | BQ (3)
|
|
15
|
+
CASTLE_CLEAR_STD[7] = 0x0E; // h1 (White King Rook) -> clears WK (14)
|
|
16
|
+
CASTLE_CLEAR_STD[0] = 0x0D; // a1 (White Queen Rook) -> clears WQ (13)
|
|
17
|
+
CASTLE_CLEAR_STD[63] = 0x0B; // h8 (Black King Rook) -> clears BK (11)
|
|
18
|
+
CASTLE_CLEAR_STD[56] = 0x07; // a8 (Black Queen Rook) -> clears BQ (7)
|
|
19
|
+
/** Standard castling plans (immutable/frozen singletons) */
|
|
20
|
+
export const PLAN_WHITE_K = Object.freeze({
|
|
21
|
+
side: "king",
|
|
22
|
+
kingFrom: 4,
|
|
23
|
+
kingTo: 6,
|
|
24
|
+
rookFrom: 7,
|
|
25
|
+
rookTo: 5,
|
|
26
|
+
});
|
|
27
|
+
export const PLAN_WHITE_Q = Object.freeze({
|
|
28
|
+
side: "queen",
|
|
29
|
+
kingFrom: 4,
|
|
30
|
+
kingTo: 2,
|
|
31
|
+
rookFrom: 0,
|
|
32
|
+
rookTo: 3,
|
|
33
|
+
});
|
|
34
|
+
export const PLAN_BLACK_K = Object.freeze({
|
|
35
|
+
side: "king",
|
|
36
|
+
kingFrom: 60,
|
|
37
|
+
kingTo: 62,
|
|
38
|
+
rookFrom: 63,
|
|
39
|
+
rookTo: 61,
|
|
40
|
+
});
|
|
41
|
+
export const PLAN_BLACK_Q = Object.freeze({
|
|
42
|
+
side: "queen",
|
|
43
|
+
kingFrom: 60,
|
|
44
|
+
kingTo: 58,
|
|
45
|
+
rookFrom: 56,
|
|
46
|
+
rookTo: 59,
|
|
47
|
+
});
|
|
48
|
+
/** Precomputed 16-entry FEN castling string lookup table */
|
|
49
|
+
export const CASTLING_FEN_STR = [
|
|
50
|
+
"-", "K", "Q", "KQ", "k", "Kk", "Qk", "KQk",
|
|
51
|
+
"q", "Kq", "Qq", "KQq", "kq", "Kkq", "Qkq", "KQkq"
|
|
52
|
+
];
|
|
53
|
+
const S0 = new Set();
|
|
54
|
+
const S7 = new Set([7]);
|
|
55
|
+
const S0_2 = new Set([0]);
|
|
56
|
+
const S0_7 = new Set([0, 7]);
|
|
57
|
+
const WHITE_SETS = [S0, S7, S0_2, S0_7];
|
|
58
|
+
const S63 = new Set([63]);
|
|
59
|
+
const S56 = new Set([56]);
|
|
60
|
+
const S56_63 = new Set([56, 63]);
|
|
61
|
+
const BLACK_SETS = [S0, S63, S56, S56_63];
|
|
62
|
+
/** Precomputed 16-entry CastlingRights table for standard chess (zero allocations) */
|
|
63
|
+
export const CASTLING_RIGHTS_TABLE = (() => {
|
|
64
|
+
const table = [];
|
|
65
|
+
for (let mask = 0; mask < 16; mask++) {
|
|
66
|
+
table.push({
|
|
67
|
+
white: WHITE_SETS[mask & 3],
|
|
68
|
+
black: BLACK_SETS[(mask >> 2) & 3],
|
|
69
|
+
whiteKing: (mask & CASTLE_WK) !== 0,
|
|
70
|
+
whiteQueen: (mask & CASTLE_WQ) !== 0,
|
|
71
|
+
blackKing: (mask & CASTLE_BK) !== 0,
|
|
72
|
+
blackQueen: (mask & CASTLE_BQ) !== 0,
|
|
73
|
+
mask,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return table;
|
|
77
|
+
})();
|
|
78
|
+
export function getCastlingMask(pos) {
|
|
79
|
+
if (pos.castlingMask !== undefined)
|
|
80
|
+
return pos.castlingMask;
|
|
81
|
+
let mask = 0;
|
|
82
|
+
if (pos.castling.white.has(7))
|
|
83
|
+
mask |= CASTLE_WK;
|
|
84
|
+
if (pos.castling.white.has(0))
|
|
85
|
+
mask |= CASTLE_WQ;
|
|
86
|
+
if (pos.castling.black.has(63))
|
|
87
|
+
mask |= CASTLE_BK;
|
|
88
|
+
if (pos.castling.black.has(56))
|
|
89
|
+
mask |= CASTLE_BQ;
|
|
90
|
+
return mask;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Precomputed 128-entry tables for rank-0 (White: 0..63) and rank-7 (Black: 64..127)
|
|
94
|
+
* castling path clearance in Chess960 and standard chess.
|
|
95
|
+
* Single-cycle check: ((CASTLE_PATH_LO[idx] & occ.lo) | (CASTLE_PATH_HI[idx] & occ.hi)) === 0
|
|
96
|
+
*/
|
|
97
|
+
export const CASTLE_PATH_LO = new Uint32Array(128);
|
|
98
|
+
export const CASTLE_PATH_HI = new Uint32Array(128);
|
|
99
|
+
export const CASTLE_TRAVERSAL_WHITE = new Array(64);
|
|
100
|
+
export const CASTLE_TRAVERSAL_BLACK = new Array(64);
|
|
101
|
+
for (let kFile = 0; kFile < 8; kFile++) {
|
|
102
|
+
for (let rFile = 0; rFile < 8; rFile++) {
|
|
103
|
+
const idx = (kFile << 3) | rFile;
|
|
104
|
+
if (kFile === rFile) {
|
|
105
|
+
CASTLE_TRAVERSAL_WHITE[idx] = [];
|
|
106
|
+
CASTLE_TRAVERSAL_BLACK[idx] = [];
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const isKingSide = rFile > kFile;
|
|
110
|
+
const kLanding = isKingSide ? 6 : 2;
|
|
111
|
+
const rLanding = isKingSide ? 5 : 3;
|
|
112
|
+
// Path clearance: all squares between king/rook and their landings must be empty,
|
|
113
|
+
// excluding initial king and rook squares.
|
|
114
|
+
let rank0Mask = 0;
|
|
115
|
+
const minK = Math.min(kFile, kLanding);
|
|
116
|
+
const maxK = Math.max(kFile, kLanding);
|
|
117
|
+
const minR = Math.min(rFile, rLanding);
|
|
118
|
+
const maxR = Math.max(rFile, rLanding);
|
|
119
|
+
for (let f = 0; f < 8; f++) {
|
|
120
|
+
if (f === kFile || f === rFile)
|
|
121
|
+
continue;
|
|
122
|
+
if ((f >= minK && f <= maxK) || (f >= minR && f <= maxR)) {
|
|
123
|
+
rank0Mask |= (1 << f) >>> 0;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// White (index 0..63): rank 0 in lo
|
|
127
|
+
CASTLE_PATH_LO[idx] = rank0Mask >>> 0;
|
|
128
|
+
CASTLE_PATH_HI[idx] = 0;
|
|
129
|
+
// Black (index 64..127): rank 7 in hi (shifted by 24)
|
|
130
|
+
CASTLE_PATH_LO[64 + idx] = 0;
|
|
131
|
+
CASTLE_PATH_HI[64 + idx] = (rank0Mask << 24) >>> 0;
|
|
132
|
+
// Traversal squares: squares king traverses towards landing (excluding kFile)
|
|
133
|
+
const travW = [];
|
|
134
|
+
const travB = [];
|
|
135
|
+
if (kLanding !== kFile) {
|
|
136
|
+
const step = kLanding > kFile ? 1 : -1;
|
|
137
|
+
for (let f = kFile + step;; f += step) {
|
|
138
|
+
travW.push(f);
|
|
139
|
+
travB.push(56 + f);
|
|
140
|
+
if (f === kLanding)
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
CASTLE_TRAVERSAL_WHITE[idx] = travW;
|
|
145
|
+
CASTLE_TRAVERSAL_BLACK[idx] = travB;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export function castlingPlanFor(color, ks, rs) {
|
|
149
|
+
const rank = squareRank(ks);
|
|
150
|
+
const isKingSide = squareFile(rs) > squareFile(ks);
|
|
151
|
+
return {
|
|
152
|
+
side: isKingSide ? "king" : "queen",
|
|
153
|
+
kingFrom: ks,
|
|
154
|
+
kingTo: isKingSide ? (rank << 3) | 6 : (rank << 3) | 2,
|
|
155
|
+
rookFrom: rs,
|
|
156
|
+
rookTo: isKingSide ? (rank << 3) | 5 : (rank << 3) | 3,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
export function planForDest(plans, to) {
|
|
160
|
+
for (let i = 0; i < plans.length; i++) {
|
|
161
|
+
const p = plans[i];
|
|
162
|
+
if (p.rookFrom === to || p.kingTo === to)
|
|
163
|
+
return p;
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
function pieceRoleAndColorAt(b, sqIdx) {
|
|
168
|
+
const bit = sqIdx < 32 ? (1 << sqIdx) >>> 0 : (1 << (sqIdx - 32)) >>> 0;
|
|
169
|
+
const occ = sqIdx < 32 ? b.occupied.lo : b.occupied.hi;
|
|
170
|
+
if (((occ & bit) >>> 0) === 0)
|
|
171
|
+
return undefined;
|
|
172
|
+
const whiteW = sqIdx < 32 ? b.white.lo : b.white.hi;
|
|
173
|
+
const color = ((whiteW & bit) >>> 0) !== 0 ? Color.White : Color.Black;
|
|
174
|
+
const rookW = sqIdx < 32 ? b.rook.lo : b.rook.hi;
|
|
175
|
+
if (((rookW & bit) >>> 0) !== 0)
|
|
176
|
+
return { color, role: Role.Rook };
|
|
177
|
+
const kingW = sqIdx < 32 ? b.king.lo : b.king.hi;
|
|
178
|
+
if (((kingW & bit) >>> 0) !== 0)
|
|
179
|
+
return { color, role: Role.King };
|
|
180
|
+
return { color, role: Role.Pawn };
|
|
181
|
+
}
|
|
182
|
+
export function detectCastling(pos, from, to) {
|
|
183
|
+
if (!pos.isChess960) {
|
|
184
|
+
const mask = pos.castlingMask ?? getCastlingMask(pos);
|
|
185
|
+
if (mask === 0)
|
|
186
|
+
return null;
|
|
187
|
+
if (pos.turn === Color.White) {
|
|
188
|
+
if ((mask & (CASTLE_WK | CASTLE_WQ)) === 0 || from !== 4)
|
|
189
|
+
return null;
|
|
190
|
+
if (to === 7 || to === 6) {
|
|
191
|
+
if ((mask & CASTLE_WK) === 0)
|
|
192
|
+
return null;
|
|
193
|
+
if (to === 6 && pieceRoleAndColorAt(pos.board, 6))
|
|
194
|
+
return null;
|
|
195
|
+
const rp = pieceRoleAndColorAt(pos.board, 7);
|
|
196
|
+
if (rp && rp.color === Color.White && rp.role === Role.Rook)
|
|
197
|
+
return PLAN_WHITE_K;
|
|
198
|
+
}
|
|
199
|
+
else if (to === 0 || to === 2) {
|
|
200
|
+
if ((mask & CASTLE_WQ) === 0)
|
|
201
|
+
return null;
|
|
202
|
+
if (to === 2 && pieceRoleAndColorAt(pos.board, 2))
|
|
203
|
+
return null;
|
|
204
|
+
const rp = pieceRoleAndColorAt(pos.board, 0);
|
|
205
|
+
if (rp && rp.color === Color.White && rp.role === Role.Rook)
|
|
206
|
+
return PLAN_WHITE_Q;
|
|
207
|
+
}
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
if ((mask & (CASTLE_BK | CASTLE_BQ)) === 0 || from !== 60)
|
|
212
|
+
return null;
|
|
213
|
+
if (to === 63 || to === 62) {
|
|
214
|
+
if ((mask & CASTLE_BK) === 0)
|
|
215
|
+
return null;
|
|
216
|
+
if (to === 62 && pieceRoleAndColorAt(pos.board, 62))
|
|
217
|
+
return null;
|
|
218
|
+
const rp = pieceRoleAndColorAt(pos.board, 63);
|
|
219
|
+
if (rp && rp.color === Color.Black && rp.role === Role.Rook)
|
|
220
|
+
return PLAN_BLACK_K;
|
|
221
|
+
}
|
|
222
|
+
else if (to === 56 || to === 58) {
|
|
223
|
+
if ((mask & CASTLE_BQ) === 0)
|
|
224
|
+
return null;
|
|
225
|
+
if (to === 58 && pieceRoleAndColorAt(pos.board, 58))
|
|
226
|
+
return null;
|
|
227
|
+
const rp = pieceRoleAndColorAt(pos.board, 56);
|
|
228
|
+
if (rp && rp.color === Color.Black && rp.role === Role.Rook)
|
|
229
|
+
return PLAN_BLACK_Q;
|
|
230
|
+
}
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// Chess960 fallback
|
|
235
|
+
if (pos.castling.white.size === 0 && pos.castling.black.size === 0)
|
|
236
|
+
return null;
|
|
237
|
+
const piece = pieceRoleAndColorAt(pos.board, from);
|
|
238
|
+
if (!piece || piece.role !== Role.King || piece.color !== pos.turn)
|
|
239
|
+
return null;
|
|
240
|
+
const rights = piece.color === Color.White ? pos.castling.white : pos.castling.black;
|
|
241
|
+
if (rights.size === 0)
|
|
242
|
+
return null;
|
|
243
|
+
const rank = squareRank(from);
|
|
244
|
+
// Input form 1: king captures own rook on its origin square (baseline/960).
|
|
245
|
+
if (rights.has(to) && squareRank(to) === rank) {
|
|
246
|
+
const target = pieceRoleAndColorAt(pos.board, to);
|
|
247
|
+
if (target && target.color === piece.color && target.role === Role.Rook) {
|
|
248
|
+
return castlingPlanFor(piece.color, from, to);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
// Input form 2: normalized landing square — a two-file step on the same rank.
|
|
252
|
+
if (squareRank(to) === rank && Math.abs(squareFile(to) - squareFile(from)) === 2) {
|
|
253
|
+
// The landing square must be empty for castling.
|
|
254
|
+
if (!pieceRoleAndColorAt(pos.board, to)) {
|
|
255
|
+
for (const rs of rights) {
|
|
256
|
+
const plan = castlingPlanFor(piece.color, from, rs);
|
|
257
|
+
if (plan.kingTo !== to)
|
|
258
|
+
continue;
|
|
259
|
+
// The right's rook must actually be present on its origin square.
|
|
260
|
+
const rp = pieceRoleAndColorAt(pos.board, rs);
|
|
261
|
+
if (rp && rp.color === piece.color && rp.role === Role.Rook)
|
|
262
|
+
return plan;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return null;
|
|
267
|
+
}
|
package/dist/chess.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SquareSet } from "./squareSet.js";
|
|
2
|
+
import { Board, type Undo as BoardUndo } from "./board.js";
|
|
2
3
|
import { Color, Role } from "./types.js";
|
|
3
4
|
import type { Setup, Move, Position } from "./types.js";
|
|
4
5
|
import type { ZobristKey } from "./zobrist.js";
|
|
@@ -10,26 +11,10 @@ export declare function isFiftyMoveDraw(pos: Position): boolean;
|
|
|
10
11
|
export declare function isSeventyFiveMoveDraw(pos: Position): boolean;
|
|
11
12
|
export declare function isThreefoldRepetition(history: Position[]): boolean;
|
|
12
13
|
export declare function isFivefoldRepetition(history: Position[]): boolean;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
rookFrom: number;
|
|
18
|
-
rookTo: number;
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Single source of truth for castling detection, shared by makeMove, isLegal,
|
|
22
|
-
* makeSan, destsFast and genLegalMoves (design D2). Accepts both input
|
|
23
|
-
* representations:
|
|
24
|
-
* - king→own-rook square (baseline / 960 input, e1h1), and
|
|
25
|
-
* - king→normalized landing square (e1g1; a two-file king step),
|
|
26
|
-
* provided the corresponding castling right exists and the rook is actually
|
|
27
|
-
* on its origin square. Returns null for every non-castling move — in
|
|
28
|
-
* particular for an ordinary one-square king step to g1/c1/g8/c8, which the
|
|
29
|
-
* previous per-call-site heuristics misclassified when ANY color (even the
|
|
30
|
-
* opponent) held castling rights (root cause of the perft parity defect).
|
|
31
|
-
*/
|
|
32
|
-
export declare function detectCastling(pos: Position, from: number, to: number): CastlingPlan | null;
|
|
14
|
+
import { detectCastling, castlingPlanFor, planForDest } from "./castling.js";
|
|
15
|
+
import type { CastlingPlan } from "./castling.js";
|
|
16
|
+
export { detectCastling, castlingPlanFor, planForDest, };
|
|
17
|
+
export type { CastlingPlan };
|
|
33
18
|
export declare function makeMove(pos: Position, move: Move): Position;
|
|
34
19
|
export declare const play: typeof makeMove;
|
|
35
20
|
type CheckContext = {
|
|
@@ -40,8 +25,8 @@ type CheckContext = {
|
|
|
40
25
|
checkMask: SquareSet;
|
|
41
26
|
/** squares the king may move to (not attacked with our king removed from occupancy) */
|
|
42
27
|
kingSafe: SquareSet;
|
|
43
|
-
/** pinned own
|
|
44
|
-
|
|
28
|
+
/** pinned own pieces bitmask */
|
|
29
|
+
pinned: SquareSet;
|
|
45
30
|
/**
|
|
46
31
|
* Castling plans for the side to move (empty when no rights / kingless),
|
|
47
32
|
* computed once per position so the hot loops never re-run detectCastling
|
|
@@ -65,6 +50,15 @@ export declare function isLegal(pos: Position, move: Move): boolean;
|
|
|
65
50
|
export declare function isCheckmate(pos: Position): boolean;
|
|
66
51
|
export declare function isStalemate(pos: Position): boolean;
|
|
67
52
|
export declare function perft(pos: Position, depth: number): number;
|
|
53
|
+
export declare class MoveCounter {
|
|
54
|
+
count: number;
|
|
55
|
+
add(set: SquareSet): void;
|
|
56
|
+
addPawns(legal: SquareSet, isWhite: boolean): void;
|
|
57
|
+
reset(): void;
|
|
58
|
+
}
|
|
59
|
+
export declare function countLegalMoves(pos: Position): number;
|
|
60
|
+
export declare function legalMovesInto(pos: Position, out: Uint16Array | Uint32Array): number;
|
|
61
|
+
export declare function forEachLegalMove(pos: Position, fn: (from: number, to: number, promo: number) => void): void;
|
|
68
62
|
export declare const INITIAL_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
|
69
63
|
/** Baseline-style color name. */
|
|
70
64
|
export type ColorName = "w" | "b";
|
|
@@ -84,18 +78,23 @@ export type VerboseMove = {
|
|
|
84
78
|
before: string;
|
|
85
79
|
after: string;
|
|
86
80
|
};
|
|
87
|
-
type HistoryEntry = {
|
|
88
|
-
before: Position;
|
|
89
|
-
after: Position;
|
|
90
|
-
move: Move;
|
|
91
|
-
san: string;
|
|
81
|
+
export type HistoryEntry = {
|
|
82
|
+
readonly before: Position;
|
|
83
|
+
readonly after: Position;
|
|
84
|
+
readonly move: Move;
|
|
85
|
+
readonly san: string;
|
|
86
|
+
readonly boardUndo?: BoardUndo;
|
|
87
|
+
readonly prev_checkers?: SquareSet;
|
|
88
|
+
readonly prev_zobrist?: ZobristKey;
|
|
92
89
|
};
|
|
90
|
+
export type { BoardUndo as Undo };
|
|
91
|
+
export { Board };
|
|
93
92
|
/**
|
|
94
93
|
* Unified root `Chess` (design D1): the chess.js-ergonomic mutable façade
|
|
95
94
|
* over the immutable engine core, extended with the high-performance
|
|
96
95
|
* bitboard surface, zero-alloc Zobrist hashing and 16-bit packed moves2, and
|
|
97
96
|
* native tree navigation. Lives at the ROOT entrypoint so the tree-shakeable
|
|
98
|
-
* `
|
|
97
|
+
* `gigachess/core` graph stays free of FEN/PGN/tree facade code.
|
|
99
98
|
*/
|
|
100
99
|
export declare class Chess {
|
|
101
100
|
#private;
|
|
@@ -143,6 +142,7 @@ export declare class Chess {
|
|
|
143
142
|
/** Reverts the last move; returns it in verbose shape, or null if none. */
|
|
144
143
|
undo(): VerboseMove | null;
|
|
145
144
|
isCheck(): boolean;
|
|
145
|
+
inCheck(): boolean;
|
|
146
146
|
isCheckmate(): boolean;
|
|
147
147
|
isStalemate(): boolean;
|
|
148
148
|
/** Draw by insufficient material, the fifty-move rule, or threefold repetition. */
|
|
@@ -198,6 +198,8 @@ export declare class Chess {
|
|
|
198
198
|
get startFen(): string;
|
|
199
199
|
/** Live engine position (read-only view over the internal state). */
|
|
200
200
|
get pos(): Position;
|
|
201
|
+
get _pos(): Position;
|
|
202
|
+
get boardInstance(): Board;
|
|
201
203
|
/** Internal history log (read-only view; used by toTree). */
|
|
202
204
|
get historyEntries(): readonly HistoryEntry[];
|
|
203
205
|
/**
|
package/dist/chess.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chess.d.ts","sourceRoot":"","sources":["../src/chess.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"chess.d.ts","sourceRoot":"","sources":["../src/chess.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,KAAK,EAAE,KAAK,IAAI,IAAI,SAAS,EAAE,MAAM,YAAY,CAAC;AAE3D,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,EAAE,KAAK,EAAE,IAAI,EAA0B,QAAQ,EAAE,MAAM,YAAY,C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