@tastic/cardtastic-engine 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +68 -2
  3. package/dist/acesup.d.mts +17 -0
  4. package/dist/acesup.d.ts +17 -0
  5. package/dist/acesup.js +8 -0
  6. package/dist/acesup.mjs +8 -0
  7. package/dist/autoFoundation-CfnFNysj.d.ts +63 -0
  8. package/dist/autoFoundation-Ctp9LKa-.d.mts +63 -0
  9. package/dist/canfield.d.mts +64 -0
  10. package/dist/canfield.d.ts +64 -0
  11. package/dist/canfield.js +20 -0
  12. package/dist/canfield.mjs +20 -0
  13. package/dist/chunk-22DC2YH6.js +95 -0
  14. package/dist/chunk-3BMUGHKQ.mjs +95 -0
  15. package/dist/chunk-4HJ7QMYV.mjs +339 -0
  16. package/dist/chunk-4WAWIFQF.js +131 -0
  17. package/dist/chunk-5266UC2Q.mjs +271 -0
  18. package/dist/chunk-6JWUTZVU.mjs +214 -0
  19. package/dist/chunk-AY3CR5EH.mjs +212 -0
  20. package/dist/chunk-B56TRALO.js +23 -0
  21. package/dist/chunk-CV3VJX34.mjs +341 -0
  22. package/dist/chunk-FDHHAPZA.mjs +177 -0
  23. package/dist/chunk-FTR733R7.mjs +297 -0
  24. package/dist/chunk-GFP5NUYK.mjs +23 -0
  25. package/dist/chunk-IKLYXDG7.mjs +131 -0
  26. package/dist/chunk-LH3UZP7D.js +165 -0
  27. package/dist/chunk-LSS44UKH.js +22 -0
  28. package/dist/chunk-M37CNJHF.mjs +165 -0
  29. package/dist/chunk-PLSNST56.mjs +163 -0
  30. package/dist/chunk-PTRHGPCN.js +163 -0
  31. package/dist/chunk-S3WNVF2K.js +339 -0
  32. package/dist/chunk-SDZPP3HI.mjs +22 -0
  33. package/dist/chunk-SLQ7RGMZ.js +212 -0
  34. package/dist/chunk-TNMFNWEM.mjs +249 -0
  35. package/dist/chunk-VGJPT3CX.js +214 -0
  36. package/dist/chunk-W5CPH6TD.js +115 -0
  37. package/dist/chunk-WJGEX74V.js +341 -0
  38. package/dist/chunk-WKES4P36.js +297 -0
  39. package/dist/chunk-XPTQNUF3.js +348 -0
  40. package/dist/chunk-XSS5M6C7.mjs +115 -0
  41. package/dist/chunk-XTHDUWJF.js +271 -0
  42. package/dist/chunk-XV5OUPIW.js +177 -0
  43. package/dist/chunk-YKY4K67K.mjs +348 -0
  44. package/dist/chunk-ZMNGTXU5.js +249 -0
  45. package/dist/fortythieves.d.mts +50 -0
  46. package/dist/fortythieves.d.ts +50 -0
  47. package/dist/fortythieves.js +22 -0
  48. package/dist/fortythieves.mjs +22 -0
  49. package/dist/freecell.d.mts +30 -0
  50. package/dist/freecell.d.ts +30 -0
  51. package/dist/freecell.js +18 -0
  52. package/dist/freecell.mjs +18 -0
  53. package/dist/gameTypes-BnfLkwzA.d.mts +201 -0
  54. package/dist/gameTypes-BnfLkwzA.d.ts +201 -0
  55. package/dist/golf.d.mts +17 -0
  56. package/dist/golf.d.ts +17 -0
  57. package/dist/golf.js +7 -0
  58. package/dist/golf.mjs +7 -0
  59. package/dist/index.d.mts +176 -0
  60. package/dist/index.d.ts +176 -0
  61. package/dist/index.js +446 -0
  62. package/dist/index.mjs +446 -0
  63. package/dist/klondike.d.mts +25 -0
  64. package/dist/klondike.d.ts +25 -0
  65. package/dist/klondike.js +15 -0
  66. package/dist/klondike.mjs +15 -0
  67. package/dist/pyramid.d.mts +36 -0
  68. package/dist/pyramid.d.ts +36 -0
  69. package/dist/pyramid.js +16 -0
  70. package/dist/pyramid.mjs +16 -0
  71. package/dist/scorpion.d.mts +14 -0
  72. package/dist/scorpion.d.ts +14 -0
  73. package/dist/scorpion.js +9 -0
  74. package/dist/scorpion.mjs +9 -0
  75. package/dist/spider.d.mts +22 -0
  76. package/dist/spider.d.ts +22 -0
  77. package/dist/spider.js +11 -0
  78. package/dist/spider.mjs +11 -0
  79. package/dist/tripeaks.d.mts +43 -0
  80. package/dist/tripeaks.d.ts +43 -0
  81. package/dist/tripeaks.js +19 -0
  82. package/dist/tripeaks.mjs +19 -0
  83. package/dist/yukon.d.mts +24 -0
  84. package/dist/yukon.d.ts +24 -0
  85. package/dist/yukon.js +15 -0
  86. package/dist/yukon.mjs +15 -0
  87. package/package.json +187 -4
