gigachess 0.2.1

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 (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +183 -0
  3. package/dist/attacks.d.ts +40 -0
  4. package/dist/attacks.d.ts.map +1 -0
  5. package/dist/attacks.js +389 -0
  6. package/dist/bishopMagicBlob.d.ts +2 -0
  7. package/dist/bishopMagicBlob.d.ts.map +1 -0
  8. package/dist/bishopMagicBlob.js +11 -0
  9. package/dist/board.d.ts +73 -0
  10. package/dist/board.d.ts.map +1 -0
  11. package/dist/board.js +237 -0
  12. package/dist/chess.d.ts +215 -0
  13. package/dist/chess.d.ts.map +1 -0
  14. package/dist/chess.js +1453 -0
  15. package/dist/chess960.d.ts +10 -0
  16. package/dist/chess960.d.ts.map +1 -0
  17. package/dist/chess960.js +18 -0
  18. package/dist/chessops/board.d.ts +49 -0
  19. package/dist/chessops/board.d.ts.map +1 -0
  20. package/dist/chessops/board.js +138 -0
  21. package/dist/chessops/chess.d.ts +104 -0
  22. package/dist/chessops/chess.d.ts.map +1 -0
  23. package/dist/chessops/chess.js +435 -0
  24. package/dist/chessops/compat.d.ts +19 -0
  25. package/dist/chessops/compat.d.ts.map +1 -0
  26. package/dist/chessops/compat.js +25 -0
  27. package/dist/chessops/convert.d.ts +42 -0
  28. package/dist/chessops/convert.d.ts.map +1 -0
  29. package/dist/chessops/convert.js +114 -0
  30. package/dist/chessops/debug.d.ts +17 -0
  31. package/dist/chessops/debug.d.ts.map +1 -0
  32. package/dist/chessops/debug.js +67 -0
  33. package/dist/chessops/fen.d.ts +28 -0
  34. package/dist/chessops/fen.d.ts.map +1 -0
  35. package/dist/chessops/fen.js +74 -0
  36. package/dist/chessops/fenInternal.d.ts +15 -0
  37. package/dist/chessops/fenInternal.d.ts.map +1 -0
  38. package/dist/chessops/fenInternal.js +42 -0
  39. package/dist/chessops/index.d.ts +12 -0
  40. package/dist/chessops/index.d.ts.map +1 -0
  41. package/dist/chessops/index.js +17 -0
  42. package/dist/chessops/san.d.ts +7 -0
  43. package/dist/chessops/san.d.ts.map +1 -0
  44. package/dist/chessops/san.js +33 -0
  45. package/dist/chessops/setup.d.ts +64 -0
  46. package/dist/chessops/setup.d.ts.map +1 -0
  47. package/dist/chessops/setup.js +158 -0
  48. package/dist/chessops/squareSet.d.ts +48 -0
  49. package/dist/chessops/squareSet.d.ts.map +1 -0
  50. package/dist/chessops/squareSet.js +192 -0
  51. package/dist/chessops/transform.d.ts +12 -0
  52. package/dist/chessops/transform.d.ts.map +1 -0
  53. package/dist/chessops/transform.js +53 -0
  54. package/dist/chessops/types.d.ts +47 -0
  55. package/dist/chessops/types.d.ts.map +1 -0
  56. package/dist/chessops/types.js +12 -0
  57. package/dist/chessops/util.d.ts +19 -0
  58. package/dist/chessops/util.d.ts.map +1 -0
  59. package/dist/chessops/util.js +52 -0
  60. package/dist/chesstree.d.ts +143 -0
  61. package/dist/chesstree.d.ts.map +1 -0
  62. package/dist/chesstree.js +514 -0
  63. package/dist/core.d.ts +10 -0
  64. package/dist/core.d.ts.map +1 -0
  65. package/dist/core.js +11 -0
  66. package/dist/fen.d.ts +10 -0
  67. package/dist/fen.d.ts.map +1 -0
  68. package/dist/fen.js +600 -0
  69. package/dist/index.d.ts +17 -0
  70. package/dist/index.d.ts.map +1 -0
  71. package/dist/index.js +22 -0
  72. package/dist/packedMove.d.ts +31 -0
  73. package/dist/packedMove.d.ts.map +1 -0
  74. package/dist/packedMove.js +88 -0
  75. package/dist/pgn.d.ts +24 -0
  76. package/dist/pgn.d.ts.map +1 -0
  77. package/dist/pgn.js +361 -0
  78. package/dist/rookMagicBlob.d.ts +2 -0
  79. package/dist/rookMagicBlob.d.ts.map +1 -0
  80. package/dist/rookMagicBlob.js +11 -0
  81. package/dist/san.d.ts +6 -0
  82. package/dist/san.d.ts.map +1 -0
  83. package/dist/san.js +484 -0
  84. package/dist/squareSet.d.ts +48 -0
  85. package/dist/squareSet.d.ts.map +1 -0
  86. package/dist/squareSet.js +154 -0
  87. package/dist/types.d.ts +147 -0
  88. package/dist/types.d.ts.map +1 -0
  89. package/dist/types.js +88 -0
  90. package/dist/util.d.ts +11 -0
  91. package/dist/util.d.ts.map +1 -0
  92. package/dist/util.js +59 -0
  93. package/dist/zobrist.d.ts +45 -0
  94. package/dist/zobrist.d.ts.map +1 -0
  95. package/dist/zobrist.js +271 -0
  96. package/dist/zobristBlob.d.ts +2 -0
  97. package/dist/zobristBlob.d.ts.map +1 -0
  98. package/dist/zobristBlob.js +11 -0
  99. package/package.json +135 -0
package/dist/san.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { Position, Move, Result, SanError, UciError } from "./types.js";
2
+ export declare function makeSan(move: Move, pos: Position): string;
3
+ export declare function parseSan(san: string, pos: Position): Result<Move, SanError>;
4
+ export declare function parseUci(uci: string): Result<Move, UciError>;
5
+ export declare function makeUci(move: Move): string;
6
+ //# sourceMappingURL=san.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"san.d.ts","sourceRoot":"","sources":["../src/san.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAwC7E,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,GAAG,MAAM,CAyJzD;AAGD,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAsJ3E;AAgDD,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAuB5D;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAgB1C"}
package/dist/san.js ADDED
@@ -0,0 +1,484 @@
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 { Color, Role } from "./types.js";
7
+ import { Err, Ok } from "./types.js";
8
+ import { squareFile, squareRank, parseSquare, squareName } from "./util.js";
9
+ function roleToSanChar(role) {
10
+ switch (role) {
11
+ case Role.King: return "K";
12
+ case Role.Queen: return "Q";
13
+ case Role.Rook: return "R";
14
+ case Role.Bishop: return "B";
15
+ case Role.Knight: return "N";
16
+ case Role.Pawn: return "";
17
+ default: return "";
18
+ }
19
+ }
20
+ function sanCharToRole(ch) {
21
+ if (ch === "K")
22
+ return Role.King;
23
+ if (ch === "Q")
24
+ return Role.Queen;
25
+ if (ch === "R")
26
+ return Role.Rook;
27
+ if (ch === "B")
28
+ return Role.Bishop;
29
+ if (ch === "N")
30
+ return Role.Knight;
31
+ return undefined;
32
+ }
33
+ function promoCharToRole(ch) {
34
+ const c = ch.toUpperCase();
35
+ if (c === "Q")
36
+ return Role.Queen;
37
+ if (c === "R")
38
+ return Role.Rook;
39
+ if (c === "B")
40
+ return Role.Bishop;
41
+ if (c === "N")
42
+ return Role.Knight;
43
+ return undefined;
44
+ }
45
+ function normalizeCastlingSan(s) {
46
+ // tolerant 0-0 -> O-O
47
+ return s.replace(/0-0-0/g, "O-O-O").replace(/0-0/g, "O-O");
48
+ }
49
+ // ---------- makeSan ----------
50
+ export function makeSan(move, pos) {
51
+ // Castling — detected via the shared detectCastling path (design D2) so a
52
+ // canonical castling move given in the representation's encoding renders
53
+ // O-O/O-O-O, never "Kg1"/"Kxh1" (measured defect: makeSan({4,6}, kiwipete)
54
+ // returned "Kg1"). The isCastling flag is kept as a fallback for moves
55
+ // produced by genLegalMoves under either representation. The role/rights
56
+ // pre-check keeps the hot SAN path free of detectCastling calls.
57
+ let castling = null;
58
+ const movingPiece = board.pieceAt(pos.board, move.from);
59
+ if (movingPiece && movingPiece.role === Role.King && (pos.castling.white.size > 0 || pos.castling.black.size > 0)) {
60
+ castling = chess.detectCastling(pos, move.from, move.to);
61
+ }
62
+ if (castling || move.isCastling) {
63
+ const isKingSide = castling ? castling.side === "king" : move.to === 6 || move.to === 62;
64
+ let base = isKingSide ? "O-O" : "O-O-O";
65
+ // add check suffix
66
+ const next = chess.makeMove(pos, move);
67
+ if (chess.isCheckmate(next))
68
+ base += "#";
69
+ else if (chess.isCheck(next))
70
+ base += "+";
71
+ return base;
72
+ }
73
+ const piece = board.pieceAt(pos.board, move.from);
74
+ if (!piece)
75
+ return "";
76
+ const toName = squareName(move.to);
77
+ const isCapture = (() => {
78
+ const target = board.pieceAt(pos.board, move.to);
79
+ if (target && target.color !== pos.turn)
80
+ return true;
81
+ if (move.isEnPassant)
82
+ return true;
83
+ // en-passant capture onto the empty ep square (baseline output parity):
84
+ // a pawn moving DIAGONALLY onto the ep square is a capture even when the
85
+ // caller did not set the isEnPassant flag (external move objects). A
86
+ // straight push to the ep square remains a quiet move.
87
+ if (piece.role === Role.Pawn &&
88
+ pos.epSquare !== null &&
89
+ move.to === pos.epSquare &&
90
+ squareFile(move.from) !== squareFile(move.to)) {
91
+ return true;
92
+ }
93
+ return false;
94
+ })();
95
+ const destRank = squareRank(move.to);
96
+ const isPromotion = !!move.isPromotion || (piece.role === Role.Pawn && (destRank === 7 || destRank === 0));
97
+ // Piece letter
98
+ let san = "";
99
+ if (piece.role !== Role.Pawn) {
100
+ san += roleToSanChar(piece.role);
101
+ }
102
+ else if (isCapture) {
103
+ // pawn capture needs file of from
104
+ san += String.fromCharCode(97 + squareFile(move.from));
105
+ }
106
+ // Disambiguation for non-pawn (and pawn capture already handled)
107
+ if (piece.role !== Role.Pawn) {
108
+ // Find all pieces of same role/color that can legally move to same destination
109
+ const candidates = [];
110
+ const own = pos.turn === Color.White ? pos.board.white : pos.board.black;
111
+ // Iterate over all squares with same role
112
+ const roleSet = (() => {
113
+ switch (piece.role) {
114
+ case Role.Knight: return pos.board.knight;
115
+ case Role.Bishop: return pos.board.bishop;
116
+ case Role.Rook: return pos.board.rook;
117
+ case Role.Queen: return pos.board.queen;
118
+ case Role.King: return pos.board.king;
119
+ default: return sq.empty();
120
+ }
121
+ })();
122
+ const sameRole = sq.and(own, roleSet);
123
+ // CheckContext reuse (design D4): the position's check/pin masks are
124
+ // computed ONCE per makeSan call and shared across every candidate
125
+ // piece's dest evaluation, instead of re-running the full
126
+ // analyzeCheckContext inside chess.dests() per candidate — O(N x movegen)
127
+ // becomes O(N) with one shared context. Zero allocation beyond the
128
+ // context itself; SAN output is byte-identical (same destsFast semantics).
129
+ const ctx = chess.analyzeCheckContext(pos);
130
+ for (const sqIdx of sq.iter(sameRole)) {
131
+ if (sqIdx === move.from)
132
+ continue;
133
+ const cand = board.pieceAt(pos.board, sqIdx);
134
+ if (!cand)
135
+ continue;
136
+ const d = chess.destsFast(pos, sqIdx, cand, ctx);
137
+ if (sq.has(d, move.to))
138
+ candidates.push(sqIdx);
139
+ }
140
+ // Include moving piece itself, total candidates +1 = all that can reach dest
141
+ // If there is any other candidate, need disambiguation
142
+ if (candidates.length > 0) {
143
+ // Determine minimal disambiguation
144
+ const movingFile = squareFile(move.from);
145
+ const movingRank = squareRank(move.from);
146
+ let needFile = false;
147
+ let needRank = false;
148
+ // Check if file alone distinguishes
149
+ let fileUnique = true;
150
+ for (const c of candidates) {
151
+ if (squareFile(c) === movingFile) {
152
+ fileUnique = false;
153
+ break;
154
+ }
155
+ }
156
+ if (fileUnique) {
157
+ san += String.fromCharCode(97 + movingFile);
158
+ }
159
+ else {
160
+ // file not unique, check rank
161
+ let rankUnique = true;
162
+ for (const c of candidates) {
163
+ if (squareRank(c) === movingRank) {
164
+ rankUnique = false;
165
+ break;
166
+ }
167
+ }
168
+ if (rankUnique) {
169
+ san += String.fromCharCode(49 + movingRank);
170
+ }
171
+ else {
172
+ san += String.fromCharCode(97 + movingFile) + String.fromCharCode(49 + movingRank);
173
+ }
174
+ }
175
+ }
176
+ }
177
+ // Capture
178
+ if (isCapture) {
179
+ // For pawn, we already added file, need 'x'
180
+ // For pieces, disambiguation already in san, now add 'x'
181
+ san += "x";
182
+ }
183
+ // Destination
184
+ // For pawn capture we already have file preceding 'x', need dest; for non-pawn we need dest after optional disamb and 'x'
185
+ // Our san currently for pawn capture is like "e" + "x" => "ex", then dest
186
+ // For non-pawn, san is piece char + disamb + 'x' if capture, then dest
187
+ // So append dest
188
+ // But for non-pawn capture case, we already have piece+disamb, now need to ensure we haven't duplicated file for pawn
189
+ // For pawn non-capture, san is "" currently, just dest
190
+ // For pawn capture, san is "ex" already, need dest
191
+ // For pieces, san currently is piece+disamb (+x if capture) => need dest
192
+ // However for pieces, if we added disamb file/rank, san already has it, so just add dest
193
+ // 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.
194
+ // For pawn non-capture "e4": san "" initially, no disamb, no capture, just dest "e4"
195
+ // For piece "Nbd2": piece N, candidates determine file unique => san = "N" + "b" => "Nb", no capture, then dest "d2" => "Nbd2"
196
+ // For "Qh4e1": piece Q, disamb "h4", san="Qh4", dest "e1" => "Qh4e1"
197
+ san += toName;
198
+ // Promotion
199
+ if (isPromotion) {
200
+ const promo = move.promotion ?? Role.Queen;
201
+ san += "=" + roleToSanChar(promo);
202
+ }
203
+ // Check / mate suffix
204
+ const next = chess.makeMove(pos, move);
205
+ if (chess.isCheckmate(next))
206
+ san += "#";
207
+ else if (chess.isCheck(next))
208
+ san += "+";
209
+ return san;
210
+ }
211
+ // ---------- parseSan ----------
212
+ export function parseSan(san, pos) {
213
+ const orig = san;
214
+ let s = normalizeCastlingSan(san.trim());
215
+ // Handle check suffix for later, but keep for validation
216
+ let checkSuffix = "";
217
+ if (s.endsWith("+") || s.endsWith("#")) {
218
+ checkSuffix = s.slice(-1);
219
+ s = s.slice(0, -1);
220
+ }
221
+ // Castling
222
+ if (s === "O-O" || s === "O-O-O") {
223
+ const isKingSide = s === "O-O";
224
+ const kingSq = board.kingSquare(pos.board, pos.turn);
225
+ if (kingSq === undefined)
226
+ return Err({ code: "san/noKing" });
227
+ // Find if castling is legal via dests. The generated dest is the
228
+ // representation's castling square: normalized landing (6/2/62/58) or the
229
+ // right's rook square (baseline/960 form). Both are accepted.
230
+ const d = chess.dests(pos, kingSq);
231
+ const normDest = pos.turn === Color.White ? (isKingSide ? 6 : 2) : (isKingSide ? 62 : 58);
232
+ let dest;
233
+ if (sq.has(d, normDest)) {
234
+ dest = normDest;
235
+ }
236
+ else {
237
+ const rights = pos.turn === Color.White ? pos.castling.white : pos.castling.black;
238
+ for (const rs of rights) {
239
+ const rookIsKingSide = squareFile(rs) > squareFile(kingSq);
240
+ if (rookIsKingSide === isKingSide && sq.has(d, rs)) {
241
+ dest = rs;
242
+ break;
243
+ }
244
+ }
245
+ }
246
+ if (dest === undefined)
247
+ return Err({ code: "san/illegal" });
248
+ const move = { from: kingSq, to: dest, isCastling: true, isEnPassant: false, isPromotion: false, promotion: null };
249
+ // Optionally validate check suffix matches actual
250
+ // We ignore mismatch for parsing tolerance, but we could validate
251
+ // If checkSuffix present, we could verify after play, but not required to error
252
+ return Ok(move);
253
+ }
254
+ // Promotion handling
255
+ let promotion = undefined;
256
+ const eqIdx = s.indexOf("=");
257
+ if (eqIdx !== -1) {
258
+ const promoChar = s[eqIdx + 1];
259
+ if (!promoChar)
260
+ return Err({ code: "san/invalidPromotion" });
261
+ const role = promoCharToRole(promoChar);
262
+ if (role === undefined)
263
+ return Err({ code: "san/invalidPromotion" });
264
+ promotion = role;
265
+ s = s.slice(0, eqIdx);
266
+ }
267
+ // Destination
268
+ if (s.length < 2)
269
+ return Err({ code: "san/invalidSan" });
270
+ const toStr = s.slice(-2);
271
+ const to = parseSquare(toStr);
272
+ if (to === undefined)
273
+ return Err({ code: "san/invalidSquare" });
274
+ let rest = s.slice(0, -2);
275
+ // Capture
276
+ let isCapture = false;
277
+ if (rest.endsWith("x")) {
278
+ isCapture = true;
279
+ rest = rest.slice(0, -1);
280
+ }
281
+ // Piece
282
+ let pieceRole = null; // null means pawn
283
+ if (rest.length > 0 && "KQRBN".includes(rest[0])) {
284
+ const ch = rest[0];
285
+ pieceRole = sanCharToRole(ch);
286
+ rest = rest.slice(1);
287
+ }
288
+ else {
289
+ pieceRole = Role.Pawn;
290
+ }
291
+ // Disambiguation remaining
292
+ let disambFile = null;
293
+ let disambRank = null;
294
+ if (rest.length === 1) {
295
+ const ch = rest[0];
296
+ if (ch >= "a" && ch <= "h")
297
+ disambFile = ch;
298
+ else if (ch >= "1" && ch <= "8")
299
+ disambRank = ch;
300
+ else
301
+ return Err({ code: "san/invalidSan" });
302
+ }
303
+ else if (rest.length === 2) {
304
+ if (rest[0] >= "a" && rest[0] <= "h" && rest[1] >= "1" && rest[1] <= "8") {
305
+ disambFile = rest[0];
306
+ disambRank = rest[1];
307
+ }
308
+ else
309
+ return Err({ code: "san/invalidSan" });
310
+ }
311
+ else if (rest.length > 2) {
312
+ return Err({ code: "san/invalidSan" });
313
+ }
314
+ // For pawn, pieceRole is pawn, but rest may be file for capture (like "e" in exd5)
315
+ // Our disamb handling already captured that: for "exd5", rest after removing piece (none) and capture, rest = "e" => disambFile = 'e' . Good.
316
+ // For pawn push "e4", rest = "" => no disamb.
317
+ // Now find legal moves matching
318
+ const legalMoves = genLegalMovesForSan(pos);
319
+ const candidates = [];
320
+ for (const m of legalMoves) {
321
+ const fromPiece = board.pieceAt(pos.board, m.from);
322
+ if (!fromPiece)
323
+ continue;
324
+ // piece match
325
+ if (pieceRole !== null) {
326
+ // pieceRole is expected role; for pawn we set Role.Pawn
327
+ if (fromPiece.role !== pieceRole)
328
+ continue;
329
+ }
330
+ else {
331
+ if (fromPiece.role !== Role.Pawn)
332
+ continue;
333
+ }
334
+ if (m.to !== to)
335
+ continue;
336
+ // Capture flag in the SAN is cosmetic on INPUT (baseline parity): a legal
337
+ // move matching piece + destination is accepted whether or not the SAN's
338
+ // 'x' matches an actual capture (e.g. baseline accepts "Nxb5" for a quiet
339
+ // knight move when b5 is empty). Output (makeSan) still renders 'x' only
340
+ // for real captures, so announcer output is unchanged.
341
+ // promotion
342
+ if (promotion !== undefined) {
343
+ if (m.promotion !== promotion)
344
+ continue;
345
+ }
346
+ else {
347
+ if (m.isPromotion)
348
+ continue;
349
+ }
350
+ // disambiguation
351
+ if (disambFile !== null) {
352
+ const f = String.fromCharCode(97 + squareFile(m.from));
353
+ if (f !== disambFile)
354
+ continue;
355
+ }
356
+ if (disambRank !== null) {
357
+ const r = String.fromCharCode(49 + squareRank(m.from));
358
+ if (r !== disambRank)
359
+ continue;
360
+ }
361
+ candidates.push(m);
362
+ }
363
+ if (candidates.length === 0)
364
+ return Err({ code: "san/noLegalMove" });
365
+ if (candidates.length > 1)
366
+ return Err({ code: "san/ambiguous" });
367
+ const chosen = candidates[0];
368
+ // Optionally validate check suffix: if suffix present but after play check status mismatches, should we error? For now ignore, but we could validate
369
+ // If suffix is '+' but resulting pos not check, or '#' but not mate, should we error? Spec says SAN includes +/# based on resulting position, so we could validate but not required for parsing
370
+ // We'll optionally validate if suffix present
371
+ if (checkSuffix) {
372
+ const next = chess.makeMove(pos, chosen);
373
+ const isMate = chess.isCheckmate(next);
374
+ const isChk = chess.isCheck(next);
375
+ if (checkSuffix === "+" && !isChk) {
376
+ // SAN claims check but not check -> still treat as illegal? We could ignore
377
+ }
378
+ if (checkSuffix === "#" && !isMate) {
379
+ // mismatch
380
+ }
381
+ }
382
+ return Ok(chosen);
383
+ }
384
+ // helper to generate legal moves for SAN matching (includes all promotion variants)
385
+ function genLegalMovesForSan(pos) {
386
+ // Use chess genLegalMoves internal? We can call chess.perft helper? But chess.genLegalMoves is not exported (private). We need to replicate or expose.
387
+ // For now, we will generate via allDests and then expand promotions
388
+ // Simpler: use chess.allDests and then for each destination, create Move(s) and test isLegal via chess.isLegal? But we need Move objects with promotion etc.
389
+ // We'll generate via iterating allDests and handling promotion expansion
390
+ const moves = [];
391
+ const all = chess.allDests(pos);
392
+ for (const [from, set] of all) {
393
+ for (const to of sq.iter(set)) {
394
+ const piece = board.pieceAt(pos.board, from);
395
+ if (!piece)
396
+ continue;
397
+ const destRank = squareRank(to);
398
+ const isPromo = piece.role === Role.Pawn && (destRank === 0 || destRank === 7);
399
+ if (isPromo) {
400
+ for (const promo of [Role.Queen, Role.Rook, Role.Bishop, Role.Knight]) {
401
+ const isEnPassant = false;
402
+ // pawn promotion capture is also capture, but set already includes captures
403
+ const target = board.pieceAt(pos.board, to);
404
+ const isCap = !!target || (pos.epSquare === to);
405
+ // Actually en passant to back rank not possible
406
+ const mv = { from, to, promotion: promo, isPromotion: true, isEnPassant: false, isCastling: false };
407
+ // Need to set isEnPassant correctly for pawn promo capture en passant? Not needed
408
+ moves.push(mv);
409
+ }
410
+ }
411
+ else {
412
+ const target = board.pieceAt(pos.board, to);
413
+ let isEnPassant = false;
414
+ if (piece.role === Role.Pawn && pos.epSquare !== null && to === pos.epSquare) {
415
+ const fileDiff = Math.abs(squareFile(to) - squareFile(from));
416
+ const dir = piece.color === Color.White ? 1 : -1;
417
+ if (fileDiff === 1 && destRank - squareRank(from) === dir)
418
+ isEnPassant = true;
419
+ }
420
+ let isCastling = false;
421
+ if (piece.role === Role.King) {
422
+ // Shared detectCastling path (design D2) — no dest-heuristic here.
423
+ isCastling = chess.detectCastling(pos, from, to) !== null;
424
+ }
425
+ moves.push({ from, to, promotion: null, isPromotion: false, isEnPassant, isCastling });
426
+ }
427
+ }
428
+ }
429
+ return moves;
430
+ }
431
+ // ---------- UCI ----------
432
+ export function parseUci(uci) {
433
+ const s = uci.trim();
434
+ if (s.length < 4 || s.length > 5)
435
+ return Err({ code: "uci/invalidUci" });
436
+ const fromStr = s.slice(0, 2);
437
+ const toStr = s.slice(2, 4);
438
+ const from = parseSquare(fromStr);
439
+ const to = parseSquare(toStr);
440
+ if (from === undefined || to === undefined)
441
+ return Err({ code: "uci/invalidSquare" });
442
+ let promotion = null;
443
+ let isPromotion = false;
444
+ if (s.length === 5) {
445
+ const promoChar = s[4];
446
+ const role = promoCharToRole(promoChar);
447
+ if (!role)
448
+ return Err({ code: "uci/invalidPromotion" });
449
+ promotion = role;
450
+ isPromotion = true;
451
+ }
452
+ // 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.
453
+ // 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.
454
+ // For now, set isCastling false, and let makeMove handle 960 normalization
455
+ const move = { from, to, promotion, isPromotion, isEnPassant: false, isCastling: false };
456
+ // 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.
457
+ return Ok(move);
458
+ }
459
+ export function makeUci(move) {
460
+ const from = squareName(move.from);
461
+ const to = squareName(move.to);
462
+ let promo = "";
463
+ if (move.promotion !== null && move.promotion !== undefined) {
464
+ switch (move.promotion) {
465
+ case Role.Queen:
466
+ promo = "q";
467
+ break;
468
+ case Role.Rook:
469
+ promo = "r";
470
+ break;
471
+ case Role.Bishop:
472
+ promo = "b";
473
+ break;
474
+ case Role.Knight:
475
+ promo = "n";
476
+ break;
477
+ default: promo = "";
478
+ }
479
+ }
480
+ else if (move.isPromotion && move.promotion) {
481
+ // same
482
+ }
483
+ return from + to + promo;
484
+ }
@@ -0,0 +1,48 @@
1
+ export type SquareSet = {
2
+ readonly lo: number;
3
+ readonly hi: number;
4
+ };
5
+ /**
6
+ * Mutable {lo,hi} view for hot-loop scratch buffers (see the WritableBoard FP
7
+ * policy in board.ts). Assignable to SquareSet (readonly is assignability-
8
+ * neutral in TS), so scratch boards flow through Board-typed parameters while
9
+ * their owning hot loop may write the bitfields in place, allocation-free.
10
+ * MutableSquareSet values must NEVER escape the hot loop that owns them.
11
+ */
12
+ export type MutableSquareSet = {
13
+ lo: number;
14
+ hi: number;
15
+ };
16
+ export declare const EMPTY: SquareSet;
17
+ export declare const FULL: SquareSet;
18
+ export declare function empty(): SquareSet;
19
+ export declare function full(): SquareSet;
20
+ export declare function singleton(sq: number): SquareSet;
21
+ export declare function has(set: SquareSet, sq: number): boolean;
22
+ export declare function and(a: SquareSet, b: SquareSet): SquareSet;
23
+ export declare function or(a: SquareSet, b: SquareSet): SquareSet;
24
+ export declare function xor(a: SquareSet, b: SquareSet): SquareSet;
25
+ export declare function not(a: SquareSet): SquareSet;
26
+ export declare function complement(a: SquareSet): SquareSet;
27
+ export declare function minus(a: SquareSet, b: SquareSet): SquareSet;
28
+ export declare function shl(set: SquareSet, n: number): SquareSet;
29
+ export declare function shr(set: SquareSet, n: number): SquareSet;
30
+ export declare function popcount(set: SquareSet): number;
31
+ export declare const popcnt: typeof popcount;
32
+ export declare function first(set: SquareSet): number | undefined;
33
+ export declare function moreThanOne(set: SquareSet): boolean;
34
+ export declare function isEmpty(set: SquareSet): boolean;
35
+ export declare function isNonEmpty(set: SquareSet): boolean;
36
+ export declare function equals(a: SquareSet, b: SquareSet): boolean;
37
+ export declare function iter(set: SquareSet): Iterable<number>;
38
+ /**
39
+ * Non-generator square iteration for hot loops. `iter` allocates 2 objects per
40
+ * square (minus + singleton) plus the generator machinery; this allocates
41
+ * nothing. Prefer `forEachSquare` over `for...of sq.iter(...)` wherever the
42
+ * callback can be a plain statement (see board.ts FP policy).
43
+ */
44
+ export declare function forEachSquare(set: SquareSet, fn: (sqIdx: number) => void): void;
45
+ export declare function size(set: SquareSet): number;
46
+ export declare function diff(a: SquareSet, b: SquareSet): SquareSet;
47
+ export declare function toString(set: SquareSet): string;
48
+ //# sourceMappingURL=squareSet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"squareSet.d.ts","sourceRoot":"","sources":["../src/squareSet.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;CACZ,CAAC;AAGF,eAAO,MAAM,KAAK,EAAE,SAAqC,CAAC;AAC1D,eAAO,MAAM,IAAI,EAAE,SAAmE,CAAC;AAEvF,wBAAgB,KAAK,IAAI,SAAS,CAEjC;AAED,wBAAgB,IAAI,IAAI,SAAS,CAEhC;AAED,wBAAgB,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,SAAS,CAG/C;AAED,wBAAgB,GAAG,CAAC,GAAG,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAGvD;AAED,wBAAgB,GAAG,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,SAAS,CAEzD;AAED,wBAAgB,EAAE,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,SAAS,CAExD;AAED,wBAAgB,GAAG,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,SAAS,CAEzD;AAED,wBAAgB,GAAG,CAAC,CAAC,EAAE,SAAS,GAAG,SAAS,CAE3C;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,SAAS,GAAG,SAAS,CAElD;AAED,wBAAgB,KAAK,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,SAAS,CAE3D;AAED,wBAAgB,GAAG,CAAC,GAAG,EAAE,SAAS,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAWxD;AAED,wBAAgB,GAAG,CAAC,GAAG,EAAE,SAAS,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAUxD;AAUD,wBAAgB,QAAQ,CAAC,GAAG,EAAE,SAAS,GAAG,MAAM,CAE/C;AAGD,eAAO,MAAM,MAAM,iBAAW,CAAC;AAE/B,wBAAgB,KAAK,CAAC,GAAG,EAAE,SAAS,GAAG,MAAM,GAAG,SAAS,CAaxD;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO,CAUnD;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO,CAE/C;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO,CAElD;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,OAAO,CAE1D;AAED,wBAAiB,IAAI,CAAC,GAAG,EAAE,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,CAOtD;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAa/E;AAGD,wBAAgB,IAAI,CAAC,GAAG,EAAE,SAAS,GAAG,MAAM,CAE3C;AAGD,wBAAgB,IAAI,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,SAAS,CAE1D;AAGD,wBAAgB,QAAQ,CAAC,GAAG,EAAE,SAAS,GAAG,MAAM,CAE/C"}