gigachess 0.6.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.
Files changed (87) hide show
  1. package/README.md +82 -49
  2. package/dist/attacks.d.ts +7 -4
  3. package/dist/attacks.d.ts.map +1 -1
  4. package/dist/attacks.js +36 -15
  5. package/dist/chess960.d.ts +2 -9
  6. package/dist/chess960.d.ts.map +1 -1
  7. package/dist/chess960.js +2 -18
  8. package/dist/chessjs.d.ts +1 -1
  9. package/dist/chessjs.d.ts.map +1 -1
  10. package/dist/chessjs.js +1 -1
  11. package/dist/chessops/board.d.ts +1 -1
  12. package/dist/chessops/board.d.ts.map +1 -1
  13. package/dist/chessops/board.js +1 -1
  14. package/dist/chessops/chess.d.ts +6 -10
  15. package/dist/chessops/chess.d.ts.map +1 -1
  16. package/dist/chessops/chess.js +73 -97
  17. package/dist/chessops/convert.d.ts +1 -1
  18. package/dist/chessops/convert.d.ts.map +1 -1
  19. package/dist/chessops/convert.js +1 -1
  20. package/dist/chessops/debug.d.ts.map +1 -1
  21. package/dist/chessops/debug.js +7 -0
  22. package/dist/chessops/fen.d.ts.map +1 -1
  23. package/dist/chessops/fen.js +13 -2
  24. package/dist/chessops/fenInternal.d.ts +1 -1
  25. package/dist/chessops/fenInternal.d.ts.map +1 -1
  26. package/dist/chessops/fenInternal.js +1 -1
  27. package/dist/chessops/nativeBridge.d.ts +14 -0
  28. package/dist/chessops/nativeBridge.d.ts.map +1 -0
  29. package/dist/chessops/nativeBridge.js +87 -0
  30. package/dist/chessops/san.d.ts.map +1 -1
  31. package/dist/chessops/san.js +12 -10
  32. package/dist/chesstree.d.ts.map +1 -1
  33. package/dist/chesstree.js +23 -62
  34. package/dist/core.d.ts +6 -8
  35. package/dist/core.d.ts.map +1 -1
  36. package/dist/core.js +5 -10
  37. package/dist/fast/index.js +1 -1
  38. package/dist/fast.d.ts +1 -11
  39. package/dist/fast.d.ts.map +1 -1
  40. package/dist/fast.js +2 -11
  41. package/dist/index.d.ts +1 -14
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +2 -19
  44. package/dist/native96.d.ts +16 -0
  45. package/dist/native96.d.ts.map +1 -0
  46. package/dist/native96.js +6 -0
  47. package/dist/nativeBoard96.d.ts +118 -0
  48. package/dist/nativeBoard96.d.ts.map +1 -0
  49. package/dist/nativeBoard96.js +1077 -0
  50. package/dist/nativeChess.d.ts +97 -0
  51. package/dist/nativeChess.d.ts.map +1 -0
  52. package/dist/nativeChess.js +201 -0
  53. package/dist/nativeMovegen96.d.ts +2 -0
  54. package/dist/nativeMovegen96.d.ts.map +1 -0
  55. package/dist/{standardMovegen.js → nativeMovegen96.js} +77 -48
  56. package/dist/nativePublicTypes.d.ts +3 -0
  57. package/dist/nativePublicTypes.d.ts.map +1 -0
  58. package/dist/nativePublicTypes.js +1 -0
  59. package/dist/perftCompact.d.ts +7 -0
  60. package/dist/perftCompact.d.ts.map +1 -1
  61. package/dist/perftCompact.js +124 -62
  62. package/dist/pgn.d.ts +2 -3
  63. package/dist/pgn.d.ts.map +1 -1
  64. package/dist/pgn.js +2 -5
  65. package/dist/setupBoard.d.ts +54 -0
  66. package/dist/setupBoard.d.ts.map +1 -0
  67. package/dist/setupBoard.js +249 -0
  68. package/dist/{fen.d.ts → setupFen.d.ts} +1 -1
  69. package/dist/setupFen.d.ts.map +1 -0
  70. package/dist/{fen.js → setupFen.js} +1 -1
  71. package/dist/types.d.ts +1 -1
  72. package/dist/types.d.ts.map +1 -1
  73. package/dist/zobrist.d.ts +2 -2
  74. package/dist/zobrist.d.ts.map +1 -1
  75. package/package.json +10 -7
  76. package/dist/board.d.ts +0 -242
  77. package/dist/board.d.ts.map +0 -1
  78. package/dist/board.js +0 -1403
  79. package/dist/chess.d.ts +0 -259
  80. package/dist/chess.d.ts.map +0 -1
  81. package/dist/chess.js +0 -2314
  82. package/dist/fen.d.ts.map +0 -1
  83. package/dist/san.d.ts +0 -38
  84. package/dist/san.d.ts.map +0 -1
  85. package/dist/san.js +0 -523
  86. package/dist/standardMovegen.d.ts +0 -3
  87. package/dist/standardMovegen.d.ts.map +0 -1
