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.
- package/LICENSE +21 -0
- package/README.md +183 -0
- package/dist/attacks.d.ts +40 -0
- package/dist/attacks.d.ts.map +1 -0
- package/dist/attacks.js +389 -0
- package/dist/bishopMagicBlob.d.ts +2 -0
- package/dist/bishopMagicBlob.d.ts.map +1 -0
- package/dist/bishopMagicBlob.js +11 -0
- package/dist/board.d.ts +73 -0
- package/dist/board.d.ts.map +1 -0
- package/dist/board.js +237 -0
- package/dist/chess.d.ts +215 -0
- package/dist/chess.d.ts.map +1 -0
- package/dist/chess.js +1453 -0
- package/dist/chess960.d.ts +10 -0
- package/dist/chess960.d.ts.map +1 -0
- package/dist/chess960.js +18 -0
- package/dist/chessops/board.d.ts +49 -0
- package/dist/chessops/board.d.ts.map +1 -0
- package/dist/chessops/board.js +138 -0
- package/dist/chessops/chess.d.ts +104 -0
- package/dist/chessops/chess.d.ts.map +1 -0
- package/dist/chessops/chess.js +435 -0
- package/dist/chessops/compat.d.ts +19 -0
- package/dist/chessops/compat.d.ts.map +1 -0
- package/dist/chessops/compat.js +25 -0
- package/dist/chessops/convert.d.ts +42 -0
- package/dist/chessops/convert.d.ts.map +1 -0
- package/dist/chessops/convert.js +114 -0
- package/dist/chessops/debug.d.ts +17 -0
- package/dist/chessops/debug.d.ts.map +1 -0
- package/dist/chessops/debug.js +67 -0
- package/dist/chessops/fen.d.ts +28 -0
- package/dist/chessops/fen.d.ts.map +1 -0
- package/dist/chessops/fen.js +74 -0
- package/dist/chessops/fenInternal.d.ts +15 -0
- package/dist/chessops/fenInternal.d.ts.map +1 -0
- package/dist/chessops/fenInternal.js +42 -0
- package/dist/chessops/index.d.ts +12 -0
- package/dist/chessops/index.d.ts.map +1 -0
- package/dist/chessops/index.js +17 -0
- package/dist/chessops/san.d.ts +7 -0
- package/dist/chessops/san.d.ts.map +1 -0
- package/dist/chessops/san.js +33 -0
- package/dist/chessops/setup.d.ts +64 -0
- package/dist/chessops/setup.d.ts.map +1 -0
- package/dist/chessops/setup.js +158 -0
- package/dist/chessops/squareSet.d.ts +48 -0
- package/dist/chessops/squareSet.d.ts.map +1 -0
- package/dist/chessops/squareSet.js +192 -0
- package/dist/chessops/transform.d.ts +12 -0
- package/dist/chessops/transform.d.ts.map +1 -0
- package/dist/chessops/transform.js +53 -0
- package/dist/chessops/types.d.ts +47 -0
- package/dist/chessops/types.d.ts.map +1 -0
- package/dist/chessops/types.js +12 -0
- package/dist/chessops/util.d.ts +19 -0
- package/dist/chessops/util.d.ts.map +1 -0
- package/dist/chessops/util.js +52 -0
- package/dist/chesstree.d.ts +143 -0
- package/dist/chesstree.d.ts.map +1 -0
- package/dist/chesstree.js +514 -0
- package/dist/core.d.ts +10 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +11 -0
- package/dist/fen.d.ts +10 -0
- package/dist/fen.d.ts.map +1 -0
- package/dist/fen.js +600 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/packedMove.d.ts +31 -0
- package/dist/packedMove.d.ts.map +1 -0
- package/dist/packedMove.js +88 -0
- package/dist/pgn.d.ts +24 -0
- package/dist/pgn.d.ts.map +1 -0
- package/dist/pgn.js +361 -0
- package/dist/rookMagicBlob.d.ts +2 -0
- package/dist/rookMagicBlob.d.ts.map +1 -0
- package/dist/rookMagicBlob.js +11 -0
- package/dist/san.d.ts +6 -0
- package/dist/san.d.ts.map +1 -0
- package/dist/san.js +484 -0
- package/dist/squareSet.d.ts +48 -0
- package/dist/squareSet.d.ts.map +1 -0
- package/dist/squareSet.js +154 -0
- package/dist/types.d.ts +147 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +88 -0
- package/dist/util.d.ts +11 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +59 -0
- package/dist/zobrist.d.ts +45 -0
- package/dist/zobrist.d.ts.map +1 -0
- package/dist/zobrist.js +271 -0
- package/dist/zobristBlob.d.ts +2 -0
- package/dist/zobristBlob.d.ts.map +1 -0
- package/dist/zobristBlob.js +11 -0
- package/package.json +135 -0
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
// src/chesstree.ts — clean-room chesstree-compatible analysis-tree layer
|
|
2
|
+
// (`turbochess/chesstree`). Public shapes mirror the `@itshak/chesstree`
|
|
3
|
+
// `.d.ts` ONLY — no third-party engine code was read or copied (same clean-room policy
|
|
4
|
+
// as the chessops compat, ADR-014/015). All logic is backed by the turbochess
|
|
5
|
+
// engine (`parseSan`/`makeMove`/`makeFen`/`makeUci`) and `turbochess/pgn`.
|
|
6
|
+
// Not part of the third-party baseline; MIT like the rest of turbochess.
|
|
7
|
+
import { parsePgn } from "./pgn.js";
|
|
8
|
+
import { parseFen, makeFen } from "./fen.js";
|
|
9
|
+
import { makeMove, isCheck } from "./chess.js";
|
|
10
|
+
import { parseSan, makeUci } from "./san.js";
|
|
11
|
+
import { Color } from "./types.js";
|
|
12
|
+
const START_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
|
13
|
+
const NULL_MOVE_SANS = new Set(["--", "Z0", "null"]);
|
|
14
|
+
const ID_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
15
|
+
/** Deterministic unique 2-char node id (same alphabet/contract the
|
|
16
|
+
* workstation's `createUniqueMoveNodeId` relies on). */
|
|
17
|
+
function nextNodeId(siblings) {
|
|
18
|
+
const used = new Set(siblings.map((s) => s.id));
|
|
19
|
+
for (const first of ID_ALPHABET) {
|
|
20
|
+
for (const second of ID_ALPHABET) {
|
|
21
|
+
const id = first + second;
|
|
22
|
+
if (!used.has(id))
|
|
23
|
+
return id;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return "zz";
|
|
27
|
+
}
|
|
28
|
+
function commentId(fullPath, index) {
|
|
29
|
+
// Same scheme the baseline emits: pgn-comment-comment-<path-to-node>-<i>
|
|
30
|
+
return `pgn-comment-comment-${fullPath}-${index}`;
|
|
31
|
+
}
|
|
32
|
+
const isNullMoveSan = (san) => NULL_MOVE_SANS.has(san);
|
|
33
|
+
/** Baseline import semantics: a move repeated as a sibling (same SAN — e.g.
|
|
34
|
+
* the same line reached via several variations) merges into one node;
|
|
35
|
+
* subtrees and comments fold together recursively. */
|
|
36
|
+
function mergeTree(existing, incoming) {
|
|
37
|
+
for (const child of incoming.children) {
|
|
38
|
+
const twin = existing.children.find((c) => c.san !== undefined && c.san === child.san);
|
|
39
|
+
if (twin !== undefined)
|
|
40
|
+
mergeTree(twin, child);
|
|
41
|
+
else
|
|
42
|
+
existing.children.push(child);
|
|
43
|
+
}
|
|
44
|
+
if (incoming.comments !== undefined && incoming.comments.length > 0) {
|
|
45
|
+
const ex = existing.comments ?? (existing.comments = []);
|
|
46
|
+
for (const c of incoming.comments) {
|
|
47
|
+
if (!ex.some((e) => e.text === c.text))
|
|
48
|
+
ex.push(c);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (existing.glyphs === undefined && incoming.glyphs !== undefined)
|
|
52
|
+
existing.glyphs = incoming.glyphs;
|
|
53
|
+
}
|
|
54
|
+
function replaySection(moves, ctx, out) {
|
|
55
|
+
// Mainline nesting: the parser emits a flat mainline list, so each
|
|
56
|
+
// successive move nests under the previous node's children. A PGN variation
|
|
57
|
+
// replaces the move it follows — it starts from the position BEFORE that
|
|
58
|
+
// move and its nodes are siblings of it (children[0] = mainline).
|
|
59
|
+
let target = out;
|
|
60
|
+
for (const m of moves) {
|
|
61
|
+
const snapshot = { ...ctx };
|
|
62
|
+
const id = nextNodeId(target);
|
|
63
|
+
const node = {
|
|
64
|
+
id,
|
|
65
|
+
ply: ctx.ply + 1,
|
|
66
|
+
fen: fenWithEpPolicy(ctx.pos),
|
|
67
|
+
uci: "",
|
|
68
|
+
children: [],
|
|
69
|
+
};
|
|
70
|
+
let advance = null;
|
|
71
|
+
if (isNullMoveSan(m.san)) {
|
|
72
|
+
// baseline normalizes Z0/null placeholders to san "--", uci "0000"
|
|
73
|
+
node.san = "--";
|
|
74
|
+
node.uci = "0000";
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
const parsed = parseSan(m.san, ctx.pos);
|
|
78
|
+
if (!parsed.ok)
|
|
79
|
+
continue; // never throw on garbage — skip the move
|
|
80
|
+
const move = parsed.value;
|
|
81
|
+
advance = { move };
|
|
82
|
+
node.san = m.san;
|
|
83
|
+
node.uci = makeUci(move);
|
|
84
|
+
node.fen = fenWithEpPolicy(makeMove(ctx.pos, move));
|
|
85
|
+
}
|
|
86
|
+
if (m.comments.length > 0) {
|
|
87
|
+
node.comments = m.comments.map((text, i) => ({
|
|
88
|
+
id: commentId(`${ctx.path}${id}`, i),
|
|
89
|
+
text,
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
// baseline pgnImport merges duplicate sibling moves (same SAN) into one
|
|
93
|
+
// node, recursively merging their subtrees
|
|
94
|
+
const existing = target.find((c) => c.san !== undefined && c.san === node.san);
|
|
95
|
+
let mergedInto;
|
|
96
|
+
if (existing !== undefined && !isNullMoveSan(node.san ?? "")) {
|
|
97
|
+
mergeTree(existing, node);
|
|
98
|
+
mergedInto = existing;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
target.push(node);
|
|
102
|
+
}
|
|
103
|
+
if (advance) {
|
|
104
|
+
ctx.pos = makeMove(ctx.pos, advance.move);
|
|
105
|
+
ctx.ply += 1;
|
|
106
|
+
ctx.path += id;
|
|
107
|
+
}
|
|
108
|
+
// variations of m are siblings of m (they replace it) → same target list;
|
|
109
|
+
// the mainline continuation instead nests under the node's children
|
|
110
|
+
for (const variation of m.variations) {
|
|
111
|
+
replaySection(variation.moves, { ...snapshot }, target);
|
|
112
|
+
}
|
|
113
|
+
if (advance) {
|
|
114
|
+
target = (mergedInto ?? node).children; // mainline continues under this node
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** FEN parity with the baseline: the ep square is only emitted when a legal
|
|
119
|
+
* en-passant capture exists (chessops/pgn behavior), not unconditionally. */
|
|
120
|
+
function ownPawnAt(pos, sq) {
|
|
121
|
+
const b = pos.board;
|
|
122
|
+
const own = pos.turn === Color.White ? b.white : b.black;
|
|
123
|
+
const lo = (own.lo & b.pawn.lo) >>> 0;
|
|
124
|
+
const hi = (own.hi & b.pawn.hi) >>> 0;
|
|
125
|
+
return sq < 32 ? (lo & (1 << sq)) !== 0 : (hi & (1 << (sq - 32))) !== 0;
|
|
126
|
+
}
|
|
127
|
+
function fenWithEpPolicy(pos) {
|
|
128
|
+
const fen = makeFen(pos);
|
|
129
|
+
const ep = pos.epSquare;
|
|
130
|
+
if (ep === null || ep === undefined)
|
|
131
|
+
return fen;
|
|
132
|
+
const offset = pos.turn === Color.White ? -8 : 8;
|
|
133
|
+
for (const df of [-1, 1]) {
|
|
134
|
+
const from = ep + offset + df;
|
|
135
|
+
if (from < 0 || from > 63)
|
|
136
|
+
continue;
|
|
137
|
+
if (Math.abs((from & 7) - (ep & 7)) !== 1)
|
|
138
|
+
continue;
|
|
139
|
+
if (!ownPawnAt(pos, from))
|
|
140
|
+
continue;
|
|
141
|
+
const after = makeMove(pos, { from, to: ep, isEnPassant: true, isCastling: false, isPromotion: false });
|
|
142
|
+
if (!isCheck(after))
|
|
143
|
+
return fen; // a legal ep capture exists → keep ep
|
|
144
|
+
}
|
|
145
|
+
// no legal ep capture — strip the ep field (baseline FEN policy)
|
|
146
|
+
const parts = fen.split(" ");
|
|
147
|
+
if (parts.length === 6) {
|
|
148
|
+
parts[3] = "-";
|
|
149
|
+
return parts.join(" ");
|
|
150
|
+
}
|
|
151
|
+
return fen;
|
|
152
|
+
}
|
|
153
|
+
// ---------- pgnImport ----------
|
|
154
|
+
const STRO_DEFAULTS = [
|
|
155
|
+
["Event", "?"],
|
|
156
|
+
["Site", "?"],
|
|
157
|
+
["Date", "????.??.??"],
|
|
158
|
+
["Round", "?"],
|
|
159
|
+
["White", "?"],
|
|
160
|
+
["Black", "?"],
|
|
161
|
+
["Result", "*"],
|
|
162
|
+
];
|
|
163
|
+
/** Headers with STRO defaults filled (baseline behavior) + SetUp/FEN from a
|
|
164
|
+
* root FEN that differs from the start position. */
|
|
165
|
+
function normalizedHeaders(headers, fen) {
|
|
166
|
+
const out = new Map();
|
|
167
|
+
for (const [k, v] of STRO_DEFAULTS)
|
|
168
|
+
out.set(k, headers.get(k) ?? v);
|
|
169
|
+
for (const [k, v] of headers) {
|
|
170
|
+
if (!out.has(k) || k === "SetUp" || k === "FEN")
|
|
171
|
+
out.set(k, v);
|
|
172
|
+
}
|
|
173
|
+
if (fen !== undefined && fen !== START_FEN) {
|
|
174
|
+
if (!out.has("SetUp"))
|
|
175
|
+
out.set("SetUp", "1");
|
|
176
|
+
out.set("FEN", fen);
|
|
177
|
+
}
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
function defaultGame(fen, headers) {
|
|
181
|
+
const norm = normalizedHeaders(headers, fen);
|
|
182
|
+
const tag = (k) => norm.get(k) ?? "?";
|
|
183
|
+
return {
|
|
184
|
+
fen,
|
|
185
|
+
id: "synthetic",
|
|
186
|
+
opening: undefined,
|
|
187
|
+
player: "white",
|
|
188
|
+
result: tag("Result"),
|
|
189
|
+
status: { id: 20, name: "started" },
|
|
190
|
+
turns: 1,
|
|
191
|
+
variant: { key: "standard", name: "standard", short: "standard" },
|
|
192
|
+
white: { name: tag("White") },
|
|
193
|
+
black: { name: tag("Black") },
|
|
194
|
+
event: tag("Event"),
|
|
195
|
+
site: tag("Site"),
|
|
196
|
+
date: tag("Date"),
|
|
197
|
+
round: tag("Round"),
|
|
198
|
+
whiteElo: headers.get("WhiteElo"),
|
|
199
|
+
blackElo: headers.get("BlackElo"),
|
|
200
|
+
timeControl: headers.get("TimeControl"),
|
|
201
|
+
termination: headers.get("Termination"),
|
|
202
|
+
tags: Object.fromEntries(norm),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
export function pgnImport(pgn) {
|
|
206
|
+
// Never throws — consumers (workstation) wrap calls in fallbacks that expect
|
|
207
|
+
// an empty treeParts on failure, unlike the reference baseline which throws.
|
|
208
|
+
const parsed = parsePgn(pgn);
|
|
209
|
+
if (!parsed.ok) {
|
|
210
|
+
const empty = defaultGame(START_FEN, new Map());
|
|
211
|
+
return {
|
|
212
|
+
game: empty,
|
|
213
|
+
player: { color: "white" },
|
|
214
|
+
opponent: { color: "black" },
|
|
215
|
+
treeParts: [],
|
|
216
|
+
sidelines: [],
|
|
217
|
+
userAnalysis: true,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
const headers = parsed.value.headers;
|
|
221
|
+
const fenHeader = headers.get("FEN");
|
|
222
|
+
const rootFen = fenHeader ?? START_FEN;
|
|
223
|
+
const startRes = parseFen(rootFen);
|
|
224
|
+
const start = startRes.ok ? startRes.value : parseFen(START_FEN).value;
|
|
225
|
+
const root = { id: "", ply: 0, fen: rootFen, uci: "", children: [] };
|
|
226
|
+
const ctx = { pos: start, ply: 0, path: "", moveNo: 1 };
|
|
227
|
+
replaySection(parsed.value.moves, ctx, root.children);
|
|
228
|
+
const game = defaultGame(rootFen, headers);
|
|
229
|
+
const white = headers.get("White");
|
|
230
|
+
const black = headers.get("Black");
|
|
231
|
+
return {
|
|
232
|
+
game,
|
|
233
|
+
player: { color: "white", name: white },
|
|
234
|
+
opponent: { color: "black", name: black },
|
|
235
|
+
treeParts: [root],
|
|
236
|
+
sidelines: [],
|
|
237
|
+
userAnalysis: true,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function lastPlyOf(root) {
|
|
241
|
+
let ply = 0;
|
|
242
|
+
let node = root.children[0];
|
|
243
|
+
while (node) {
|
|
244
|
+
ply = node.ply;
|
|
245
|
+
node = node.children[0];
|
|
246
|
+
}
|
|
247
|
+
return ply;
|
|
248
|
+
}
|
|
249
|
+
function nodeAtPathFrom(root, path) {
|
|
250
|
+
let node = root;
|
|
251
|
+
for (let i = 0; i + 2 <= path.length; i += 2) {
|
|
252
|
+
const chunk = path.slice(i, i + 2);
|
|
253
|
+
node = node.children.find((c) => c.id === chunk);
|
|
254
|
+
if (!node)
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
return node;
|
|
258
|
+
}
|
|
259
|
+
function nodeListFrom(root, path) {
|
|
260
|
+
const list = [root];
|
|
261
|
+
let node = root;
|
|
262
|
+
for (let i = 0; i + 2 <= path.length; i += 2) {
|
|
263
|
+
const chunk = path.slice(i, i + 2);
|
|
264
|
+
const next = node.children.find((c) => c.id === chunk);
|
|
265
|
+
if (!next)
|
|
266
|
+
break;
|
|
267
|
+
list.push(next);
|
|
268
|
+
node = next;
|
|
269
|
+
}
|
|
270
|
+
return list;
|
|
271
|
+
}
|
|
272
|
+
function pathIsMainlineFrom(root, path) {
|
|
273
|
+
let node = root;
|
|
274
|
+
for (let i = 0; i + 2 <= path.length; i += 2) {
|
|
275
|
+
const chunk = path.slice(i, i + 2);
|
|
276
|
+
const first = node.children[0];
|
|
277
|
+
if (!first || first.id !== chunk)
|
|
278
|
+
return false;
|
|
279
|
+
node = first;
|
|
280
|
+
}
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
export class TreeWrapperImpl {
|
|
284
|
+
constructor(root) {
|
|
285
|
+
this.root = root;
|
|
286
|
+
}
|
|
287
|
+
lastPly() {
|
|
288
|
+
return lastPlyOf(this.root);
|
|
289
|
+
}
|
|
290
|
+
nodeAtPath(path) {
|
|
291
|
+
return nodeAtPathFrom(this.root, path);
|
|
292
|
+
}
|
|
293
|
+
getNodeList(path) {
|
|
294
|
+
return nodeListFrom(this.root, path);
|
|
295
|
+
}
|
|
296
|
+
nodesOnPath(path) {
|
|
297
|
+
return nodeListFrom(this.root, path);
|
|
298
|
+
}
|
|
299
|
+
longestValidPath(path) {
|
|
300
|
+
let valid = "";
|
|
301
|
+
let node = this.root;
|
|
302
|
+
for (let i = 0; i + 2 <= path.length; i += 2) {
|
|
303
|
+
const chunk = path.slice(i, i + 2);
|
|
304
|
+
const next = node.children.find((c) => c.id === chunk);
|
|
305
|
+
if (!next)
|
|
306
|
+
break;
|
|
307
|
+
valid += chunk;
|
|
308
|
+
node = next;
|
|
309
|
+
}
|
|
310
|
+
return valid;
|
|
311
|
+
}
|
|
312
|
+
addNode(node, path) {
|
|
313
|
+
const parent = this.nodeAtPath(path);
|
|
314
|
+
if (!parent)
|
|
315
|
+
return undefined;
|
|
316
|
+
parent.children.push(node); // no dedup — baseline semantics
|
|
317
|
+
return path + node.id;
|
|
318
|
+
}
|
|
319
|
+
addNodes(nodes, path) {
|
|
320
|
+
let p = path;
|
|
321
|
+
for (const n of nodes) {
|
|
322
|
+
p = this.addNode(n, p ?? "");
|
|
323
|
+
if (p === undefined)
|
|
324
|
+
return undefined;
|
|
325
|
+
}
|
|
326
|
+
return p;
|
|
327
|
+
}
|
|
328
|
+
setCommentAt(comment, path) {
|
|
329
|
+
const node = this.nodeAtPath(path);
|
|
330
|
+
if (!node)
|
|
331
|
+
return undefined;
|
|
332
|
+
const comments = node.comments ?? [];
|
|
333
|
+
const idx = comments.findIndex((c) => c.id === comment.id);
|
|
334
|
+
if (idx >= 0)
|
|
335
|
+
comments[idx] = comment;
|
|
336
|
+
else
|
|
337
|
+
comments.push(comment);
|
|
338
|
+
node.comments = comments;
|
|
339
|
+
return node;
|
|
340
|
+
}
|
|
341
|
+
deleteCommentAt(id, path) {
|
|
342
|
+
const node = this.nodeAtPath(path);
|
|
343
|
+
if (!node)
|
|
344
|
+
return undefined;
|
|
345
|
+
node.comments = (node.comments ?? []).filter((c) => c.id !== id);
|
|
346
|
+
return node;
|
|
347
|
+
}
|
|
348
|
+
setGlyphsAt(glyphs, path) {
|
|
349
|
+
const node = this.nodeAtPath(path);
|
|
350
|
+
if (!node)
|
|
351
|
+
return undefined;
|
|
352
|
+
node.glyphs = glyphs;
|
|
353
|
+
return node;
|
|
354
|
+
}
|
|
355
|
+
setClockAt(clock, path) {
|
|
356
|
+
const node = this.nodeAtPath(path);
|
|
357
|
+
if (!node)
|
|
358
|
+
return undefined;
|
|
359
|
+
node.clock = clock;
|
|
360
|
+
return node;
|
|
361
|
+
}
|
|
362
|
+
pathIsMainline(path) {
|
|
363
|
+
return pathIsMainlineFrom(this.root, path);
|
|
364
|
+
}
|
|
365
|
+
pathExists(path) {
|
|
366
|
+
return this.nodeAtPath(path) !== undefined;
|
|
367
|
+
}
|
|
368
|
+
parentNode(path) {
|
|
369
|
+
if (path.length < 2)
|
|
370
|
+
return undefined;
|
|
371
|
+
return this.nodeAtPath(path.slice(0, -2));
|
|
372
|
+
}
|
|
373
|
+
export() {
|
|
374
|
+
return pgnExport.renderFullTxt({
|
|
375
|
+
data: { game: defaultGame(this.root.fen, new Map()) },
|
|
376
|
+
tree: this,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
pgn() {
|
|
380
|
+
return this.export();
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
export function build(root) {
|
|
384
|
+
return new TreeWrapperImpl(root);
|
|
385
|
+
}
|
|
386
|
+
// ---------- PGN export ----------
|
|
387
|
+
const STRO_KEYS = new Set(["Event", "Site", "Date", "Round", "White", "Black", "Result"]);
|
|
388
|
+
const GLYPH_TO_NAG = { "!": 1, "?": 2, "!!": 3, "??": 4, "!?": 5, "?!": 6 };
|
|
389
|
+
const Q = '"';
|
|
390
|
+
const headerLine = (k, v) => "[" + k + " " + Q + (v ?? "?") + Q + "]";
|
|
391
|
+
function renderHeaders(game, rootFen) {
|
|
392
|
+
const lines = [
|
|
393
|
+
headerLine("Event", game.event),
|
|
394
|
+
headerLine("Site", game.site),
|
|
395
|
+
headerLine("Date", game.date ?? "????.??.??"),
|
|
396
|
+
headerLine("Round", game.round),
|
|
397
|
+
headerLine("White", game.white?.name),
|
|
398
|
+
headerLine("Black", game.black?.name),
|
|
399
|
+
headerLine("Result", game.result ?? "*"),
|
|
400
|
+
];
|
|
401
|
+
if (rootFen !== START_FEN) {
|
|
402
|
+
lines.push(headerLine("SetUp", "1"), headerLine("FEN", rootFen));
|
|
403
|
+
}
|
|
404
|
+
for (const [k, v] of Object.entries(game.tags)) {
|
|
405
|
+
if (STRO_KEYS.has(k) || k === "SetUp" || k === "FEN")
|
|
406
|
+
continue;
|
|
407
|
+
lines.push(headerLine(k, v));
|
|
408
|
+
}
|
|
409
|
+
return lines;
|
|
410
|
+
}
|
|
411
|
+
function advanceState(state) {
|
|
412
|
+
if (state.turn === "b") {
|
|
413
|
+
state.moveNo += 1;
|
|
414
|
+
state.turn = "w";
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
state.turn = "b";
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
/** Emits the tree from `list[idx]`: the node, then the paren groups for its
|
|
421
|
+
* sibling alternatives (list[idx+1..], which replace it — baseline places
|
|
422
|
+
* them right after the move they replace), then descends the mainline child. */
|
|
423
|
+
function emitFrom(list, idx, state, tokens) {
|
|
424
|
+
if (idx >= list.length)
|
|
425
|
+
return;
|
|
426
|
+
const stateBefore = { ...state };
|
|
427
|
+
const node = list[idx];
|
|
428
|
+
const san = node.san;
|
|
429
|
+
const isNull = san !== undefined && isNullMoveSan(san);
|
|
430
|
+
if (san !== undefined && !isNull) {
|
|
431
|
+
if (state.turn === "w")
|
|
432
|
+
tokens.push(state.moveNo + ".");
|
|
433
|
+
else if (state.needNumber)
|
|
434
|
+
tokens.push(state.moveNo + "...");
|
|
435
|
+
tokens.push(san);
|
|
436
|
+
state.needNumber = false;
|
|
437
|
+
for (const g of node.glyphs ?? []) {
|
|
438
|
+
const nag = GLYPH_TO_NAG[g.symbol];
|
|
439
|
+
if (nag !== undefined)
|
|
440
|
+
tokens.push("$" + nag);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
for (const c of node.comments ?? [])
|
|
444
|
+
tokens.push("{" + c.text + "}");
|
|
445
|
+
if (san !== undefined && !isNull)
|
|
446
|
+
advanceState(state);
|
|
447
|
+
for (let j = idx + 1; j < list.length; j++) {
|
|
448
|
+
const inner = { ...stateBefore, needNumber: true };
|
|
449
|
+
const sub = [];
|
|
450
|
+
emitFrom([list[j]], 0, inner, sub);
|
|
451
|
+
tokens.push("(" + sub.join(" ") + ")");
|
|
452
|
+
}
|
|
453
|
+
if (list.length > idx + 1)
|
|
454
|
+
state.needNumber = true;
|
|
455
|
+
emitFrom(node.children, 0, state, tokens);
|
|
456
|
+
}
|
|
457
|
+
function renderSection(nodes, state) {
|
|
458
|
+
const tokens = [];
|
|
459
|
+
emitFrom(nodes, 0, state, tokens);
|
|
460
|
+
return tokens.join(" ");
|
|
461
|
+
}
|
|
462
|
+
function numberingFromFen(fen) {
|
|
463
|
+
const parts = fen.split(/\s+/);
|
|
464
|
+
const turn = parts[1] === "b" ? "b" : "w";
|
|
465
|
+
const moveNo = parseInt(parts[5] ?? "1", 10) || 1;
|
|
466
|
+
return { turn, moveNo, needNumber: true };
|
|
467
|
+
}
|
|
468
|
+
function renderMovetext(root, game) {
|
|
469
|
+
const state = numberingFromFen(root.fen);
|
|
470
|
+
const moves = renderSection(root.children, state);
|
|
471
|
+
const result = game.result ?? "*";
|
|
472
|
+
return moves.length > 0 ? moves + " " + result : result;
|
|
473
|
+
}
|
|
474
|
+
export const pgnExport = {
|
|
475
|
+
/** Full PGN (tags + movetext) — byte-compatible with the reference baseline's
|
|
476
|
+
* output for the surfaces consumers use (STRO headers, FEN/SetUp, comments,
|
|
477
|
+
* variations, result). */
|
|
478
|
+
renderFullTxt(ctrl) {
|
|
479
|
+
const game = ctrl.data.game;
|
|
480
|
+
const root = ctrl.tree.root;
|
|
481
|
+
const headers = renderHeaders(game, root.fen);
|
|
482
|
+
return headers.join("\n") + "\n\n" + renderMovetext(root, game);
|
|
483
|
+
},
|
|
484
|
+
/** Renders a node list as a one-line PGN mainline (no result token). */
|
|
485
|
+
renderVariationPgn(game, nodeList) {
|
|
486
|
+
if (nodeList.length === 0)
|
|
487
|
+
return "";
|
|
488
|
+
const root = nodeList[0].san === undefined ? nodeList[0] : { id: "", ply: 0, fen: nodeList[0].fen, uci: "", children: nodeList };
|
|
489
|
+
const headers = renderHeaders(game, root.fen);
|
|
490
|
+
const state = numberingFromFen(root.fen);
|
|
491
|
+
const nodes = root.children;
|
|
492
|
+
const tokens = [];
|
|
493
|
+
for (const node of nodes) {
|
|
494
|
+
const san = node.san;
|
|
495
|
+
if (san === undefined)
|
|
496
|
+
continue;
|
|
497
|
+
const isNull = isNullMoveSan(san);
|
|
498
|
+
if (!isNull) {
|
|
499
|
+
if (state.turn === "w")
|
|
500
|
+
tokens.push(`${state.moveNo}.`);
|
|
501
|
+
else if (state.needNumber)
|
|
502
|
+
tokens.push(`${state.moveNo}...`);
|
|
503
|
+
tokens.push(san);
|
|
504
|
+
state.needNumber = false;
|
|
505
|
+
advanceState(state);
|
|
506
|
+
}
|
|
507
|
+
for (const c of node.comments ?? [])
|
|
508
|
+
tokens.push(`{${c.text}}`);
|
|
509
|
+
}
|
|
510
|
+
return `${headers.join("\n")}\n\n${tokens.join(" ")}`;
|
|
511
|
+
},
|
|
512
|
+
};
|
|
513
|
+
/** chesstree-compatible alias — `import { buildTree } from "turbochess/chesstree"`. */
|
|
514
|
+
export const buildTree = build;
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./squareSet.js";
|
|
2
|
+
export * from "./board.js";
|
|
3
|
+
export * from "./types.js";
|
|
4
|
+
export * from "./util.js";
|
|
5
|
+
export * from "./fen.js";
|
|
6
|
+
export * from "./san.js";
|
|
7
|
+
export { knightAttacks, kingAttacks, pawnAttacks, bishopAttacks, rookAttacks, queenAttacks, ensureMagicTablesLoaded, magicTablesLoaded, ray, between, isAttacked, kingAttackers, } from "./attacks.js";
|
|
8
|
+
export { dests, allDests, detectCastling, isCheck, isCheckmate, isStalemate, isInsufficientMaterial, isFiftyMoveDraw, isThreefoldRepetition, perft, makeMove, play, Chess, } from "./chess.js";
|
|
9
|
+
export type { CastlingPlan } from "./chess.js";
|
|
10
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAIA,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,OAAO,EACL,aAAa,EACb,WAAW,EACX,WAAW,EACX,aAAa,EACb,WAAW,EACX,YAAY,EACZ,uBAAuB,EACvB,iBAAiB,EACjB,GAAG,EACH,OAAO,EACP,UAAU,EACV,aAAa,GACd,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,KAAK,EACL,QAAQ,EACR,cAAc,EACd,OAAO,EACP,WAAW,EACX,WAAW,EACX,sBAAsB,EACtB,eAAe,EACf,qBAAqB,EACrB,KAAK,EACL,QAAQ,EACR,IAAI,EACJ,KAAK,GACN,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/core.ts — turbochess core entry (standard rules only) for tree-shaking
|
|
2
|
+
// Exports board, squareSet, attacks, fen, san, chess (standard rules)
|
|
3
|
+
// Side-effect free, tree-shakeable per ADR-012
|
|
4
|
+
export * from "./squareSet.js";
|
|
5
|
+
export * from "./board.js";
|
|
6
|
+
export * from "./types.js";
|
|
7
|
+
export * from "./util.js";
|
|
8
|
+
export * from "./fen.js";
|
|
9
|
+
export * from "./san.js";
|
|
10
|
+
export { knightAttacks, kingAttacks, pawnAttacks, bishopAttacks, rookAttacks, queenAttacks, ensureMagicTablesLoaded, magicTablesLoaded, ray, between, isAttacked, kingAttackers, } from "./attacks.js";
|
|
11
|
+
export { dests, allDests, detectCastling, isCheck, isCheckmate, isStalemate, isInsufficientMaterial, isFiftyMoveDraw, isThreefoldRepetition, perft, makeMove, play, Chess, } from "./chess.js";
|
package/dist/fen.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Setup, FenError, Result } from "./types.js";
|
|
2
|
+
export declare function parseFen(fen: string, opts?: {
|
|
3
|
+
chess960?: boolean;
|
|
4
|
+
strict?: boolean;
|
|
5
|
+
}): Result<Setup, FenError>;
|
|
6
|
+
export declare function makeFen(setup: Setup, opts?: {
|
|
7
|
+
shredder?: boolean;
|
|
8
|
+
chess960?: boolean;
|
|
9
|
+
}): string;
|
|
10
|
+
//# sourceMappingURL=fen.d.ts.map
|
|
@@ -0,0 +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"}
|