js-chess-engine 1.0.2 → 2.0.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 (103) hide show
  1. package/README.md +586 -205
  2. package/dist/adapters/APIAdapter.d.ts +88 -0
  3. package/dist/adapters/APIAdapter.d.ts.map +1 -0
  4. package/dist/adapters/APIAdapter.js +225 -0
  5. package/dist/adapters/APIAdapter.js.map +1 -0
  6. package/dist/ai/AIEngine.d.ts +42 -0
  7. package/dist/ai/AIEngine.d.ts.map +1 -0
  8. package/dist/ai/AIEngine.js +62 -0
  9. package/dist/ai/AIEngine.js.map +1 -0
  10. package/dist/ai/Evaluator.d.ts +48 -0
  11. package/dist/ai/Evaluator.d.ts.map +1 -0
  12. package/dist/ai/Evaluator.js +248 -0
  13. package/dist/ai/Evaluator.js.map +1 -0
  14. package/dist/ai/MoveOrdering.d.ts +60 -0
  15. package/dist/ai/MoveOrdering.d.ts.map +1 -0
  16. package/dist/ai/MoveOrdering.js +173 -0
  17. package/dist/ai/MoveOrdering.js.map +1 -0
  18. package/dist/ai/Search.d.ts +62 -0
  19. package/dist/ai/Search.d.ts.map +1 -0
  20. package/dist/ai/Search.js +340 -0
  21. package/dist/ai/Search.js.map +1 -0
  22. package/dist/ai/TranspositionTable.d.ts +100 -0
  23. package/dist/ai/TranspositionTable.d.ts.map +1 -0
  24. package/dist/ai/TranspositionTable.js +176 -0
  25. package/dist/ai/TranspositionTable.js.map +1 -0
  26. package/dist/core/AttackDetector.d.ts +52 -0
  27. package/dist/core/AttackDetector.d.ts.map +1 -0
  28. package/dist/core/AttackDetector.js +397 -0
  29. package/dist/core/AttackDetector.js.map +1 -0
  30. package/dist/core/Board.d.ts +109 -0
  31. package/dist/core/Board.d.ts.map +1 -0
  32. package/dist/core/Board.js +410 -0
  33. package/dist/core/Board.js.map +1 -0
  34. package/dist/core/MoveGenerator.d.ts +48 -0
  35. package/dist/core/MoveGenerator.d.ts.map +1 -0
  36. package/dist/core/MoveGenerator.js +678 -0
  37. package/dist/core/MoveGenerator.js.map +1 -0
  38. package/dist/core/Position.d.ts +135 -0
  39. package/dist/core/Position.d.ts.map +1 -0
  40. package/dist/core/Position.js +351 -0
  41. package/dist/core/Position.js.map +1 -0
  42. package/dist/core/index.d.ts +9 -0
  43. package/dist/core/index.d.ts.map +1 -0
  44. package/dist/core/index.js +25 -0
  45. package/dist/core/index.js.map +1 -0
  46. package/dist/core/zobrist.d.ts +93 -0
  47. package/dist/core/zobrist.d.ts.map +1 -0
  48. package/dist/core/zobrist.js +273 -0
  49. package/dist/core/zobrist.js.map +1 -0
  50. package/dist/index.d.ts +154 -0
  51. package/dist/index.d.ts.map +1 -0
  52. package/dist/index.js +353 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/types/ai.types.d.ts +97 -0
  55. package/dist/types/ai.types.d.ts.map +1 -0
  56. package/dist/types/ai.types.js +17 -0
  57. package/dist/types/ai.types.js.map +1 -0
  58. package/dist/types/board.types.d.ts +140 -0
  59. package/dist/types/board.types.d.ts.map +1 -0
  60. package/dist/types/board.types.js +34 -0
  61. package/dist/types/board.types.js.map +1 -0
  62. package/dist/types/index.d.ts +7 -0
  63. package/dist/types/index.d.ts.map +1 -0
  64. package/dist/types/index.js +26 -0
  65. package/dist/types/index.js.map +1 -0
  66. package/dist/types/move.types.d.ts +70 -0
  67. package/dist/types/move.types.d.ts.map +1 -0
  68. package/dist/types/move.types.js +53 -0
  69. package/dist/types/move.types.js.map +1 -0
  70. package/dist/utils/constants.d.ts +123 -0
  71. package/dist/utils/constants.d.ts.map +1 -0
  72. package/dist/utils/constants.js +259 -0
  73. package/dist/utils/constants.js.map +1 -0
  74. package/dist/utils/conversion.d.ts +152 -0
  75. package/dist/utils/conversion.d.ts.map +1 -0
  76. package/dist/utils/conversion.js +288 -0
  77. package/dist/utils/conversion.js.map +1 -0
  78. package/dist/utils/environment.d.ts +33 -0
  79. package/dist/utils/environment.d.ts.map +1 -0
  80. package/dist/utils/environment.js +71 -0
  81. package/dist/utils/environment.js.map +1 -0
  82. package/dist/utils/fen.d.ts +28 -0
  83. package/dist/utils/fen.d.ts.map +1 -0
  84. package/dist/utils/fen.js +203 -0
  85. package/dist/utils/fen.js.map +1 -0
  86. package/package.json +31 -29
  87. package/.eslintrc.json +0 -16
  88. package/.github/workflows/main.yml +0 -20
  89. package/CHANGELOG.md +0 -587
  90. package/dist/js-chess-engine.js +0 -1
  91. package/example/aiMatch.mjs +0 -21
  92. package/example/console.mjs +0 -37
  93. package/example/server.mjs +0 -27
  94. package/lib/Board.mjs +0 -943
  95. package/lib/const/board.mjs +0 -838
  96. package/lib/js-chess-engine.mjs +0 -99
  97. package/lib/utils.mjs +0 -154
  98. package/test/.eslintrc.json +0 -11
  99. package/test/ai.test.mjs +0 -132
  100. package/test/badge.svg +0 -1
  101. package/test/importExport.mjs +0 -108
  102. package/test/moves.test.mjs +0 -773
  103. package/webpack.config.js +0 -12
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Zobrist hashing for position identification
3
+ *
4
+ * Zobrist hashing provides a fast way to uniquely identify board positions
5
+ * for use in the transposition table. Each piece on each square gets a random
6
+ * 64-bit number, and the hash is the XOR of all piece positions plus state.
7
+ */
8
+ import { Piece, SquareIndex, InternalBoard } from '../types';
9
+ /**
10
+ * Initialize Zobrist hash tables with random 64-bit numbers
11
+ *
12
+ * This should be called once at startup. Uses a simple PRNG seeded
13
+ * with a fixed value for deterministic hashing.
14
+ */
15
+ export declare function initZobrist(): void;
16
+ /**
17
+ * Compute the Zobrist hash for a board position
18
+ *
19
+ * @param board - Board to hash
20
+ * @returns 64-bit Zobrist hash
21
+ */
22
+ export declare function computeZobristHash(board: InternalBoard): bigint;
23
+ /**
24
+ * Update hash after moving a piece
25
+ *
26
+ * This is more efficient than recomputing the entire hash.
27
+ *
28
+ * @param hash - Current hash
29
+ * @param piece - Piece being moved
30
+ * @param from - Source square
31
+ * @param to - Destination square
32
+ * @returns Updated hash
33
+ */
34
+ export declare function updateHashMove(hash: bigint, piece: Piece, from: SquareIndex, to: SquareIndex): bigint;
35
+ /**
36
+ * Update hash after capturing a piece
37
+ *
38
+ * @param hash - Current hash
39
+ * @param capturedPiece - Piece being captured
40
+ * @param square - Square where capture occurred
41
+ * @returns Updated hash
42
+ */
43
+ export declare function updateHashCapture(hash: bigint, capturedPiece: Piece, square: SquareIndex): bigint;
44
+ /**
45
+ * Toggle side to move in hash
46
+ *
47
+ * @param hash - Current hash
48
+ * @returns Updated hash with toggled side
49
+ */
50
+ export declare function toggleSide(hash: bigint): bigint;
51
+ /**
52
+ * Update hash for castling rights change
53
+ *
54
+ * @param hash - Current hash
55
+ * @param whiteShortOld - Old white short castling right
56
+ * @param whiteShortNew - New white short castling right
57
+ * @param whiteLongOld - Old white long castling right
58
+ * @param whiteLongNew - New white long castling right
59
+ * @param blackShortOld - Old black short castling right
60
+ * @param blackShortNew - New black short castling right
61
+ * @param blackLongOld - Old black long castling right
62
+ * @param blackLongNew - New black long castling right
63
+ * @returns Updated hash
64
+ */
65
+ export declare function updateHashCastling(hash: bigint, whiteShortOld: boolean, whiteShortNew: boolean, whiteLongOld: boolean, whiteLongNew: boolean, blackShortOld: boolean, blackShortNew: boolean, blackLongOld: boolean, blackLongNew: boolean): bigint;
66
+ /**
67
+ * Update hash for en passant square change
68
+ *
69
+ * @param hash - Current hash
70
+ * @param oldSquare - Old en passant square (or null)
71
+ * @param newSquare - New en passant square (or null)
72
+ * @returns Updated hash
73
+ */
74
+ export declare function updateHashEnPassant(hash: bigint, oldSquare: SquareIndex | null, newSquare: SquareIndex | null): bigint;
75
+ /**
76
+ * Add a piece to the hash
77
+ *
78
+ * @param hash - Current hash
79
+ * @param piece - Piece to add
80
+ * @param square - Square where piece is added
81
+ * @returns Updated hash
82
+ */
83
+ export declare function addPieceToHash(hash: bigint, piece: Piece, square: SquareIndex): bigint;
84
+ /**
85
+ * Remove a piece from the hash
86
+ *
87
+ * @param hash - Current hash
88
+ * @param piece - Piece to remove
89
+ * @param square - Square where piece is removed
90
+ * @returns Updated hash
91
+ */
92
+ export declare function removePieceFromHash(hash: bigint, piece: Piece, square: SquareIndex): bigint;
93
+ //# sourceMappingURL=zobrist.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zobrist.d.ts","sourceRoot":"","sources":["../../src/core/zobrist.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,KAAK,EAAE,WAAW,EAAiB,aAAa,EAAE,MAAM,UAAU,CAAC;AAmC5E;;;;;GAKG;AACH,wBAAgB,WAAW,IAAI,IAAI,CA0ClC;AAID;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,CAyC/D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC1B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,WAAW,EACjB,EAAE,EAAE,WAAW,GAChB,MAAM,CAYR;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC7B,IAAI,EAAE,MAAM,EACZ,aAAa,EAAE,KAAK,EACpB,MAAM,EAAE,WAAW,GACpB,MAAM,CASR;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM/C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAC9B,IAAI,EAAE,MAAM,EACZ,aAAa,EAAE,OAAO,EACtB,aAAa,EAAE,OAAO,EACtB,YAAY,EAAE,OAAO,EACrB,YAAY,EAAE,OAAO,EACrB,aAAa,EAAE,OAAO,EACtB,aAAa,EAAE,OAAO,EACtB,YAAY,EAAE,OAAO,EACrB,YAAY,EAAE,OAAO,GACtB,MAAM,CAkBR;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAC/B,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,WAAW,GAAG,IAAI,EAC7B,SAAS,EAAE,WAAW,GAAG,IAAI,GAC9B,MAAM,CAkBR;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,GAAG,MAAM,CAMtF;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,GAAG,MAAM,CAM3F"}
@@ -0,0 +1,273 @@
1
+ "use strict";
2
+ /**
3
+ * Zobrist hashing for position identification
4
+ *
5
+ * Zobrist hashing provides a fast way to uniquely identify board positions
6
+ * for use in the transposition table. Each piece on each square gets a random
7
+ * 64-bit number, and the hash is the XOR of all piece positions plus state.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.initZobrist = initZobrist;
11
+ exports.computeZobristHash = computeZobristHash;
12
+ exports.updateHashMove = updateHashMove;
13
+ exports.updateHashCapture = updateHashCapture;
14
+ exports.toggleSide = toggleSide;
15
+ exports.updateHashCastling = updateHashCastling;
16
+ exports.updateHashEnPassant = updateHashEnPassant;
17
+ exports.addPieceToHash = addPieceToHash;
18
+ exports.removePieceFromHash = removePieceFromHash;
19
+ const types_1 = require("../types");
20
+ const constants_1 = require("../utils/constants");
21
+ // ==================== Zobrist Key Tables ====================
22
+ /**
23
+ * Random 64-bit numbers for Zobrist hashing
24
+ * [piece][square] -> random bigint
25
+ */
26
+ let pieceKeys = [];
27
+ /**
28
+ * Random number for side to move (white)
29
+ */
30
+ let sideKey = 0n;
31
+ /**
32
+ * Random numbers for castling rights
33
+ * [0] = white short, [1] = white long, [2] = black short, [3] = black long
34
+ */
35
+ let castlingKeys = [];
36
+ /**
37
+ * Random numbers for en passant file
38
+ * [file] -> random bigint (8 files)
39
+ */
40
+ let enPassantKeys = [];
41
+ /**
42
+ * Flag to check if Zobrist keys are initialized
43
+ */
44
+ let initialized = false;
45
+ // ==================== Initialization ====================
46
+ /**
47
+ * Initialize Zobrist hash tables with random 64-bit numbers
48
+ *
49
+ * This should be called once at startup. Uses a simple PRNG seeded
50
+ * with a fixed value for deterministic hashing.
51
+ */
52
+ function initZobrist() {
53
+ if (initialized) {
54
+ return;
55
+ }
56
+ // Initialize pseudo-random number generator with seed
57
+ let seed = 12345n;
58
+ const rand64 = () => {
59
+ // Simple XORShift64 PRNG
60
+ seed ^= seed << 13n;
61
+ seed ^= seed >> 7n;
62
+ seed ^= seed << 17n;
63
+ return seed;
64
+ };
65
+ // Initialize piece keys [piece type][square]
66
+ pieceKeys = [];
67
+ for (let piece = 0; piece <= 12; piece++) {
68
+ pieceKeys[piece] = [];
69
+ for (let square = 0; square < constants_1.TOTAL_SQUARES; square++) {
70
+ pieceKeys[piece][square] = rand64();
71
+ }
72
+ }
73
+ // Initialize side key (for white to move)
74
+ sideKey = rand64();
75
+ // Initialize castling keys
76
+ castlingKeys = [
77
+ rand64(), // white short
78
+ rand64(), // white long
79
+ rand64(), // black short
80
+ rand64(), // black long
81
+ ];
82
+ // Initialize en passant keys (one per file)
83
+ enPassantKeys = [];
84
+ for (let file = 0; file < 8; file++) {
85
+ enPassantKeys[file] = rand64();
86
+ }
87
+ initialized = true;
88
+ }
89
+ // ==================== Hash Computation ====================
90
+ /**
91
+ * Compute the Zobrist hash for a board position
92
+ *
93
+ * @param board - Board to hash
94
+ * @returns 64-bit Zobrist hash
95
+ */
96
+ function computeZobristHash(board) {
97
+ if (!initialized) {
98
+ initZobrist();
99
+ }
100
+ let hash = 0n;
101
+ // XOR piece positions
102
+ for (let square = 0; square < constants_1.TOTAL_SQUARES; square++) {
103
+ const piece = board.mailbox[square];
104
+ if (piece !== types_1.Piece.EMPTY) {
105
+ hash ^= pieceKeys[piece][square];
106
+ }
107
+ }
108
+ // XOR side to move (if white)
109
+ if (board.turn === types_1.InternalColor.WHITE) {
110
+ hash ^= sideKey;
111
+ }
112
+ // XOR castling rights
113
+ if (board.castlingRights.whiteShort) {
114
+ hash ^= castlingKeys[0];
115
+ }
116
+ if (board.castlingRights.whiteLong) {
117
+ hash ^= castlingKeys[1];
118
+ }
119
+ if (board.castlingRights.blackShort) {
120
+ hash ^= castlingKeys[2];
121
+ }
122
+ if (board.castlingRights.blackLong) {
123
+ hash ^= castlingKeys[3];
124
+ }
125
+ // XOR en passant square
126
+ if (board.enPassantSquare !== null) {
127
+ const file = board.enPassantSquare % 8;
128
+ hash ^= enPassantKeys[file];
129
+ }
130
+ return hash;
131
+ }
132
+ /**
133
+ * Update hash after moving a piece
134
+ *
135
+ * This is more efficient than recomputing the entire hash.
136
+ *
137
+ * @param hash - Current hash
138
+ * @param piece - Piece being moved
139
+ * @param from - Source square
140
+ * @param to - Destination square
141
+ * @returns Updated hash
142
+ */
143
+ function updateHashMove(hash, piece, from, to) {
144
+ if (!initialized) {
145
+ initZobrist();
146
+ }
147
+ // Remove piece from old square
148
+ hash ^= pieceKeys[piece][from];
149
+ // Add piece to new square
150
+ hash ^= pieceKeys[piece][to];
151
+ return hash;
152
+ }
153
+ /**
154
+ * Update hash after capturing a piece
155
+ *
156
+ * @param hash - Current hash
157
+ * @param capturedPiece - Piece being captured
158
+ * @param square - Square where capture occurred
159
+ * @returns Updated hash
160
+ */
161
+ function updateHashCapture(hash, capturedPiece, square) {
162
+ if (!initialized) {
163
+ initZobrist();
164
+ }
165
+ // Remove captured piece
166
+ hash ^= pieceKeys[capturedPiece][square];
167
+ return hash;
168
+ }
169
+ /**
170
+ * Toggle side to move in hash
171
+ *
172
+ * @param hash - Current hash
173
+ * @returns Updated hash with toggled side
174
+ */
175
+ function toggleSide(hash) {
176
+ if (!initialized) {
177
+ initZobrist();
178
+ }
179
+ return hash ^ sideKey;
180
+ }
181
+ /**
182
+ * Update hash for castling rights change
183
+ *
184
+ * @param hash - Current hash
185
+ * @param whiteShortOld - Old white short castling right
186
+ * @param whiteShortNew - New white short castling right
187
+ * @param whiteLongOld - Old white long castling right
188
+ * @param whiteLongNew - New white long castling right
189
+ * @param blackShortOld - Old black short castling right
190
+ * @param blackShortNew - New black short castling right
191
+ * @param blackLongOld - Old black long castling right
192
+ * @param blackLongNew - New black long castling right
193
+ * @returns Updated hash
194
+ */
195
+ function updateHashCastling(hash, whiteShortOld, whiteShortNew, whiteLongOld, whiteLongNew, blackShortOld, blackShortNew, blackLongOld, blackLongNew) {
196
+ if (!initialized) {
197
+ initZobrist();
198
+ }
199
+ // XOR out old castling rights
200
+ if (whiteShortOld)
201
+ hash ^= castlingKeys[0];
202
+ if (whiteLongOld)
203
+ hash ^= castlingKeys[1];
204
+ if (blackShortOld)
205
+ hash ^= castlingKeys[2];
206
+ if (blackLongOld)
207
+ hash ^= castlingKeys[3];
208
+ // XOR in new castling rights
209
+ if (whiteShortNew)
210
+ hash ^= castlingKeys[0];
211
+ if (whiteLongNew)
212
+ hash ^= castlingKeys[1];
213
+ if (blackShortNew)
214
+ hash ^= castlingKeys[2];
215
+ if (blackLongNew)
216
+ hash ^= castlingKeys[3];
217
+ return hash;
218
+ }
219
+ /**
220
+ * Update hash for en passant square change
221
+ *
222
+ * @param hash - Current hash
223
+ * @param oldSquare - Old en passant square (or null)
224
+ * @param newSquare - New en passant square (or null)
225
+ * @returns Updated hash
226
+ */
227
+ function updateHashEnPassant(hash, oldSquare, newSquare) {
228
+ if (!initialized) {
229
+ initZobrist();
230
+ }
231
+ // XOR out old en passant
232
+ if (oldSquare !== null) {
233
+ const oldFile = oldSquare % 8;
234
+ hash ^= enPassantKeys[oldFile];
235
+ }
236
+ // XOR in new en passant
237
+ if (newSquare !== null) {
238
+ const newFile = newSquare % 8;
239
+ hash ^= enPassantKeys[newFile];
240
+ }
241
+ return hash;
242
+ }
243
+ /**
244
+ * Add a piece to the hash
245
+ *
246
+ * @param hash - Current hash
247
+ * @param piece - Piece to add
248
+ * @param square - Square where piece is added
249
+ * @returns Updated hash
250
+ */
251
+ function addPieceToHash(hash, piece, square) {
252
+ if (!initialized) {
253
+ initZobrist();
254
+ }
255
+ return hash ^ pieceKeys[piece][square];
256
+ }
257
+ /**
258
+ * Remove a piece from the hash
259
+ *
260
+ * @param hash - Current hash
261
+ * @param piece - Piece to remove
262
+ * @param square - Square where piece is removed
263
+ * @returns Updated hash
264
+ */
265
+ function removePieceFromHash(hash, piece, square) {
266
+ if (!initialized) {
267
+ initZobrist();
268
+ }
269
+ return hash ^ pieceKeys[piece][square];
270
+ }
271
+ // Initialize on module load
272
+ initZobrist();
273
+ //# sourceMappingURL=zobrist.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zobrist.js","sourceRoot":"","sources":["../../src/core/zobrist.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AA2CH,kCA0CC;AAUD,gDAyCC;AAaD,wCAiBC;AAUD,8CAaC;AAQD,gCAMC;AAgBD,gDA4BC;AAUD,kDAsBC;AAUD,wCAMC;AAUD,kDAMC;AArTD,oCAA4E;AAC5E,kDAAmD;AAEnD,+DAA+D;AAE/D;;;GAGG;AACH,IAAI,SAAS,GAAe,EAAE,CAAC;AAE/B;;GAEG;AACH,IAAI,OAAO,GAAW,EAAE,CAAC;AAEzB;;;GAGG;AACH,IAAI,YAAY,GAAa,EAAE,CAAC;AAEhC;;;GAGG;AACH,IAAI,aAAa,GAAa,EAAE,CAAC;AAEjC;;GAEG;AACH,IAAI,WAAW,GAAG,KAAK,CAAC;AAExB,2DAA2D;AAE3D;;;;;GAKG;AACH,SAAgB,WAAW;IACvB,IAAI,WAAW,EAAE,CAAC;QACd,OAAO;IACX,CAAC;IAED,sDAAsD;IACtD,IAAI,IAAI,GAAG,MAAM,CAAC;IAClB,MAAM,MAAM,GAAG,GAAW,EAAE;QACxB,yBAAyB;QACzB,IAAI,IAAI,IAAI,IAAI,GAAG,CAAC;QACpB,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;QACnB,IAAI,IAAI,IAAI,IAAI,GAAG,CAAC;QACpB,OAAO,IAAI,CAAC;IAChB,CAAC,CAAC;IAEF,6CAA6C;IAC7C,SAAS,GAAG,EAAE,CA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,OAAO,IAAI,CAAC;AAChB,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,mBAAmB,CAC/B,IAAY,EACZ,SAA6B,EAC7B,SAA6B;IAE7B,IAAI,CAAC,WAAW,EAAE,CAAC;QACf,WAAW,EAAE,CAAC;IAClB,CAAC;IAED,yBAAyB;IACzB,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACrB,MAAM,OAAO,GAAG,SAAS,GAAG,CAAC,CAAC;QAC9B,IAAI,IAAI,aAAa,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IAED,wBAAwB;IACxB,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACrB,MAAM,OAAO,GAAG,SAAS,GAAG,CAAC,CAAC;QAC9B,IAAI,IAAI,aAAa,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IAED,OAAO,IAAI,CAAC;AAChB,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,cAAc,CAAC,IAAY,EAAE,KAAY,EAAE,MAAmB;IAC1E,IAAI,CAAC,WAAW,EAAE,CAAC;QACf,WAAW,EAAE,CAAC;IAClB,CAAC;IAED,OAAO,IAAI,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC;AAC3C,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,mBAAmB,CAAC,IAAY,EAAE,KAAY,EAAE,MAAmB;IAC/E,IAAI,CAAC,WAAW,EAAE,CAAC;QACf,WAAW,EAAE,CAAC;IAClB,CAAC;IAED,OAAO,IAAI,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC;AAC3C,CAAC;AAED,4BAA4B;AAC5B,WAAW,EAAE,CAAC"}
@@ -0,0 +1,154 @@
1
+ /**
2
+ * js-chess-engine v2
3
+ *
4
+ * Public API for chess game management
5
+ */
6
+ import { BoardConfig, MovesMap, PieceSymbol, HistoryEntry } from './types';
7
+ export * from './types';
8
+ /**
9
+ * Main Game class - manages chess game state and moves
10
+ */
11
+ export declare class Game {
12
+ private board;
13
+ private history;
14
+ private aiEngine;
15
+ /**
16
+ * Create a new game
17
+ *
18
+ * @param configuration - Optional board configuration (JSON object, FEN string, or undefined for new game)
19
+ */
20
+ constructor(configuration?: BoardConfig | string);
21
+ /**
22
+ * Make a move
23
+ *
24
+ * @param from - From square (case-insensitive, e.g., 'E2' or 'e2')
25
+ * @param to - To square (case-insensitive, e.g., 'E4' or 'e4')
26
+ * @returns Board configuration after the move
27
+ */
28
+ move(from: string, to: string): BoardConfig;
29
+ /**
30
+ * Get all legal moves, optionally filtered by from-square
31
+ *
32
+ * @param from - Optional from square to filter moves
33
+ * @returns Map of from-squares to array of to-squares
34
+ */
35
+ moves(from?: string): MovesMap;
36
+ /**
37
+ * Set a piece on a square
38
+ *
39
+ * @param square - Square to place piece (case-insensitive)
40
+ * @param piece - Piece symbol (K, Q, R, B, N, P, k, q, r, b, n, p)
41
+ */
42
+ setPiece(square: string, piece: PieceSymbol): void;
43
+ /**
44
+ * Remove a piece from a square
45
+ *
46
+ * @param square - Square to remove piece from (case-insensitive)
47
+ */
48
+ removePiece(square: string): void;
49
+ /**
50
+ * Get move history
51
+ *
52
+ * @returns Array of history entries with board state after each move
53
+ */
54
+ getHistory(): Array<BoardConfig & {
55
+ move: HistoryEntry;
56
+ }>;
57
+ /**
58
+ * Export current board state as JSON configuration
59
+ *
60
+ * @returns Board configuration object
61
+ */
62
+ exportJson(): BoardConfig;
63
+ /**
64
+ * Export current board state as FEN string
65
+ *
66
+ * @returns FEN string
67
+ */
68
+ exportFEN(): string;
69
+ /**
70
+ * Print board to console (Unicode chess pieces)
71
+ */
72
+ printToConsole(): void;
73
+ /**
74
+ * Make an AI move (v1 compatible - returns only the move)
75
+ *
76
+ * @param level - AI level (1-5, default 3)
77
+ * @returns The played move object (e.g., {"E2": "E4"})
78
+ */
79
+ aiMove(level?: number): HistoryEntry;
80
+ /**
81
+ * Make an AI move and return both move and board state
82
+ *
83
+ * @param options - Optional configuration object
84
+ * @param options.level - AI difficulty level (1-5, default: 3)
85
+ * @param options.play - Whether to apply the move to the game (default: true). If false, only returns the move without modifying game state.
86
+ * @param options.ttSizeMB - Transposition table size in MB (0 to disable, 0.25-256). Default: auto-scaled by level (e.g., level 3: 16 MB Node.js, 2 MB browser)
87
+ * @returns Object containing the move and board configuration (current state if play=false, updated state if play=true)
88
+ */
89
+ ai(options?: {
90
+ level?: number;
91
+ play?: boolean;
92
+ ttSizeMB?: number;
93
+ }): {
94
+ move: HistoryEntry;
95
+ board: BoardConfig;
96
+ };
97
+ }
98
+ /**
99
+ * Get all legal moves for a position
100
+ *
101
+ * @param config - Board configuration or FEN string
102
+ * @returns Map of from-squares to array of to-squares
103
+ */
104
+ export declare function moves(config: BoardConfig | string): MovesMap;
105
+ /**
106
+ * Get board status
107
+ *
108
+ * @param config - Board configuration or FEN string
109
+ * @returns Board configuration with current status
110
+ */
111
+ export declare function status(config: BoardConfig | string): BoardConfig;
112
+ /**
113
+ * Get FEN string for a position
114
+ *
115
+ * @param config - Board configuration or FEN string
116
+ * @returns FEN string
117
+ */
118
+ export declare function getFen(config: BoardConfig | string): string;
119
+ /**
120
+ * Make a move on a board
121
+ *
122
+ * @param config - Board configuration or FEN string
123
+ * @param from - From square
124
+ * @param to - To square
125
+ * @returns Board configuration after the move
126
+ */
127
+ export declare function move(config: BoardConfig | string, from: string, to: string): BoardConfig;
128
+ /**
129
+ * Make an AI move (v1 compatible - returns only the move)
130
+ *
131
+ * @param config - Board configuration or FEN string
132
+ * @param level - AI level (1-5, default 3)
133
+ * @returns The played move object (e.g., {"E2": "E4"})
134
+ */
135
+ export declare function aiMove(config: BoardConfig | string, level?: number): HistoryEntry;
136
+ /**
137
+ * Make an AI move and return both move and board state
138
+ *
139
+ * @param config - Board configuration or FEN string
140
+ * @param options - Optional configuration object
141
+ * @param options.level - AI difficulty level (1-5, default: 3)
142
+ * @param options.play - Whether to apply the move to the game (default: true). If false, only returns the move without modifying game state.
143
+ * @param options.ttSizeMB - Transposition table size in MB (0 to disable, 0.25-256). Default: auto-scaled by level (e.g., level 3: 16 MB Node.js, 2 MB browser)
144
+ * @returns Object containing the move and board configuration (current state if play=false, updated state if play=true)
145
+ */
146
+ export declare function ai(config: BoardConfig | string, options?: {
147
+ level?: number;
148
+ play?: boolean;
149
+ ttSizeMB?: number;
150
+ }): {
151
+ move: HistoryEntry;
152
+ board: BoardConfig;
153
+ };
154
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAEH,WAAW,EACX,QAAQ,EAER,WAAW,EACX,YAAY,EACf,MAAM,SAAS,CAAC;AAkBjB,cAAc,SAAS,CAAC;AAExB;;GAEG;AACH,qBAAa,IAAI;IACb,OAAO,CAAC,KAAK,CAAgB;IAC7B,OAAO,CAAC,OAAO,CAAsB;IACrC,OAAO,CAAC,QAAQ,CAAW;IAE3B;;;;OAIG;gBACS,aAAa,CAAC,EAAE,WAAW,GAAG,MAAM;IAehD;;;;;;OAMG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,WAAW;IAyB3C;;;;;OAKG;IACH,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,QAAQ;IAa9B;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,IAAI;IAOlD;;;;OAIG;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAMjC;;;;OAIG;IACH,UAAU,IAAI,KAAK,CAAC,WAAW,GAAG;QAAE,IAAI,EAAE,YAAY,CAAA;KAAE,CAAC;IA4BzD;;;;OAIG;IACH,UAAU,IAAI,WAAW;IAIzB;;;;OAIG;IACH,SAAS,IAAI,MAAM;IAInB;;OAEG;IACH,cAAc,IAAI,IAAI;IAqBtB;;;;;OAKG;IACH,MAAM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