gigachess 0.2.1 → 0.3.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.
@@ -1 +1 @@
1
- {"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../../src/chessops/convert.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,KAAK,EAAE,IAAI,IAAI,UAAU,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EAAE,IAAI,IAAI,KAAK,EAAE,KAAK,IAAI,MAAM,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,KAAK,EAAE,cAAc,IAAI,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,KAAK,IAAI,MAAM,EAAE,IAAI,IAAI,KAAK,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,KAAK,EAAE,IAAI,IAAI,UAAU,EAAE,UAAU,IAAI,gBAAgB,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAC5F,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAK3C,eAAO,MAAM,aAAa,GAAI,GAAG,MAAM,KAAG,MAAuD,CAAC;AAClG,eAAO,MAAM,eAAe,GAAI,GAAG,MAAM,KAAG,MAAkD,CAAC;AAC/F,eAAO,MAAM,YAAY,GAAI,GAAG,KAAK,KAAG,KAA6C,CAAC;AACtF,eAAO,MAAM,cAAc,GAAI,GAAG,KAAK,KAAG,KAAiB,CAAC;AAE5D,eAAO,MAAM,eAAe,GAAI,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,KAAK,CAAA;CAAE,KAAG,KAGlE,CAAC;AAEH,yEAAy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AK,GAAG,cAAc,CAU1D;AAED,yCAAyC;AACzC,wBAAgB,eAAe,CAAC,GAAG,EAAE,cAAc,GAAG,KAAK,CAiB1D;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU,CAoBhF;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,UAAU,GAAG,gBAAgB,CAI5D"}
@@ -17,12 +17,7 @@ export const pieceFromEngine = (p) => ({
17
17
  });
18
18
  /** Facade Board -> engine board, normalized to unsigned bit patterns. */
19
19
  export const boardToEngine = (b) => {
20
- const src = b._b;
21
- const out = {};
22
- for (const k of Object.keys(src)) {
23
- out[k] = { lo: src[k].lo >>> 0, hi: src[k].hi >>> 0 };
24
- }
25
- return out;
20
+ return engineBoardMod.cloneBoard(b._b);
26
21
  };