@@ -0,0 +1,23 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkW5CPH6TDjs = require('./chunk-W5CPH6TD.js');
4
+
5
+ // src/engine/autoMove.ts
6
+ function pickBestTableauTarget(candidates, isValidRun) {
7
+ const nonEmpty = candidates.filter((c) => c.pile.length > 0);
8
+ const pool = nonEmpty.length > 0 ? nonEmpty : candidates;
9
+ let best = null;
10
+ let bestBuried = Infinity;
11
+ for (const c of pool) {
12
+ const buried = _chunkW5CPH6TDjs.buriedCount.call(void 0, c.pile, isValidRun);
13
+ if (best === null || buried < bestBuried || buried === bestBuried && c.pile.length > best.pile.length) {
14
+ best = c;
15
+ bestBuried = buried;
16
+ }
17
+ }
18
+ return best;
19
+ }
20
+
21
+
22
+
23
+ exports.pickBestTableauTarget = pickBestTableauTarget;
@@ -0,0 +1,341 @@
1
+ import {
2
+ columnsKey,
3
+ pileKey
4
+ } from "./chunk-FDHHAPZA.mjs";
5
+ import {
6
+ isDescendingSameSuit,
7
+ shuffledDeck,
8
+ top
9
+ } from "./chunk-XSS5M6C7.mjs";
10
+
11
+ // src/games/fortythieves/rules.ts
12
+ var NUM_COLUMNS = 10;
13
+ var NUM_FOUNDATIONS = 8;
14
+ function cloneState(state) {
15
+ return {
16
+ tableau: state.tableau.slice(),
17
+ foundation: state.foundation.slice(),
18
+ stock: state.stock,
19
+ waste: state.waste
20
+ };
21
+ }
22
+ function setPile(state, ref, pile) {
23
+ switch (ref.zone) {
24
+ case "tableau":
25
+ state.tableau[ref.index] = pile;
26
+ break;
27
+ case "foundation":
28
+ state.foundation[ref.index] = pile;
29
+ break;
30
+ case "stock":
31
+ state.stock = pile;
32
+ break;
33
+ case "waste":
34
+ state.waste = pile;
35
+ break;
36
+ }
37
+ }
38
+ function canPlaceOnFoundation(dest, moving) {
39
+ const destTop = top(dest);
40
+ if (destTop === void 0) return moving.rank === 1;
41
+ return destTop.suit === moving.suit && moving.rank === destTop.rank + 1;
42
+ }
43
+ function canPlaceOnTableau(dest, moving) {
44
+ const destTop = top(dest);
45
+ if (destTop === void 0) return true;
46
+ return destTop.suit === moving.suit && moving.rank === destTop.rank - 1;
47
+ }
48
+ function findFoundationSlot(foundation, card) {
49
+ for (let f = 0; f < NUM_FOUNDATIONS; f++) {
50
+ if (foundation[f].length > 0 && canPlaceOnFoundation(foundation[f], card)) return f;
51
+ }
52
+ if (card.rank === 1) {
53
+ const emptyIndex = foundation.findIndex((pile) => pile.length === 0);
54
+ if (emptyIndex !== -1) return emptyIndex;
55
+ }
56
+ return null;
57
+ }
58
+ function runLengthAtTop(column) {
59
+ if (column.length === 0) return 0;
60
+ let len = 1;
61
+ for (let i = column.length - 2; i >= 0; i--) {
62
+ const cur = column[i];
63
+ const next = column[i + 1];
64
+ if (cur.suit !== next.suit || cur.rank !== next.rank + 1) break;
65
+ len++;
66
+ }
67
+ return len;
68
+ }
69
+ function getPile(state, ref) {
70
+ switch (ref.zone) {
71
+ case "tableau":
72
+ return state.tableau[ref.index] ?? null;
73
+ case "foundation":
74
+ return state.foundation[ref.index] ?? null;
75
+ case "waste":
76
+ return state.waste;
77
+ default:
78
+ return null;
79
+ }
80
+ }
81
+ function newGame(seed) {
82
+ const deck = shuffledDeck(seed, 2);
83
+ let cursor = 0;
84
+ const tableau = [];
85
+ for (let col = 0; col < NUM_COLUMNS; col++) {
86
+ const cards = [];
87
+ for (let i = 0; i < 4; i++) cards.push({ ...deck[cursor++], faceUp: true });
88
+ tableau.push(cards);
89
+ }
90
+ const foundation = Array.from({ length: NUM_FOUNDATIONS }, () => []);
91
+ const stock = deck.slice(cursor).map((c) => ({ ...c, faceUp: false }));
92
+ return { tableau, foundation, stock, waste: [] };
93
+ }
94
+ function drawStock(state) {
95
+ if (state.stock.length === 0) return null;
96
+ const next = cloneState(state);
97
+ const nextStock = next.stock.slice();
98
+ const nextWaste = next.waste.slice();
99
+ const card = nextStock.pop();
100
+ nextWaste.push(card.faceUp ? card : { ...card, faceUp: true });
101
+ setPile(next, { zone: "stock", index: 0 }, nextStock);
102
+ setPile(next, { zone: "waste", index: 0 }, nextWaste);
103
+ return next;
104
+ }
105
+ function isWon(state) {
106
+ return state.foundation.every((pile) => pile.length === 13);
107
+ }
108
+ function accessibleCards(state) {
109
+ const result = [];
110
+ const wasteTop = top(state.waste);
111
+ if (wasteTop) result.push({ ref: { zone: "waste", index: 0 }, card: wasteTop });
112
+ state.tableau.forEach((pile, index) => {
113
+ const t = top(pile);
114
+ if (t) result.push({ ref: { zone: "tableau", index }, card: t });
115
+ });
116
+ return result;
117
+ }
118
+ function createFortyThievesController(runMoves) {
119
+ function tryMove(state, move) {
120
+ const { from, to, count } = move;
121
+ if (count < 1) return null;
122
+ if (from.zone === to.zone && from.index === to.index) return null;
123
+ if (from.zone === "foundation") return null;
124
+ if (from.zone === "stock") return null;
125
+ const sourcePile = getPile(state, from);
126
+ if (!sourcePile || sourcePile.length < count) return null;
127
+ if (count > 1 && (from.zone !== "tableau" || to.zone !== "tableau" || !runMoves)) return null;
128
+ const movingCards = sourcePile.slice(sourcePile.length - count);
129
+ if (movingCards.some((c) => !c.faceUp)) return null;
130
+ if (count > 1 && !isDescendingSameSuit(movingCards)) return null;
131
+ const destPile = getPile(state, to);
132
+ if (!destPile) return null;
133
+ if (to.zone === "tableau") {
134
+ if (!canPlaceOnTableau(destPile, movingCards[0])) return null;
135
+ } else if (to.zone === "foundation") {
136
+ if (count !== 1) return null;
137
+ if (!canPlaceOnFoundation(destPile, movingCards[0])) return null;
138
+ } else {
139
+ return null;
140
+ }
141
+ const next = cloneState(state);
142
+ const nextSourcePile = getPile(next, from).slice();
143
+ const nextDestPile = getPile(next, to).slice();
144
+ setPile(next, from, nextSourcePile);
145
+ setPile(next, to, nextDestPile);
146
+ const moved = nextSourcePile.splice(nextSourcePile.length - count, count);
147
+ nextDestPile.push(...moved);
148
+ return next;
149
+ }
150
+ function legalMoves(state) {
151
+ const moves = [];
152
+ const tryAdd = (from, to, count) => {
153
+ if (tryMove(state, { from, to, count })) moves.push({ from, to, count });
154
+ };
155
+ if (state.waste.length > 0) {
156
+ const from = { zone: "waste", index: 0 };
157
+ for (let f = 0; f < NUM_FOUNDATIONS; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
158
+ for (let t = 0; t < NUM_COLUMNS; t++) tryAdd(from, { zone: "tableau", index: t }, 1);
159
+ }
160
+ for (let col = 0; col < NUM_COLUMNS; col++) {
161
+ const pile = state.tableau[col];
162
+ if (pile.length === 0) continue;
163
+ const from = { zone: "tableau", index: col };
164
+ for (let f = 0; f < NUM_FOUNDATIONS; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
165
+ let runLen = 1;
166
+ if (runMoves) {
167
+ for (let i = pile.length - 1; i > 0; i--) {
168
+ const cur = pile[i];
169
+ const prev = pile[i - 1];
170
+ if (prev.rank !== cur.rank + 1 || prev.suit !== cur.suit) break;
171
+ runLen++;
172
+ }
173
+ }
174
+ for (let count = 1; count <= runLen; count++) {
175
+ for (let t = 0; t < NUM_COLUMNS; t++) {
176
+ if (t === col) continue;
177
+ tryAdd(from, { zone: "tableau", index: t }, count);
178
+ }
179
+ }
180
+ }
181
+ return moves;
182
+ }
183
+ function isStuck(state) {
184
+ return !isWon(state) && state.stock.length === 0 && legalMoves(state).length === 0;
185
+ }
186
+ function autoMoveHint(state, from, cardId) {
187
+ const pile = getPile(state, from);
188
+ if (!pile) return null;
189
+ const cardIndex = pile.findIndex((c) => c.id === cardId);
190
+ if (cardIndex === -1) return null;
191
+ const movingCards = pile.slice(cardIndex);
192
+ if (movingCards.some((c) => !c.faceUp)) return null;
193
+ const count = movingCards.length;
194
+ if (count > 1 && (from.zone !== "tableau" || !runMoves || !isDescendingSameSuit(movingCards))) return null;
195
+ if (count === 1) {
196
+ const slot = findFoundationSlot(state.foundation, movingCards[0]);
197
+ if (slot !== null) return { from, to: { zone: "foundation", index: slot }, count: 1 };
198
+ }
199
+ for (let t = 0; t < NUM_COLUMNS; t++) {
200
+ if (from.zone === "tableau" && from.index === t) continue;
201
+ if (canPlaceOnTableau(state.tableau[t], movingCards[0])) {
202
+ return { from, to: { zone: "tableau", index: t }, count };
203
+ }
204
+ }
205
+ return null;
206
+ }
207
+ function scoreHintMove(state, move) {
208
+ if (move.to.zone !== "tableau") return 0;
209
+ if (move.from.zone === "waste") {
210
+ const dest = state.tableau[move.to.index];
211
+ if (!dest || dest.length === 0 || state.waste.length === 0) return 0;
212
+ return 50 + 30 + 2;
213
+ }
214
+ if (move.from.zone !== "tableau") return 0;
215
+ const fromCol = state.tableau[move.from.index];
216
+ const toCol = state.tableau[move.to.index];
217
+ if (!fromCol || !toCol) return 0;
218
+ const group = fromCol.slice(fromCol.length - move.count);
219
+ const runLen = runMoves ? runLengthAtTop(fromCol) : 1;
220
+ const destRunLenBefore = runLengthAtTop(toCol);
221
+ const sameSuitExtension = toCol.length > 0 && toCol[toCol.length - 1].suit === group[0].suit;
222
+ const newRunLenAtDest = move.count + (sameSuitExtension ? destRunLenBefore : 0);
223
+ const remainderRunLen = move.count < runLen ? runLen - move.count : 0;
224
+ const netRunStructureChange = Math.max(newRunLenAtDest, remainderRunLen) - runLen;
225
+ const fromBecomesEmpty = move.count === fromCol.length;
226
+ const toWasEmpty = toCol.length === 0;
227
+ const netEmptyDelta = (fromBecomesEmpty ? 1 : 0) - (toWasEmpty ? 1 : 0);
228
+ let score = netRunStructureChange * 30;
229
+ score += netEmptyDelta * 40;
230
+ score += move.count * 2;
231
+ return score;
232
+ }
233
+ const progress = {
234
+ id: runMoves ? "fortythieves-runs" : "fortythieves",
235
+ moves: legalMoves,
236
+ drawIsStep: false,
237
+ key: (state) => `${columnsKey(state.tableau)}/${pileKey(state.waste)}/${state.stock.length}`,
238
+ progress: (state) => [state.foundation.reduce((n, p) => n + p.length, 0)]
239
+ };
240
+ return {
241
+ id: "fortythieves",
242
+ title: "Forty Thieves",
243
+ newGame,
244
+ legalMoves,
245
+ tryMove,
246
+ drawStock,
247
+ isWon,
248
+ isStuck,
249
+ autoMoveHint,
250
+ accessibleCards,
251
+ scoreHintMove,
252
+ progress
253
+ };
254
+ }
255
+ function findNextFoundationMove(controller, state) {
256
+ if (!controller.accessibleCards) return null;
257
+ for (const entry of controller.accessibleCards(state)) {
258
+ const slot = findFoundationSlot(state.foundation, entry.card);
259
+ if (slot === null) continue;
260
+ const to = { zone: "foundation", index: slot };
261
+ if (controller.tryMove(state, { from: entry.ref, to, count: 1 })) return entry;
262
+ }
263
+ return null;
264
+ }
265
+ function isFullyAutoCompletable(controller, state) {
266
+ if (!controller.accessibleCards) return false;
267
+ let cur = state;
268
+ while (!controller.isWon(cur)) {
269
+ const entry = findNextFoundationMove(controller, cur);
270
+ if (!entry) return false;
271
+ const slot = findFoundationSlot(cur.foundation, entry.card);
272
+ const result = controller.tryMove(cur, { from: entry.ref, to: { zone: "foundation", index: slot }, count: 1 });
273
+ if (!result) return false;
274
+ cur = result;
275
+ }
276
+ return true;
277
+ }
278
+ function nextAutoCompleteMove(controller, state) {
279
+ return findNextFoundationMove(controller, state);
280
+ }
281
+ function canAutoFinish(controller, state) {
282
+ if (state.stock.length > 0 || state.waste.length > 0) return false;
283
+ return !controller.isWon(state) && isFullyAutoCompletable(controller, state);
284
+ }
285
+ function nextFinishMove(controller, state) {
286
+ if (!canAutoFinish(controller, state)) return null;
287
+ const entry = findNextFoundationMove(controller, state);
288
+ if (!entry) return null;
289
+ return { entry, to: { zone: "foundation", index: findFoundationSlot(state.foundation, entry.card) } };
290
+ }
291
+ function findNextAutoCompleteStepWithDraws(controller, state) {
292
+ const playEntry = findNextFoundationMove(controller, state);
293
+ if (playEntry) {
294
+ const slot = findFoundationSlot(state.foundation, playEntry.card);
295
+ return { kind: "play", entry: playEntry, to: { zone: "foundation", index: slot } };
296
+ }
297
+ if (!controller.drawStock) return null;
298
+ const afterDraw = controller.drawStock(state);
299
+ if (!afterDraw) return null;
300
+ const drawEntry = findNextFoundationMove(controller, afterDraw);
301
+ if (drawEntry) {
302
+ const slot = findFoundationSlot(afterDraw.foundation, drawEntry.card);
303
+ return { kind: "drawAndPlay", entry: drawEntry, to: { zone: "foundation", index: slot } };
304
+ }
305
+ return { kind: "draw" };
306
+ }
307
+ function isFullyAutoCompletableWithDraws(controller, state) {
308
+ if (!controller.accessibleCards || !controller.drawStock) return false;
309
+ const seen = /* @__PURE__ */ new Set();
310
+ let cur = state;
311
+ while (!controller.isWon(cur)) {
312
+ const step = findNextAutoCompleteStepWithDraws(controller, cur);
313
+ if (!step) return false;
314
+ if (step.kind === "play") {
315
+ cur = controller.tryMove(cur, { from: step.entry.ref, to: step.to, count: 1 });
316
+ } else if (step.kind === "drawAndPlay") {
317
+ const afterDraw = controller.drawStock(cur);
318
+ cur = controller.tryMove(afterDraw, { from: step.entry.ref, to: step.to, count: 1 });
319
+ } else {
320
+ const key = JSON.stringify(cur);
321
+ if (seen.has(key)) return false;
322
+ seen.add(key);
323
+ cur = controller.drawStock(cur);
324
+ }
325
+ }
326
+ return true;
327
+ }
328
+ function nextAutoCompleteMoveWithDraws(controller, state) {
329
+ return findNextAutoCompleteStepWithDraws(controller, state);
330
+ }
331
+
332
+ export {
333
+ findFoundationSlot,
334
+ createFortyThievesController,
335
+ isFullyAutoCompletable,
336
+ nextAutoCompleteMove,
337
+ canAutoFinish,
338
+ nextFinishMove,
339
+ isFullyAutoCompletableWithDraws,
340
+ nextAutoCompleteMoveWithDraws
341
+ };
@@ -0,0 +1,177 @@
1
+ // src/engine/progressSearch.ts
2
+ function higher(a, b) {
3
+ for (let i = 0; i < a.length; i++) if (a[i] > b[i]) return true;
4
+ return false;
5
+ }
6
+ function stepKey(step) {
7
+ return step.kind === "draw" ? "draw" : `${step.move.from.zone}${step.move.from.index}>${step.move.to.zone}${step.move.to.index}x${step.move.count}`;
8
+ }
9
+ var ProgressSearch = class {
10
+ constructor(controller, spec, start, mode) {
11
+ this.controller = controller;
12
+ this.spec = spec;
13
+ this.mode = mode;
14
+ this.startProgress = spec.progress(start);
15
+ this.seen.add(spec.key(start));
16
+ this.queue.push({ state: start, first: null, depth: 0 });
17
+ }
18
+ controller;
19
+ spec;
20
+ mode;
21
+ queue = [];
22
+ head = 0;
23
+ seen = /* @__PURE__ */ new Set();
24
+ startProgress;
25
+ progressDepth = Infinity;
26
+ progressFirst = [];
27
+ progressKeys = /* @__PURE__ */ new Set();
28
+ nodes = 0;
29
+ done = false;
30
+ exhausted = false;
31
+ /** Expands up to `maxNodes` more positions; true once the search has its answer. */
32
+ step(maxNodes) {
33
+ if (this.done) return true;
34
+ const stopAt = this.nodes + maxNodes;
35
+ while (this.nodes < stopAt) {
36
+ if (this.head >= this.queue.length) {
37
+ this.exhausted = true;
38
+ this.done = true;
39
+ return true;
40
+ }
41
+ const node = this.queue[this.head];
42
+ if (node.depth >= this.progressDepth) {
43
+ this.done = true;
44
+ return true;
45
+ }
46
+ this.queue[this.head++] = void 0;
47
+ this.nodes++;
48
+ if (this.expand(node)) {
49
+ this.done = true;
50
+ return true;
51
+ }
52
+ }
53
+ return false;
54
+ }
55
+ /** Positions expanded so far. */
56
+ get expanded() {
57
+ return this.nodes;
58
+ }
59
+ /** Runs to the end, or until `budget` positions have been expanded, or (given) `maxMs` have passed. */
60
+ run(budget, maxMs = Infinity) {
61
+ const until = Date.now() + maxMs;
62
+ while (this.nodes < budget && !this.step(Math.min(25, budget - this.nodes))) {
63
+ if (Date.now() >= until) break;
64
+ }
65
+ return this.outcome();
66
+ }
67
+ outcome() {
68
+ const found = this.progressFirst.length > 0 ? "progress" : null;
69
+ return { found, firstSteps: this.progressFirst, exhausted: this.exhausted && found === null, nodes: this.nodes };
70
+ }
71
+ /** True when the search can stop (alive mode found progress). */
72
+ expand(node) {
73
+ const { controller, spec } = this;
74
+ const moves = this.orderedMoves(node);
75
+ if (this.mode === "alive" && spec.rescueMoves) moves.push(...spec.rescueMoves(node.state));
76
+ const steps = moves.map((move) => ({ kind: "move", move }));
77
+ if (spec.drawIsStep && controller.drawStock) steps.push({ kind: "draw" });
78
+ for (const step of steps) {
79
+ const next = step.kind === "draw" ? controller.drawStock(node.state) : controller.tryMove(node.state, step.move);
80
+ if (!next) continue;
81
+ const key = spec.key(next);
82
+ if (this.seen.has(key)) continue;
83
+ this.seen.add(key);
84
+ const first = node.first ?? step;
85
+ const depth = node.depth + 1;
86
+ if (higher(spec.progress(next), this.startProgress)) {
87
+ if (this.mode === "alive") {
88
+ this.progressFirst.push(first);
89
+ return true;
90
+ }
91
+ this.progressDepth = Math.min(this.progressDepth, depth);
92
+ const firstKey = stepKey(first);
93
+ if (depth === this.progressDepth && !this.progressKeys.has(firstKey)) {
94
+ this.progressKeys.add(firstKey);
95
+ this.progressFirst.push(first);
96
+ }
97
+ continue;
98
+ }
99
+ this.queue.push({ state: next, first, depth });
100
+ }
101
+ return false;
102
+ }
103
+ /** At the start, the order Hint should offer moves in when several get somewhere equally soon: foundation moves first,
104
+ * then the game's own scorer. Deeper down the order doesn't matter, so it is left as it comes. */
105
+ orderedMoves(node) {
106
+ const moves = this.spec.moves(node.state);
107
+ if (node.depth > 0 || this.mode === "alive") return moves;
108
+ const score = this.controller.scoreHintMove;
109
+ const rank = (m) => (m.to.zone === "foundation" ? 1e9 : 0) + (score ? score(node.state, m) : 0);
110
+ return moves.map((m, i) => ({ m, i, r: rank(m) })).sort((a, b) => b.r - a.r || a.i - b.i).map((x) => x.m);
111
+ }
112
+ };
113
+ var HINT_SEARCH_BUDGET = 1500;
114
+ var HINT_SEARCH_MAX_MS = 80;
115
+ var LOOKAHEAD_BUDGET = 4e4;
116
+ var LOOKAHEAD_MAX_MS = 1e3;
117
+ function isConclusive(outcome) {
118
+ return outcome.found === "progress" || outcome.exhausted;
119
+ }
120
+ function allDestinations(controller, state, zones, extra = []) {
121
+ const out = [];
122
+ const seen = /* @__PURE__ */ new Set();
123
+ const add = (move, checked) => {
124
+ const key = `${move.from.zone}${move.from.index}>${move.to.zone}${move.to.index}x${move.count}`;
125
+ if (seen.has(key)) return;
126
+ seen.add(key);
127
+ if (checked || controller.tryMove(state, move)) out.push(move);
128
+ };
129
+ const legal = controller.legalMoves(state);
130
+ for (const move of legal) add(move, true);
131
+ for (const move of legal) {
132
+ const size = zones[move.to.zone];
133
+ if (size === void 0) continue;
134
+ for (let i = 0; i < size; i++) if (i !== move.to.index) add({ from: move.from, to: { zone: move.to.zone, index: i }, count: move.count }, false);
135
+ }
136
+ for (const move of extra) add(move, false);
137
+ return out;
138
+ }
139
+ function foundationDownMoves(foundations, columns, takes) {
140
+ const moves = [];
141
+ foundations.forEach((pile, f) => {
142
+ const card = pile[pile.length - 1];
143
+ if (card === void 0) return;
144
+ columns.forEach((column, t) => {
145
+ if (takes(column, [card])) moves.push({ from: { zone: "foundation", index: f }, to: { zone: "tableau", index: t }, count: 1 });
146
+ });
147
+ });
148
+ return moves;
149
+ }
150
+ var SUIT_CODE = { clubs: 0, diamonds: 1, hearts: 2, spades: 3 };
151
+ function pileKey(cards) {
152
+ let key = "";
153
+ for (const c of cards) key += String.fromCharCode(48 + (SUIT_CODE[c.suit] ?? 0) * 13 + c.rank + (c.faceUp ? 0 : 60));
154
+ return key;
155
+ }
156
+ function columnsKey(columns) {
157
+ return columns.map(pileKey).sort().join("|");
158
+ }
159
+ function faceDownCount(columns) {
160
+ let n = 0;
161
+ for (const col of columns) for (const c of col) if (!c.faceUp) n++;
162
+ return n;
163
+ }
164
+
165
+ export {
166
+ ProgressSearch,
167
+ HINT_SEARCH_BUDGET,
168
+ HINT_SEARCH_MAX_MS,
169
+ LOOKAHEAD_BUDGET,
170
+ LOOKAHEAD_MAX_MS,
171
+ isConclusive,
172
+ allDestinations,
173
+ foundationDownMoves,
174
+ pileKey,
175
+ columnsKey,
176
+ faceDownCount
177
+ };