package/dist/fen.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"fen.d.ts","sourceRoot":"","sources":["../src/fen.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,KAAK,EAAkB,QAAQ,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AA0F1E,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,CAyR9G;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,MAAM,CAsF/F"}
package/dist/san.d.ts DELETED
@@ -1,38 +0,0 @@
1
- import type { Position, Move, Result, SanError, UciError } from "./types.js";
2
- /**
3
- * The null move is a first-class pass, not a placeholder alias: the only SAN
4
- * tokens that denote one are `--` (the canonical rendering) and `Z0` (the
5
- * moves2/FEN pass token). `null`, `pass` and every other spelling are rejected
6
- * by `parseSan`.
7
- */
8
- export declare const NULL_MOVE_SANS: readonly string[];
9
- /** True when `san` is one of the two accepted null-move tokens. */
10
- export declare function isNullMoveSan(san: string): boolean;
11
- export declare function makeSan(move: Move, pos: Position): string;
12
- export declare function parseSan(san: string, pos: Position): Result<Move, SanError>;
13
- /**
14
- * `parseSan` with the matching moves retained.
15
- *
16
- * A multi-match parse ends in `san/ambiguous` and `parseSan` throws the matches
17
- * away, so a caller can report *that* the input was ambiguous but never *what*
18
- * it could have meant. `candidates` carries that list so a UI can name the
19
- * disambiguated spellings instead of guessing (BlindBase ADR-042, report #11).
20
- *
21
- * `result` is exactly what `parseSan` returns for the same input; this exists so
22
- * the two cannot drift, not as a second parse path.
23
- */
24
- export declare function parseSanDetailed(san: string, pos: Position): {
25
- result: Result<Move, SanError>;
26
- candidates: Move[] | null;
27
- };
28
- /**
29
- * Every legal move `san` could denote, or the error when it names none.
30
- *
31
- * The counterpart to `parseSan` for callers that must react to ambiguity rather
32
- * than accept it: `length === 1` means resolved, `length > 1` means the caller
33
- * should ask which of these it meant.
34
- */
35
- export declare function parseSanCandidates(san: string, pos: Position): Result<Move[], SanError>;
36
- export declare function parseUci(uci: string): Result<Move, UciError>;
37
- export declare function makeUci(move: Move): string;
38
- //# sourceMappingURL=san.d.ts.map
package/dist/san.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"san.d.ts","sourceRoot":"","sources":["../src/san.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAQ7E;;;;;GAKG;AACH,eAAO,MAAM,cAAc,EAAE,SAAS,MAAM,EAA0B,CAAC;AAGvE,mEAAmE;AACnE,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAElD;AAYD,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,GAAG,MAAM,CAsIzD;AAGD,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAE3E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,QAAQ,GACZ;IAAE,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IAAC,UAAU,EAAE,IAAI,EAAE,GAAG,IAAI,CAAA;CAAE,CAc/D;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,MAAM,CAAC,IAAI,EAAE,EAAE,QAAQ,CAAC,CAMvF;AAuND,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CA2B5D;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAmB1C"}
package/dist/san.js DELETED
@@ -1,523 +0,0 @@
1
- // src/san.ts — parseSan/makeSan, parseUci/makeUci per purechess-rules
2
- // Clean-room from spec tables + FIDE notes, no G P L
3
- import * as sq from "./squareSet.js";
4
- import * as board from "./board.js";
5
- import * as chess from "./chess.js";
6
- import * as attacks from "./attacks.js";
7
- import { Color, Role } from "./types.js";
8
- import { Err, Ok } from "./types.js";
9
- import { squareFile, squareRank, parseSquare, squareName } from "./util.js";
10
- import { isNullMove, nullMove, NULL_MOVE_SAN, NULL_MOVE_UCI } from "./packedMove.js";
11
- const ROLE_SAN = ["", "N", "B", "R", "Q", "K"];
12
- const roleToSanChar = (role) => ROLE_SAN[role] ?? "";
13
- /**
14
- * The null move is a first-class pass, not a placeholder alias: the only SAN
15
- * tokens that denote one are `--` (the canonical rendering) and `Z0` (the
16
- * moves2/FEN pass token). `null`, `pass` and every other spelling are rejected
17
- * by `parseSan`.
18
- */
19
- export const NULL_MOVE_SANS = [NULL_MOVE_SAN, "Z0"];
20
- const NULL_MOVE_SAN_SET = new Set(NULL_MOVE_SANS);
21
- /** True when `san` is one of the two accepted null-move tokens. */
22
- export function isNullMoveSan(san) {
23
- return NULL_MOVE_SAN_SET.has(san);
24
- }
25
- const SAN_ROLES = { K: Role.King, Q: Role.Queen, R: Role.Rook, B: Role.Bishop, N: Role.Knight };
26
- const sanCharToRole = (ch) => SAN_ROLES[ch];
27
- const promoCharToRole = (ch) => SAN_ROLES[ch.toUpperCase()];
28
- function normalizeCastlingSan(s) {
29
- // tolerant 0-0 -> O-O
30
- return s.replace(/0-0-0/g, "O-O-O").replace(/0-0/g, "O-O");
31
- }
32
- // ---------- makeSan ----------
33
- export function makeSan(move, pos) {
34
- // A pass renders as exactly `--` and returns here: it is never suffixed with
35
- // `+` or `#` (a pass can neither give nor answer check), and it has no
36
- // origin square for the castling/disambiguation paths below.
37
- if (isNullMove(move))
38
- return NULL_MOVE_SAN;
39
- // Castling — detected via the shared detectCastling path (design D2) so a
40
- // canonical castling move given in the representation's encoding renders
41
- // O-O/O-O-O, never "Kg1"/"Kxh1" (measured defect: makeSan({4,6}, kiwipete)
42
- // returned "Kg1"). The isCastling flag is kept as a fallback for moves
43
- // produced by genLegalMoves under either representation. The role/rights
44
- // pre-check keeps the hot SAN path free of detectCastling calls.
45
- let castling = null;
46
- const movingPiece = board.pieceAt(pos.board, move.from);
47
- if (movingPiece && movingPiece.role === Role.King && (pos.castling.white.size > 0 || pos.castling.black.size > 0)) {
48
- castling = chess.detectCastling(pos, move.from, move.to);
49
- }
50
- if (castling || move.isCastling) {
51
- const isKingSide = castling ? castling.side === "king" : move.to === 6 || move.to === 62;
52
- let base = isKingSide ? "O-O" : "O-O-O";
53
- // add check suffix
54
- const next = chess.makeMove(pos, move);
55
- if (chess.isCheck(next)) {
56
- if (chess.isCheckmate(next))
57
- base += "#";
58
- else
59
- base += "+";
60
- }
61
- return base;
62
- }
63
- const piece = movingPiece;
64
- if (!piece)
65
- return "";
66
- const toName = squareName(move.to);
67
- const isCapture = (() => {
68
- const target = board.pieceAt(pos.board, move.to);
69
- if (target && target.color !== pos.turn)
70
- return true;
71
- if (move.isEnPassant)
72
- return true;
73
- // en-passant capture onto the empty ep square (baseline output parity):
74
- // a pawn moving DIAGONALLY onto the ep square is a capture even when the
75
- // caller did not set the isEnPassant flag (external move objects). A
76
- // straight push to the ep square remains a quiet move.
77
- if (piece.role === Role.Pawn &&
78
- pos.epSquare !== null &&
79
- move.to === pos.epSquare &&
80
- squareFile(move.from) !== squareFile(move.to)) {
81
- return true;
82
- }
83
- return false;
84
- })();
85
- const destRank = squareRank(move.to);
86
- const isPromotion = !!move.isPromotion || (piece.role === Role.Pawn && (destRank === 7 || destRank === 0));
87
- // Piece letter
88
- let san = "";
89
- if (piece.role !== Role.Pawn) {
90
- san += roleToSanChar(piece.role);
91
- }
92
- else if (isCapture) {
93
- // pawn capture needs file of from
94
- san += String.fromCharCode(97 + squareFile(move.from));
95
- }
96
- // Disambiguation for non-pawn (and pawn capture already handled)
97
- if (piece.role !== Role.Pawn) {
98
- // Legal alternatives determine the shortest SAN disambiguation.
99
- // Track only the facts needed for formatting; no candidate array.
100
- const own = pos.turn === Color.White ? pos.board.white : pos.board.black;
101
- let attackers;
102
- switch (piece.role) {
103
- case Role.Knight:
104
- attackers = sq.and(attacks.knightAttacks(move.to), sq.and(own, pos.board.knight));
105
- break;
106
- case Role.Bishop:
107
- attackers = sq.and(attacks.bishopAttacks(move.to, pos.board.occupied), sq.and(own, pos.board.bishop));
108
- break;
109
- case Role.Rook:
110
- attackers = sq.and(attacks.rookAttacks(move.to, pos.board.occupied), sq.and(own, pos.board.rook));
111
- break;
112
- case Role.Queen:
113
- attackers = sq.and(attacks.queenAttacks(move.to, pos.board.occupied), sq.and(own, pos.board.queen));
114
- break;
115
- case Role.King:
116
- attackers = sq.and(attacks.kingAttacks(move.to), sq.and(own, pos.board.king));
117
- break;
118
- default:
119
- attackers = sq.EMPTY;
120
- break;
121
- }
122
- const otherAttackers = sq.minus(attackers, sq.singleton(move.from));
123
- if (!sq.isEmpty(otherAttackers)) {
124
- const ctx = chess.analyzeCheckContext(pos);
125
- const movingFile = squareFile(move.from), movingRank = squareRank(move.from);
126
- let ambiguous = false, sameFile = false, sameRank = false;
127
- for (const sqIdx of sq.iter(otherAttackers)) {
128
- const cand = board.pieceAt(pos.board, sqIdx);
129
- if (!cand || !sq.has(chess.destsFast(pos, sqIdx, cand, ctx), move.to))
130
- continue;
131
- ambiguous = true;
132
- sameFile || (sameFile = squareFile(sqIdx) === movingFile);
133
- sameRank || (sameRank = squareRank(sqIdx) === movingRank);
134
- if (sameFile && sameRank)
135
- break;
136
- }
137
- if (ambiguous) {
138
- if (!sameFile || sameRank)
139
- san += String.fromCharCode(97 + movingFile);
140
- if (sameFile)
141
- san += String.fromCharCode(49 + movingRank);
142
- }
143
- }
144
- }
145
- // Capture
146
- if (isCapture) {
147
- // For pawn, we already added file, need 'x'
148
- // For pieces, disambiguation already in san, now add 'x'
149
- san += "x";
150
- }
151
- // Destination
152
- // For pawn capture we already have file preceding 'x', need dest; for non-pawn we need dest after optional disamb and 'x'
153
- // Our san currently for pawn capture is like "e" + "x" => "ex", then dest
154
- // For non-pawn, san is piece char + disamb + 'x' if capture, then dest
155
- // So append dest
156
- // But for non-pawn capture case, we already have piece+disamb, now need to ensure we haven't duplicated file for pawn
157
- // For pawn non-capture, san is "" currently, just dest
158
- // For pawn capture, san is "ex" already, need dest
159
- // For pieces, san currently is piece+disamb (+x if capture) => need dest
160
- // However for pieces, if we added disamb file/rank, san already has it, so just add dest
161
- // Implementation above for pieces: san = piece char + disamb (file or rank or both) . For pawn capture, san = file + 'x' ? Wait we added file for pawn at start, then for disamb we skip for pawn, then capture adds 'x'. So for pawn capture "exd5": piece pawn => san initially "e" (file), then disamb skipped, then isCapture adds "x" => "ex", then dest "d5" => "exd5" correct.
162
- // For pawn non-capture "e4": san "" initially, no disamb, no capture, just dest "e4"
163
- // For piece "Nbd2": piece N, candidates determine file unique => san = "N" + "b" => "Nb", no capture, then dest "d2" => "Nbd2"
164
- // For "Qh4e1": piece Q, disamb "h4", san="Qh4", dest "e1" => "Qh4e1"
165
- san += toName;
166
- // Promotion
167
- if (isPromotion) {
168
- const promo = move.promotion ?? Role.Queen;
169
- san += "=" + roleToSanChar(promo);
170
- }
171
- // Check / mate suffix
172
- const next = chess.makeMove(pos, move);
173
- if (chess.isCheck(next)) {
174
- if (chess.isCheckmate(next))
175
- san += "#";
176
- else
177
- san += "+";
178
- }
179
- return san;
180
- }
181
- // ---------- parseSan ----------
182
- export function parseSan(san, pos) {
183
- return parseSanDetailed(san, pos).result;
184
- }
185
- /**
186
- * `parseSan` with the matching moves retained.
187
- *
188
- * A multi-match parse ends in `san/ambiguous` and `parseSan` throws the matches
189
- * away, so a caller can report *that* the input was ambiguous but never *what*
190
- * it could have meant. `candidates` carries that list so a UI can name the
191
- * disambiguated spellings instead of guessing (BlindBase ADR-042, report #11).
192
- *
193
- * `result` is exactly what `parseSan` returns for the same input; this exists so
194
- * the two cannot drift, not as a second parse path.
195
- */
196
- export function parseSanDetailed(san, pos) {
197
- const traced = parseSanTraced(san, pos);
198
- const candidates = traced.candidates;
199
- if (candidates === null) {
200
- // Rejected before any move could match — nothing to disambiguate.
201
- return { result: traced.result, candidates: null };
202
- }
203
- if (candidates.length === 0) {
204
- return { result: Err({ code: "san/noLegalMove" }), candidates };
205
- }
206
- if (candidates.length > 1) {
207
- return { result: Err({ code: "san/ambiguous" }), candidates };
208
- }
209
- return { result: Ok(candidates[0]), candidates };
210
- }
211
- /**
212
- * Every legal move `san` could denote, or the error when it names none.
213
- *
214
- * The counterpart to `parseSan` for callers that must react to ambiguity rather
215
- * than accept it: `length === 1` means resolved, `length > 1` means the caller
216
- * should ask which of these it meant.
217
- */
218
- export function parseSanCandidates(san, pos) {
219
- const { result, candidates } = parseSanDetailed(san, pos);
220
- if (candidates === null || candidates.length === 0) {
221
- return result.ok ? Ok([result.value]) : Err(result.error);
222
- }
223
- return Ok(candidates);
224
- }
225
- /**
226
- * The parse body, keeping the collected candidates.
227
- *
228
- * Every early refusal before candidate collection returns `candidates: null`,
229
- * which is what distinguishes "this string is not a SAN at all" from "this SAN
230
- * is ambiguous" — a distinction `parseSan`'s single error channel previously
231
- * lost.
232
- */
233
- function parseSanTraced(san, pos) {
234
- // A refusal raised before any move could match: nothing to disambiguate, which
235
- // is what separates "not a SAN at all" from "an ambiguous SAN".
236
- const fail = (code) => ({ result: Err({ code }), candidates: null });
237
- const orig = san;
238
- let s = normalizeCastlingSan(san.trim());
239
- // Null move (pass) — exactly `--` or `Z0`, matched BEFORE any check-suffix
240
- // handling so a suffixed spelling (`--+`) is not silently accepted. A pass is
241
- // legal iff the side to move is not in check; the test is computed fresh from
242
- // the attack tables, never from the cached `checkers` bitboard.
243
- if (isNullMoveSan(s)) {
244
- if (!chess.isNullMoveLegal(pos))
245
- return fail("san/illegal");
246
- return { result: Ok(nullMove()), candidates: [nullMove()] };
247
- }
248
- // Handle check suffix for later, but keep for validation
249
- let checkSuffix = "";
250
- if (s.endsWith("+") || s.endsWith("#")) {
251
- checkSuffix = s.slice(-1);
252
- s = s.slice(0, -1);
253
- }
254
- // Castling
255
- if (s === "O-O" || s === "O-O-O") {
256
- const isKingSide = s === "O-O";
257
- const kingSq = board.kingSquare(pos.board, pos.turn);
258
- if (kingSq === undefined)
259
- return fail("san/noKing");
260
- // Find if castling is legal via dests. The generated dest is the
261
- // representation's castling square: normalized landing (6/2/62/58) or the
262
- // right's rook square (baseline/960 form). Both are accepted.
263
- const d = chess.dests(pos, kingSq);
264
- const normDest = pos.turn === Color.White ? (isKingSide ? 6 : 2) : (isKingSide ? 62 : 58);
265
- let dest;
266
- if (sq.has(d, normDest)) {
267
- dest = normDest;
268
- }
269
- else {
270
- const rights = pos.turn === Color.White ? pos.castling.white : pos.castling.black;
271
- for (const rs of rights) {
272
- const rookIsKingSide = squareFile(rs) > squareFile(kingSq);
273
- if (rookIsKingSide === isKingSide && sq.has(d, rs)) {
274
- dest = rs;
275
- break;
276
- }
277
- }
278
- }
279
- if (dest === undefined)
280
- return fail("san/illegal");
281
- const move = { from: kingSq, to: dest, isCastling: true, isEnPassant: false, isPromotion: false, promotion: null };
282
- // Optionally validate check suffix matches actual
283
- // We ignore mismatch for parsing tolerance, but we could validate
284
- // If checkSuffix present, we could verify after play, but not required to error
285
- return { result: Ok(move), candidates: [move] };
286
- }
287
- // Promotion handling
288
- let promotion = undefined;
289
- const eqIdx = s.indexOf("=");
290
- if (eqIdx !== -1) {
291
- const promoChar = s[eqIdx + 1];
292
- if (!promoChar)
293
- return fail("san/invalidPromotion");
294
- const role = promoCharToRole(promoChar);
295
- if (role === undefined)
296
- return fail("san/invalidPromotion");
297
- promotion = role;
298
- s = s.slice(0, eqIdx);
299
- }
300
- // Destination
301
- if (s.length < 2)
302
- return fail("san/invalidSan");
303
- const toStr = s.slice(-2);
304
- const to = parseSquare(toStr);
305
- if (to === undefined)
306
- return fail("san/invalidSquare");
307
- let rest = s.slice(0, -2);
308
- // Capture
309
- let isCapture = false;
310
- if (rest.endsWith("x")) {
311
- isCapture = true;
312
- rest = rest.slice(0, -1);
313
- }
314
- // Piece
315
- let pieceRole = null; // null means pawn
316
- if (rest.length > 0 && "KQRBN".includes(rest[0])) {
317
- const ch = rest[0];
318
- pieceRole = sanCharToRole(ch);
319
- rest = rest.slice(1);
320
- }
321
- else {
322
- pieceRole = Role.Pawn;
323
- }
324
- // Disambiguation remaining
325
- let disambFile = null;
326
- let disambRank = null;
327
- if (rest.length === 1) {
328
- const ch = rest[0];
329
- if (ch >= "a" && ch <= "h")
330
- disambFile = ch;
331
- else if (ch >= "1" && ch <= "8")
332
- disambRank = ch;
333
- else
334
- return fail("san/invalidSan");
335
- }
336
- else if (rest.length === 2) {
337
- if (rest[0] >= "a" && rest[0] <= "h" && rest[1] >= "1" && rest[1] <= "8") {
338
- disambFile = rest[0];
339
- disambRank = rest[1];
340
- }
341
- else
342
- return fail("san/invalidSan");
343
- }
344
- else if (rest.length > 2) {
345
- return fail("san/invalidSan");
346
- }
347
- // For pawn, pieceRole is pawn, but rest may be file for capture (like "e" in exd5)
348
- // Our disamb handling already captured that: for "exd5", rest after removing piece (none) and capture, rest = "e" => disambFile = 'e' . Good.
349
- // For pawn push "e4", rest = "" => no disamb.
350
- // Targeted reverse attacker lookup
351
- const us = pos.turn;
352
- const own = us === Color.White ? pos.board.white : pos.board.black;
353
- const candidateFroms = [];
354
- if (pieceRole === Role.Pawn) {
355
- // 1. Pawn pushes
356
- const pushFrom = us === Color.White ? to - 8 : to + 8;
357
- if (pushFrom >= 0 && pushFrom < 64 && !board.pieceAt(pos.board, to)) {
358
- const p = board.pieceAt(pos.board, pushFrom);
359
- if (p && p.color === us && p.role === Role.Pawn)
360
- candidateFroms.push(pushFrom);
361
- }
362
- if (us === Color.White && squareRank(to) === 3) {
363
- const dblFrom = to - 16;
364
- if (!board.pieceAt(pos.board, to) && !board.pieceAt(pos.board, to - 8)) {
365
- const p = board.pieceAt(pos.board, dblFrom);
366
- if (p && p.color === us && p.role === Role.Pawn)
367
- candidateFroms.push(dblFrom);
368
- }
369
- }
370
- else if (us === Color.Black && squareRank(to) === 4) {
371
- const dblFrom = to + 16;
372
- if (!board.pieceAt(pos.board, to) && !board.pieceAt(pos.board, to + 8)) {
373
- const p = board.pieceAt(pos.board, dblFrom);
374
- if (p && p.color === us && p.role === Role.Pawn)
375
- candidateFroms.push(dblFrom);
376
- }
377
- }
378
- // 2. Pawn diagonal attacks (normal captures or en-passant)
379
- const pAtt = attacks.pawnAttacks(us === Color.White ? Color.Black : Color.White, to);
380
- const pawnCaps = sq.and(pAtt, sq.and(own, pos.board.pawn));
381
- for (const sqIdx of sq.iter(pawnCaps)) {
382
- candidateFroms.push(sqIdx);
383
- }
384
- }
385
- else {
386
- let attackers;
387
- switch (pieceRole) {
388
- case Role.Knight:
389
- attackers = sq.and(attacks.knightAttacks(to), sq.and(own, pos.board.knight));
390
- break;
391
- case Role.Bishop:
392
- attackers = sq.and(attacks.bishopAttacks(to, pos.board.occupied), sq.and(own, pos.board.bishop));
393
- break;
394
- case Role.Rook:
395
- attackers = sq.and(attacks.rookAttacks(to, pos.board.occupied), sq.and(own, pos.board.rook));
396
- break;
397
- case Role.Queen:
398
- attackers = sq.and(attacks.queenAttacks(to, pos.board.occupied), sq.and(own, pos.board.queen));
399
- break;
400
- case Role.King:
401
- attackers = sq.and(attacks.kingAttacks(to), sq.and(own, pos.board.king));
402
- break;
403
- default:
404
- attackers = sq.EMPTY;
405
- break;
406
- }
407
- for (const sqIdx of sq.iter(attackers)) {
408
- candidateFroms.push(sqIdx);
409
- }
410
- }
411
- // Disambiguation filtering
412
- let filteredFroms = candidateFroms;
413
- if (disambFile !== null) {
414
- const reqFile = disambFile.charCodeAt(0) - 97;
415
- filteredFroms = filteredFroms.filter(f => squareFile(f) === reqFile);
416
- }
417
- if (disambRank !== null) {
418
- const reqRank = disambRank.charCodeAt(0) - 49;
419
- filteredFroms = filteredFroms.filter(f => squareRank(f) === reqRank);
420
- }
421
- if (filteredFroms.length === 0)
422
- return fail("san/noLegalMove");
423
- // Legality filtering with single shared check context
424
- const ctx = chess.analyzeCheckContext(pos);
425
- const candidates = [];
426
- const destRank = squareRank(to);
427
- for (const from of filteredFroms) {
428
- const fromPiece = board.pieceAt(pos.board, from);
429
- if (!fromPiece)
430
- continue;
431
- const isPromo = fromPiece.role === Role.Pawn && (destRank === 0 || destRank === 7);
432
- if (isPromo && promotion === undefined)
433
- continue;
434
- if (!isPromo && promotion !== undefined)
435
- continue;
436
- const d = chess.destsFast(pos, from, fromPiece, ctx);
437
- if (!sq.has(d, to))
438
- continue;
439
- let isEnPassant = false;
440
- if (fromPiece.role === Role.Pawn && pos.epSquare !== null && to === pos.epSquare) {
441
- const fileDiff = Math.abs(squareFile(to) - squareFile(from));
442
- const dir = fromPiece.color === Color.White ? 1 : -1;
443
- if (fileDiff === 1 && destRank - squareRank(from) === dir)
444
- isEnPassant = true;
445
- }
446
- candidates.push({
447
- from,
448
- to,
449
- promotion: isPromo ? (promotion ?? Role.Queen) : null,
450
- isPromotion: isPromo,
451
- isEnPassant,
452
- isCastling: false,
453
- });
454
- }
455
- if (candidates.length === 0)
456
- return { result: Err({ code: "san/noLegalMove" }), candidates };
457
- if (candidates.length > 1)
458
- return { result: Err({ code: "san/ambiguous" }), candidates };
459
- return { result: Ok(candidates[0]), candidates };
460
- }
461
- // ---------- UCI ----------
462
- export function parseUci(uci) {
463
- const s = uci.trim();
464
- // The null move is the `0000` token (its own 4-char spelling, not a square
465
- // pair). Parsing is position-free; the legality of a pass is judged by the
466
- // validating entry points (isLegal / parseSan / makeMove).
467
- if (s === NULL_MOVE_UCI)
468
- return Ok(nullMove());
469
- if (s.length < 4 || s.length > 5)
470
- return Err({ code: "uci/invalidUci" });
471
- const fromStr = s.slice(0, 2);
472
- const toStr = s.slice(2, 4);
473
- const from = parseSquare(fromStr);
474
- const to = parseSquare(toStr);
475
- if (from === undefined || to === undefined)
476
- return Err({ code: "uci/invalidSquare" });
477
- let promotion = null;
478
- let isPromotion = false;
479
- if (s.length === 5) {
480
- const promoChar = s[4];
481
- const role = promoCharToRole(promoChar);
482
- if (!role)
483
- return Err({ code: "uci/invalidPromotion" });
484
- promotion = role;
485
- isPromotion = true;
486
- }
487
- // Determine if this is castling via king move? For UCI, castling is e1g1 or e1h1 etc. We need to detect if this is king move and castling rights? But we don't have Position here to validate. parseUci as per spec is pure without position: it just parses squares and promotion. However spec says parseUci handles e1g1 and e1h1 both as castling in 960. But without position, we can't know. So we just return Move with from,to,promotion and set isCastling false initially; caller can interpret via position's dests/isLegal.
488
- // But we can set isCastling heuristically: if from is king start and to is G1/C1 or rook square? We don't have board. We'll just return move and let chess.isLegal handle normalization.
489
- // For now, set isCastling false, and let makeMove handle 960 normalization
490
- const move = { from, to, promotion, isPromotion, isEnPassant: false, isCastling: false };
491
- // Also need to detect promotion case where to square is back rank and promotion missing? That's SAN concern, not UCI: UCI must have promotion char if pawn to back rank, but we can allow without and still return move with null promotion? Spec says e7e8q and e7e8Q both promotion queen. We'll handle.
492
- return Ok(move);
493
- }
494
- export function makeUci(move) {
495
- // A pass renders as `0000` (the 16-bit sentinel's 4-char spelling) and
496
- // returns here — it has no origin/destination square.
497
- if (isNullMove(move))
498
- return NULL_MOVE_UCI;
499
- const from = squareName(move.from);
500
- const to = squareName(move.to);
501
- let promo = "";
502
- if (move.promotion !== null && move.promotion !== undefined) {
503
- switch (move.promotion) {
504
- case Role.Queen:
505
- promo = "q";
506
- break;
507
- case Role.Rook:
508
- promo = "r";
509
- break;
510
- case Role.Bishop:
511
- promo = "b";
512
- break;
513
- case Role.Knight:
514
- promo = "n";
515
- break;
516
- default: promo = "";
517
- }
518
- }
519
- else if (move.isPromotion && move.promotion) {
520
- // same
521
- }
522
- return from + to + promo;
523
- }
@@ -1,3 +0,0 @@
1
- import type { Position } from "./types.js";
2
- export declare function standardMovesInto(pos: Position, out: Uint16Array | Uint32Array): number | undefined;
3
- //# sourceMappingURL=standardMovegen.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"standardMovegen.d.ts","sourceRoot":"","sources":["../src/standardMovegen.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAsE3C,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,WAAW,GAAG,WAAW,GAAG,MAAM,GAAG,SAAS,CA+LnG"}