27
22
  export const boardFromEngine = (b) => {
28
23
  const facade = Board.empty();
package/dist/fen.d.ts.map CHANGED
@@ -1 +1 @@
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;AAkE1E,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,CA0R9G;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"}
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,CAgS9G;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,CA2F/F"}
package/dist/fen.js CHANGED
@@ -6,12 +6,35 @@ import { Color, Role, Err, Ok } from "./types.js";
6
6
  import { squareFile, squareRank, squareName } from "./util.js";
7
7
  import * as attacks from "./attacks.js";
8
8
  import { zobristTablesLoaded, calculateZobrist } from "./zobrist.js";
9
+ import { CASTLE_WK, CASTLE_WQ, CASTLE_BK, CASTLE_BQ, CASTLING_RIGHTS_TABLE, CASTLING_FEN_STR, } from "./castling.js";
9
10
  // helpers to create CastlingRights
10
11
  function makeCastling(whiteSet, blackSet) {
11
12
  const whiteKing = whiteSet.has(7);
12
13
  const whiteQueen = whiteSet.has(0);
13
14
  const blackKing = blackSet.has(63);
14
15
  const blackQueen = blackSet.has(56);
16
+ let isStd = true;
17
+ for (const s of whiteSet) {
18
+ if (s !== 7 && s !== 0) {
19
+ isStd = false;
20
+ break;
21
+ }
22
+ }
23
+ if (isStd) {
24
+ for (const s of blackSet) {
25
+ if (s !== 63 && s !== 56) {
26
+ isStd = false;
27
+ break;
28
+ }
29
+ }
30
+ }
31
+ if (isStd) {
32
+ const mask = (whiteKing ? CASTLE_WK : 0) |
33
+ (whiteQueen ? CASTLE_WQ : 0) |
34
+ (blackKing ? CASTLE_BK : 0) |
35
+ (blackQueen ? CASTLE_BQ : 0);
36
+ return CASTLING_RIGHTS_TABLE[mask];
37
+ }
15
38
  return {
16
39
  white: new Set(whiteSet),
17
40
  black: new Set(blackSet),
@@ -406,16 +429,23 @@ export function parseFen(fen, opts) {
406
429
  if (turn === Color.Black && whiteAttacked)
407
430
  return Err({ code: "fen/oppositeCheck" });
408
431
  }
409
- // castling validation extra: if not chess960 and castling rights refer to missing rook/king not on original? For now skip strict
432
+ const is960 = !!opts?.chess960;
433
+ const kingSqPair = [wkSq ?? -1, bkSq ?? -1];
434
+ curBoard.kingSq = kingSqPair;
435
+ const chk = attacks.kingAttackers(curBoard, turn);
410
436
  const setup = {
411
437
  board: curBoard,
412
438
  turn,
413
439
  castling,
440
+ castlingMask: castling.mask,
441
+ isChess960: is960 || castling.mask === undefined,
414
442
  epSquare,
415
443
  halfmoves: half,
416
444
  fullmoves: full,
417
445
  halfmove: half,
418
446
  fullmove: full,
447
+ kingSq: kingSqPair,
448
+ checkers: chk,
419
449
  };
420
450
  // Seed the initial Zobrist key when the Polyglot tables are already loaded
421
451
  // (lazy-loading contract — see src/zobrist.ts); makeMove then maintains it
@@ -474,60 +504,66 @@ export function makeFen(setup, opts) {
474
504
  let castlingStr = "";
475
505
  const chess960 = !!opts?.chess960;
476
506
  const shredder = !!opts?.shredder;
477
- const whiteSet = setup.castling.white;
478
- const blackSet = setup.castling.black;
479
- const hasWhite = whiteSet.size > 0;
480
- const hasBlack = blackSet.size > 0;
481
- if (!hasWhite && !hasBlack) {
482
- castlingStr = "-";
507
+ if (!chess960 && !shredder && !setup.isChess960 && (setup.castlingMask !== undefined || setup.castling.mask !== undefined)) {
508
+ const m = (setup.castlingMask ?? setup.castling.mask) & 0x0F;
509
+ castlingStr = CASTLING_FEN_STR[m];
483
510
  }
484
511
  else {
485
- if (!chess960) {
486
- // standard KQkq
487
- if (whiteSet.has(7))
488
- castlingStr += "K";
489
- if (whiteSet.has(0))
490
- castlingStr += "Q";
491
- if (blackSet.has(63))
492
- castlingStr += "k";
493
- if (blackSet.has(56))
494
- castlingStr += "q";
495
- if (castlingStr === "")
496
- castlingStr = "-";
512
+ const whiteSet = setup.castling.white;
513
+ const blackSet = setup.castling.black;
514
+ const hasWhite = whiteSet.size > 0;
515
+ const hasBlack = blackSet.size > 0;
516
+ if (!hasWhite && !hasBlack) {
517
+ castlingStr = "-";
497
518
  }
498
519
  else {
499
- if (shredder) {
500
- // Shredder: emit KQkq if rooks on standard squares else fallback to file letters
501
- let emitted = false;
502
- const canShredderWhiteK = whiteSet.has(7);
503
- const canShredderWhiteQ = whiteSet.has(0);
504
- const canShredderBlackK = blackSet.has(63);
505
- const canShredderBlackQ = blackSet.has(56);
506
- const isStandard = (canShredderWhiteK || canShredderWhiteQ || canShredderBlackK || canShredderBlackQ) &&
507
- [...whiteSet].every(s => s === 7 || s === 0) &&
508
- [...blackSet].every(s => s === 63 || s === 56);
509
- if (isStandard) {
510
- if (canShredderWhiteK)
511
- castlingStr += "K";
512
- if (canShredderWhiteQ)
513
- castlingStr += "Q";
514
- if (canShredderBlackK)
515
- castlingStr += "k";
516
- if (canShredderBlackQ)
517
- castlingStr += "q";
518
- emitted = true;
519
- }
520
- if (!emitted) {
521
- // fallback to X-FEN
522
- castlingStr = makeXFenString(setup);
523
- }
520
+ if (!chess960) {
521
+ // standard KQkq
522
+ if (whiteSet.has(7))
523
+ castlingStr += "K";
524
+ if (whiteSet.has(0))
525
+ castlingStr += "Q";
526
+ if (blackSet.has(63))
527
+ castlingStr += "k";
528
+ if (blackSet.has(56))
529
+ castlingStr += "q";
524
530
  if (castlingStr === "")
525
531
  castlingStr = "-";
526
532
  }
527
533
  else {
528
- castlingStr = makeXFenString(setup);
529
- if (castlingStr === "")
530
- castlingStr = "-";
534
+ if (shredder) {
535
+ // Shredder: emit KQkq if rooks on standard squares else fallback to file letters
536
+ let emitted = false;
537
+ const canShredderWhiteK = whiteSet.has(7);
538
+ const canShredderWhiteQ = whiteSet.has(0);
539
+ const canShredderBlackK = blackSet.has(63);
540
+ const canShredderBlackQ = blackSet.has(56);
541
+ const isStandard = (canShredderWhiteK || canShredderWhiteQ || canShredderBlackK || canShredderBlackQ) &&
542
+ [...whiteSet].every(s => s === 7 || s === 0) &&
543
+ [...blackSet].every(s => s === 63 || s === 56);
544
+ if (isStandard) {
545
+ if (canShredderWhiteK)
546
+ castlingStr += "K";
547
+ if (canShredderWhiteQ)
548
+ castlingStr += "Q";
549
+ if (canShredderBlackK)
550
+ castlingStr += "k";
551
+ if (canShredderBlackQ)
552
+ castlingStr += "q";
553
+ emitted = true;
554
+ }
555
+ if (!emitted) {
556
+ // fallback to X-FEN
557
+ castlingStr = makeXFenString(setup);
558
+ }
559
+ if (castlingStr === "")
560
+ castlingStr = "-";
561
+ }
562
+ else {
563
+ castlingStr = makeXFenString(setup);
564
+ if (castlingStr === "")
565
+ castlingStr = "-";
566
+ }
531
567
  }
532
568
  }
533
569
  }
package/dist/index.d.ts CHANGED
@@ -8,10 +8,11 @@ export * from "./pgn.js";
8
8
  export * from "./chess960.js";
9
9
  export * from "./zobrist.js";
10
10
  export * from "./packedMove.js";
11
- export { knightAttacks, kingAttacks, pawnAttacks, bishopAttacks, rookAttacks, queenAttacks, ensureMagicTablesLoaded, magicTablesLoaded, ray, between, isAttacked, } from "./attacks.js";
11
+ export { knightAttacks, kingAttacks, pawnAttacks, bishopAttacks, rookAttacks, queenAttacks, ensureMagicTablesLoaded, magicTablesLoaded, ray, between, lineRay, LINE_RAY_LO, LINE_RAY_HI, isAttacked, } from "./attacks.js";
12
12
  export { kingAttackers } from "./attacks.js";
13
- export { dests, allDests, detectCastling, isCheck, isCheckmate, isStalemate, isInsufficientMaterial, isFiftyMoveDraw, isSeventyFiveMoveDraw, isThreefoldRepetition, isLegal, perft, makeMove, play, INITIAL_FEN, } from "./chess.js";
14
- export type { CastlingPlan } from "./chess.js";
13
+ export { CASTLE_PATH_LO, CASTLE_PATH_HI, CASTLE_CLEAR_STD, } from "./castling.js";
14
+ export { dests, allDests, detectCastling, isCheck, isCheckmate, isStalemate, isInsufficientMaterial, isFiftyMoveDraw, isSeventyFiveMoveDraw, isThreefoldRepetition, isLegal, perft, countLegalMoves, legalMovesInto, forEachLegalMove, MoveCounter, makeMove, play, INITIAL_FEN, } from "./chess.js";
15
+ export type { CastlingPlan, Undo } from "./chess.js";
15
16
  export { pgnImport, buildTree, build, pgnExport, TreeWrapperImpl, } from "./chesstree.js";
16
17
  export type { TreeWrapper, TreeNode, Path, Comment, Glyph, Clock, Shape, Eval as TreeEval, Game, Player, AnalyseData, } from "./chesstree.js";
17
18
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAGhC,OAAO,EACL,aAAa,EACb,WAAW,EACX,WAAW,EACX,aAAa,EACb,WAAW,EACX,YAAY,EACZ,uBAAuB,EACvB,iBAAiB,EACjB,GAAG,EACH,OAAO,EACP,UAAU,GACX,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAG7C,OAAO,EACL,KAAK,EACL,QAAQ,EACR,cAAc,EACd,OAAO,EACP,WAAW,EACX,WAAW,EACX,sBAAsB,EACtB,eAAe,EACf,qBAAqB,EACrB,qBAAqB,EACrB,OAAO,EACP,KAAK,EACL,QAAQ,EACR,IAAI,EACJ,WAAW,GACZ,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG/C,OAAO,EACL,SAAS,EACT,SAAS,EACT,KAAK,EACL,SAAS,EACT,eAAe,GAChB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EACV,WAAW,EACX,QAAQ,EACR,IAAI,EACJ,OAAO,EACP,KAAK,EACL,KAAK,EACL,KAAK,EACL,IAAI,IAAI,QAAQ,EAChB,IAAI,EACJ,MAAM,EACN,WAAW,GACZ,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAGhC,OAAO,EACL,aAAa,EACb,WAAW,EACX,WAAW,EACX,aAAa,EACb,WAAW,EACX,YAAY,EACZ,uBAAuB,EACvB,iBAAiB,EACjB,GAAG,EACH,OAAO,EACP,OAAO,EACP,WAAW,EACX,WAAW,EACX,UAAU,GACX,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EACL,cAAc,EACd,cAAc,EACd,gBAAgB,GACjB,MAAM,eAAe,CAAC;AAGvB,OAAO,EACL,KAAK,EACL,QAAQ,EACR,cAAc,EACd,OAAO,EACP,WAAW,EACX,WAAW,EACX,sBAAsB,EACtB,eAAe,EACf,qBAAqB,EACrB,qBAAqB,EACrB,OAAO,EACP,KAAK,EACL,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,WAAW,EACX,QAAQ,EACR,IAAI,EACJ,WAAW,GACZ,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAGrD,OAAO,EACL,SAAS,EACT,SAAS,EACT,KAAK,EACL,SAAS,EACT,eAAe,GAChB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EACV,WAAW,EACX,QAAQ,EACR,IAAI,EACJ,OAAO,EACP,KAAK,EACL,KAAK,EACL,KAAK,EACL,IAAI,IAAI,QAAQ,EAChB,IAAI,EACJ,MAAM,EACN,WAAW,GACZ,MAAM,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -14,9 +14,10 @@ export * from "./chess960.js";
14
14
  export * from "./zobrist.js";
15
15
  export * from "./packedMove.js";
16
16
  // attacks: explicit to avoid duplicate kingAttackers with chess
17
- export { knightAttacks, kingAttacks, pawnAttacks, bishopAttacks, rookAttacks, queenAttacks, ensureMagicTablesLoaded, magicTablesLoaded, ray, between, isAttacked, } from "./attacks.js";
17
+ export { knightAttacks, kingAttacks, pawnAttacks, bishopAttacks, rookAttacks, queenAttacks, ensureMagicTablesLoaded, magicTablesLoaded, ray, between, lineRay, LINE_RAY_LO, LINE_RAY_HI, isAttacked, } from "./attacks.js";
18
18
  export { kingAttackers } from "./attacks.js";
19
+ export { CASTLE_PATH_LO, CASTLE_PATH_HI, CASTLE_CLEAR_STD, } from "./castling.js";
19
20
  // chess core (avoid duplicate Position and kingAttackers)
20
- export { dests, allDests, detectCastling, isCheck, isCheckmate, isStalemate, isInsufficientMaterial, isFiftyMoveDraw, isSeventyFiveMoveDraw, isThreefoldRepetition, isLegal, perft, makeMove, play, INITIAL_FEN, } from "./chess.js";
21
+ export { dests, allDests, detectCastling, isCheck, isCheckmate, isStalemate, isInsufficientMaterial, isFiftyMoveDraw, isSeventyFiveMoveDraw, isThreefoldRepetition, isLegal, perft, countLegalMoves, legalMovesInto, forEachLegalMove, MoveCounter, makeMove, play, INITIAL_FEN, } from "./chess.js";
21
22
  // chesstree integration (tree shapes + analysis API, folded into the root)
22
23
  export { pgnImport, buildTree, build, pgnExport, TreeWrapperImpl, } from "./chesstree.js";
@@ -1,5 +1,5 @@
1
1
  // src/packedMove.ts — 16-bit packed move encoding ("moves2", design D3)
2
- // 2-byte format matching blind-base's gigabase_moves.rs:
2
+ // 2-byte format matching gigachess 16-bit moves2 wire format:
3
3
  // bits 0..5 (6 bits) from square index (0..63, a1 = 0)
4
4
  // bits 6..11 (6 bits) to square index (0..63)
5
5
  // bits 12..15 (4 bits) promotion code (0 = none, 1 = N, 2 = B, 3 = R, 4 = Q)
package/dist/san.d.ts.map CHANGED
@@ -1 +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"}
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;AAiB7E,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,GAAG,MAAM,CAsJzD;AAGD,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CA6L3E;AAGD,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 CHANGED
@@ -3,45 +3,15 @@
3
3
  import * as sq from "./squareSet.js";
4
4
  import * as board from "./board.js";
5
5
  import * as chess from "./chess.js";
6
+ import * as attacks from "./attacks.js";
6
7
  import { Color, Role } from "./types.js";
7
8
  import { Err, Ok } from "./types.js";
8
9
  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
- }
10
+ const ROLE_SAN = ["", "N", "B", "R", "Q", "K"];
11
+ const roleToSanChar = (role) => ROLE_SAN[role] ?? "";
12
+ const SAN_ROLES = { K: Role.King, Q: Role.Queen, R: Role.Rook, B: Role.Bishop, N: Role.Knight };
13
+ const sanCharToRole = (ch) => SAN_ROLES[ch];
14
+ const promoCharToRole = (ch) => SAN_ROLES[ch.toUpperCase()];
45
15
  function normalizeCastlingSan(s) {
46
16
  // tolerant 0-0 -> O-O
47
17
  return s.replace(/0-0-0/g, "O-O-O").replace(/0-0/g, "O-O");
@@ -64,10 +34,12 @@ export function makeSan(move, pos) {
64
34
  let base = isKingSide ? "O-O" : "O-O-O";
65
35
  // add check suffix
66
36
  const next = chess.makeMove(pos, move);
67
- if (chess.isCheckmate(next))
68
- base += "#";
69
- else if (chess.isCheck(next))
70
- base += "+";
37
+ if (chess.isCheck(next)) {
38
+ if (chess.isCheckmate(next))
39
+ base += "#";
40
+ else
41
+ base += "+";
42
+ }
71
43
  return base;
72
44
  }
73
45
  const piece = board.pieceAt(pos.board, move.from);
@@ -105,37 +77,41 @@ export function makeSan(move, pos) {
105
77
  }
106
78
  // Disambiguation for non-pawn (and pawn capture already handled)
107
79
  if (piece.role !== Role.Pawn) {
108
- // Find all pieces of same role/color that can legally move to same destination
80
+ // Find other pieces of same role/color that can legally move to same destination
109
81
  const candidates = [];
110
82
  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();
83
+ let attackers;
84
+ switch (piece.role) {
85
+ case Role.Knight:
86
+ attackers = sq.and(attacks.knightAttacks(move.to), sq.and(own, pos.board.knight));
87
+ break;
88
+ case Role.Bishop:
89
+ attackers = sq.and(attacks.bishopAttacks(move.to, pos.board.occupied), sq.and(own, pos.board.bishop));
90
+ break;
91
+ case Role.Rook:
92
+ attackers = sq.and(attacks.rookAttacks(move.to, pos.board.occupied), sq.and(own, pos.board.rook));
93
+ break;
94
+ case Role.Queen:
95
+ attackers = sq.and(attacks.queenAttacks(move.to, pos.board.occupied), sq.and(own, pos.board.queen));
96
+ break;
97
+ case Role.King:
98
+ attackers = sq.and(attacks.kingAttacks(move.to), sq.and(own, pos.board.king));
99
+ break;
100
+ default:
101
+ attackers = sq.EMPTY;
102
+ break;
103
+ }
104
+ const otherAttackers = sq.minus(attackers, sq.singleton(move.from));
105
+ if (!sq.isEmpty(otherAttackers)) {
106
+ const ctx = chess.analyzeCheckContext(pos);
107
+ for (const sqIdx of sq.iter(otherAttackers)) {
108
+ const cand = board.pieceAt(pos.board, sqIdx);
109
+ if (!cand)
110
+ continue;
111
+ const d = chess.destsFast(pos, sqIdx, cand, ctx);
112
+ if (sq.has(d, move.to))
113
+ candidates.push(sqIdx);
120
114
  }
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
115
  }
140
116
  // Include moving piece itself, total candidates +1 = all that can reach dest
141
117
  // If there is any other candidate, need disambiguation
@@ -202,10 +178,12 @@ export function makeSan(move, pos) {
202
178
  }
203
179
  // Check / mate suffix
204
180
  const next = chess.makeMove(pos, move);
205
- if (chess.isCheckmate(next))
206
- san += "#";
207
- else if (chess.isCheck(next))
208
- san += "+";
181
+ if (chess.isCheck(next)) {
182
+ if (chess.isCheckmate(next))
183
+ san += "#";
184
+ else
185
+ san += "+";
186
+ }
209
187
  return san;
210
188
  }
211
189
  // ---------- parseSan ----------
@@ -314,120 +292,118 @@ export function parseSan(san, pos) {
314
292
  // For pawn, pieceRole is pawn, but rest may be file for capture (like "e" in exd5)
315
293
  // Our disamb handling already captured that: for "exd5", rest after removing piece (none) and capture, rest = "e" => disambFile = 'e' . Good.
316
294
  // 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;
295
+ // Targeted reverse attacker lookup
296
+ const us = pos.turn;
297
+ const own = us === Color.White ? pos.board.white : pos.board.black;
298
+ const candidateFroms = [];
299
+ if (pieceRole === Role.Pawn) {
300
+ // 1. Pawn pushes
301
+ const pushFrom = us === Color.White ? to - 8 : to + 8;
302
+ if (pushFrom >= 0 && pushFrom < 64 && !board.pieceAt(pos.board, to)) {
303
+ const p = board.pieceAt(pos.board, pushFrom);
304
+ if (p && p.color === us && p.role === Role.Pawn)
305
+ candidateFroms.push(pushFrom);
329
306
  }
330
- else {
331
- if (fromPiece.role !== Role.Pawn)
332
- continue;
307
+ if (us === Color.White && squareRank(to) === 3) {
308
+ const dblFrom = to - 16;
309
+ if (!board.pieceAt(pos.board, to) && !board.pieceAt(pos.board, to - 8)) {
310
+ const p = board.pieceAt(pos.board, dblFrom);
311
+ if (p && p.color === us && p.role === Role.Pawn)
312
+ candidateFroms.push(dblFrom);
313
+ }
333
314
  }
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;
315
+ else if (us === Color.Black && squareRank(to) === 4) {
316
+ const dblFrom = to + 16;
317
+ if (!board.pieceAt(pos.board, to) && !board.pieceAt(pos.board, to + 8)) {
318
+ const p = board.pieceAt(pos.board, dblFrom);
319
+ if (p && p.color === us && p.role === Role.Pawn)
320
+ candidateFroms.push(dblFrom);
321
+ }
345
322
  }
346
- else {
347
- if (m.isPromotion)
348
- continue;
323
+ // 2. Pawn diagonal attacks (normal captures or en-passant)
324
+ const pAtt = attacks.pawnAttacks(us === Color.White ? Color.Black : Color.White, to);
325
+ const pawnCaps = sq.and(pAtt, sq.and(own, pos.board.pawn));
326
+ for (const sqIdx of sq.iter(pawnCaps)) {
327
+ candidateFroms.push(sqIdx);
349
328
  }
350
- // disambiguation
351
- if (disambFile !== null) {
352
- const f = String.fromCharCode(97 + squareFile(m.from));
353
- if (f !== disambFile)
354
- continue;
329
+ }
330
+ else {
331
+ let attackers;
332
+ switch (pieceRole) {
333
+ case Role.Knight:
334
+ attackers = sq.and(attacks.knightAttacks(to), sq.and(own, pos.board.knight));
335
+ break;
336
+ case Role.Bishop:
337
+ attackers = sq.and(attacks.bishopAttacks(to, pos.board.occupied), sq.and(own, pos.board.bishop));
338
+ break;
339
+ case Role.Rook:
340
+ attackers = sq.and(attacks.rookAttacks(to, pos.board.occupied), sq.and(own, pos.board.rook));
341
+ break;
342
+ case Role.Queen:
343
+ attackers = sq.and(attacks.queenAttacks(to, pos.board.occupied), sq.and(own, pos.board.queen));
344
+ break;
345
+ case Role.King:
346
+ attackers = sq.and(attacks.kingAttacks(to), sq.and(own, pos.board.king));
347
+ break;
348
+ default:
349
+ attackers = sq.EMPTY;
350
+ break;
355
351
  }
356
- if (disambRank !== null) {
357
- const r = String.fromCharCode(49 + squareRank(m.from));
358
- if (r !== disambRank)
359
- continue;
352
+ for (const sqIdx of sq.iter(attackers)) {
353
+ candidateFroms.push(sqIdx);
360
354
  }
361
- candidates.push(m);
355
+ }
356
+ // Disambiguation filtering
357
+ let filteredFroms = candidateFroms;
358
+ if (disambFile !== null) {
359
+ const reqFile = disambFile.charCodeAt(0) - 97;
360
+ filteredFroms = filteredFroms.filter(f => squareFile(f) === reqFile);
361
+ }
362
+ if (disambRank !== null) {
363
+ const reqRank = disambRank.charCodeAt(0) - 49;
364
+ filteredFroms = filteredFroms.filter(f => squareRank(f) === reqRank);
365
+ }
366
+ if (filteredFroms.length === 0)
367
+ return Err({ code: "san/noLegalMove" });
368
+ // Legality filtering with single shared check context
369
+ const ctx = chess.analyzeCheckContext(pos);
370
+ const candidates = [];
371
+ const destRank = squareRank(to);
372
+ for (const from of filteredFroms) {
373
+ const fromPiece = board.pieceAt(pos.board, from);
374
+ if (!fromPiece)
375
+ continue;
376
+ const isPromo = fromPiece.role === Role.Pawn && (destRank === 0 || destRank === 7);
377
+ if (isPromo && promotion === undefined)
378
+ continue;
379
+ if (!isPromo && promotion !== undefined)
380
+ continue;
381
+ const d = chess.destsFast(pos, from, fromPiece, ctx);
382
+ if (!sq.has(d, to))
383
+ continue;
384
+ let isEnPassant = false;
385
+ if (fromPiece.role === Role.Pawn && pos.epSquare !== null && to === pos.epSquare) {
386
+ const fileDiff = Math.abs(squareFile(to) - squareFile(from));
387
+ const dir = fromPiece.color === Color.White ? 1 : -1;
388
+ if (fileDiff === 1 && destRank - squareRank(from) === dir)
389
+ isEnPassant = true;
390
+ }
391
+ candidates.push({
392
+ from,
393
+ to,
394
+ promotion: isPromo ? (promotion ?? Role.Queen) : null,
395
+ isPromotion: isPromo,
396
+ isEnPassant,
397
+ isCastling: false,
398
+ });
362
399
  }
363
400
  if (candidates.length === 0)
364
401
  return Err({ code: "san/noLegalMove" });
365
402
  if (candidates.length > 1)
366
403
  return Err({ code: "san/ambiguous" });
367
404
  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
405
  return Ok(chosen);
383
406
  }
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
407
  // ---------- UCI ----------
432
408
  export function parseUci(uci) {
433
409
  const s = uci.trim();