@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.
- package/LICENSE +19 -0
- package/README.md +68 -2
- package/dist/acesup.d.mts +17 -0
- package/dist/acesup.d.ts +17 -0
- package/dist/acesup.js +8 -0
- package/dist/acesup.mjs +8 -0
- package/dist/autoFoundation-CfnFNysj.d.ts +63 -0
- package/dist/autoFoundation-Ctp9LKa-.d.mts +63 -0
- package/dist/canfield.d.mts +64 -0
- package/dist/canfield.d.ts +64 -0
- package/dist/canfield.js +20 -0
- package/dist/canfield.mjs +20 -0
- package/dist/chunk-22DC2YH6.js +95 -0
- package/dist/chunk-3BMUGHKQ.mjs +95 -0
- package/dist/chunk-4HJ7QMYV.mjs +339 -0
- package/dist/chunk-4WAWIFQF.js +131 -0
- package/dist/chunk-5266UC2Q.mjs +271 -0
- package/dist/chunk-6JWUTZVU.mjs +214 -0
- package/dist/chunk-AY3CR5EH.mjs +212 -0
- package/dist/chunk-B56TRALO.js +23 -0
- package/dist/chunk-CV3VJX34.mjs +341 -0
- package/dist/chunk-FDHHAPZA.mjs +177 -0
- package/dist/chunk-FTR733R7.mjs +297 -0
- package/dist/chunk-GFP5NUYK.mjs +23 -0
- package/dist/chunk-IKLYXDG7.mjs +131 -0
- package/dist/chunk-LH3UZP7D.js +165 -0
- package/dist/chunk-LSS44UKH.js +22 -0
- package/dist/chunk-M37CNJHF.mjs +165 -0
- package/dist/chunk-PLSNST56.mjs +163 -0
- package/dist/chunk-PTRHGPCN.js +163 -0
- package/dist/chunk-S3WNVF2K.js +339 -0
- package/dist/chunk-SDZPP3HI.mjs +22 -0
- package/dist/chunk-SLQ7RGMZ.js +212 -0
- package/dist/chunk-TNMFNWEM.mjs +249 -0
- package/dist/chunk-VGJPT3CX.js +214 -0
- package/dist/chunk-W5CPH6TD.js +115 -0
- package/dist/chunk-WJGEX74V.js +341 -0
- package/dist/chunk-WKES4P36.js +297 -0
- package/dist/chunk-XPTQNUF3.js +348 -0
- package/dist/chunk-XSS5M6C7.mjs +115 -0
- package/dist/chunk-XTHDUWJF.js +271 -0
- package/dist/chunk-XV5OUPIW.js +177 -0
- package/dist/chunk-YKY4K67K.mjs +348 -0
- package/dist/chunk-ZMNGTXU5.js +249 -0
- package/dist/fortythieves.d.mts +50 -0
- package/dist/fortythieves.d.ts +50 -0
- package/dist/fortythieves.js +22 -0
- package/dist/fortythieves.mjs +22 -0
- package/dist/freecell.d.mts +30 -0
- package/dist/freecell.d.ts +30 -0
- package/dist/freecell.js +18 -0
- package/dist/freecell.mjs +18 -0
- package/dist/gameTypes-BnfLkwzA.d.mts +201 -0
- package/dist/gameTypes-BnfLkwzA.d.ts +201 -0
- package/dist/golf.d.mts +17 -0
- package/dist/golf.d.ts +17 -0
- package/dist/golf.js +7 -0
- package/dist/golf.mjs +7 -0
- package/dist/index.d.mts +176 -0
- package/dist/index.d.ts +176 -0
- package/dist/index.js +446 -0
- package/dist/index.mjs +446 -0
- package/dist/klondike.d.mts +25 -0
- package/dist/klondike.d.ts +25 -0
- package/dist/klondike.js +15 -0
- package/dist/klondike.mjs +15 -0
- package/dist/pyramid.d.mts +36 -0
- package/dist/pyramid.d.ts +36 -0
- package/dist/pyramid.js +16 -0
- package/dist/pyramid.mjs +16 -0
- package/dist/scorpion.d.mts +14 -0
- package/dist/scorpion.d.ts +14 -0
- package/dist/scorpion.js +9 -0
- package/dist/scorpion.mjs +9 -0
- package/dist/spider.d.mts +22 -0
- package/dist/spider.d.ts +22 -0
- package/dist/spider.js +11 -0
- package/dist/spider.mjs +11 -0
- package/dist/tripeaks.d.mts +43 -0
- package/dist/tripeaks.d.ts +43 -0
- package/dist/tripeaks.js +19 -0
- package/dist/tripeaks.mjs +19 -0
- package/dist/yukon.d.mts +24 -0
- package/dist/yukon.d.ts +24 -0
- package/dist/yukon.js +15 -0
- package/dist/yukon.mjs +15 -0
- package/package.json +187 -4
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunk22DC2YH6js = require('./chunk-22DC2YH6.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunkLSS44UKHjs = require('./chunk-LSS44UKH.js');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
var _chunkB56TRALOjs = require('./chunk-B56TRALO.js');
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
var _chunkXV5OUPIWjs = require('./chunk-XV5OUPIW.js');
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
var _chunkW5CPH6TDjs = require('./chunk-W5CPH6TD.js');
|
|
23
|
+
|
|
24
|
+
// src/games/yukon/rules.ts
|
|
25
|
+
function cloneState(state) {
|
|
26
|
+
return {
|
|
27
|
+
tableau: state.tableau.slice(),
|
|
28
|
+
foundation: state.foundation.slice()
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function getPile(state, ref) {
|
|
32
|
+
switch (ref.zone) {
|
|
33
|
+
case "tableau":
|
|
34
|
+
return _nullishCoalesce(state.tableau[ref.index], () => ( null));
|
|
35
|
+
case "foundation":
|
|
36
|
+
return _nullishCoalesce(state.foundation[ref.index], () => ( null));
|
|
37
|
+
default:
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function setPile(state, ref, pile) {
|
|
42
|
+
switch (ref.zone) {
|
|
43
|
+
case "tableau":
|
|
44
|
+
state.tableau[ref.index] = pile;
|
|
45
|
+
break;
|
|
46
|
+
case "foundation":
|
|
47
|
+
state.foundation[ref.index] = pile;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function flipFaceUp(pile) {
|
|
52
|
+
const i = pile.length - 1;
|
|
53
|
+
const c = pile[i];
|
|
54
|
+
if (c && !c.faceUp) pile[i] = { ...c, faceUp: true };
|
|
55
|
+
}
|
|
56
|
+
function canPlaceOnTableau(dest, moving) {
|
|
57
|
+
if (moving.length === 0) return false;
|
|
58
|
+
const movingTop = moving[0];
|
|
59
|
+
const destTop = _chunkW5CPH6TDjs.top.call(void 0, dest);
|
|
60
|
+
if (destTop === void 0) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
if (!destTop.faceUp) return false;
|
|
64
|
+
const destColor = _chunkW5CPH6TDjs.cardColor.call(void 0, destTop.suit);
|
|
65
|
+
const movingColor = _chunkW5CPH6TDjs.cardColor.call(void 0, movingTop.suit);
|
|
66
|
+
return movingTop.rank === destTop.rank - 1 && destColor !== movingColor;
|
|
67
|
+
}
|
|
68
|
+
function altColorRunLengthAtTop(column) {
|
|
69
|
+
if (column.length === 0) return 0;
|
|
70
|
+
if (!column[column.length - 1].faceUp) return 0;
|
|
71
|
+
let len = 1;
|
|
72
|
+
for (let i = column.length - 2; i >= 0; i--) {
|
|
73
|
+
const cur = column[i];
|
|
74
|
+
const next = column[i + 1];
|
|
75
|
+
if (!cur.faceUp || cur.rank !== next.rank + 1 || _chunkW5CPH6TDjs.cardColor.call(void 0, cur.suit) === _chunkW5CPH6TDjs.cardColor.call(void 0, next.suit)) break;
|
|
76
|
+
len++;
|
|
77
|
+
}
|
|
78
|
+
return len;
|
|
79
|
+
}
|
|
80
|
+
function scoreHintMove(state, move) {
|
|
81
|
+
if (move.from.zone !== "tableau" || move.to.zone !== "tableau") return 0;
|
|
82
|
+
const fromCol = state.tableau[move.from.index];
|
|
83
|
+
const toCol = state.tableau[move.to.index];
|
|
84
|
+
if (!fromCol || !toCol) return 0;
|
|
85
|
+
const group = fromCol.slice(fromCol.length - move.count);
|
|
86
|
+
const remainder = fromCol.slice(0, fromCol.length - move.count);
|
|
87
|
+
const runBefore = Math.max(altColorRunLengthAtTop(fromCol), altColorRunLengthAtTop(toCol));
|
|
88
|
+
const runAfterDest = altColorRunLengthAtTop([...toCol, ...group]);
|
|
89
|
+
const runAfterRemainder = altColorRunLengthAtTop(remainder);
|
|
90
|
+
const netRunStructureChange = Math.max(runAfterDest, runAfterRemainder) - runBefore;
|
|
91
|
+
const fromBecomesEmpty = move.count === fromCol.length;
|
|
92
|
+
const toWasEmpty = toCol.length === 0;
|
|
93
|
+
const netEmptyDelta = (fromBecomesEmpty ? 1 : 0) - (toWasEmpty ? 1 : 0);
|
|
94
|
+
const newFromTop = remainder[remainder.length - 1];
|
|
95
|
+
const revealsHiddenCard = newFromTop !== void 0 && !newFromTop.faceUp;
|
|
96
|
+
let score = revealsHiddenCard ? 500 : 0;
|
|
97
|
+
score += netRunStructureChange * 30;
|
|
98
|
+
score += netEmptyDelta * 40;
|
|
99
|
+
if (score <= 0) return score;
|
|
100
|
+
return score + move.count * 2;
|
|
101
|
+
}
|
|
102
|
+
function searchMoves(state) {
|
|
103
|
+
const moves = [];
|
|
104
|
+
for (let col = 0; col < 7; col++) {
|
|
105
|
+
const pile = state.tableau[col];
|
|
106
|
+
if (pile.length === 0) continue;
|
|
107
|
+
const from = { zone: "tableau", index: col };
|
|
108
|
+
const topCard = pile[pile.length - 1];
|
|
109
|
+
if (topCard.faceUp) {
|
|
110
|
+
const f = _chunkLSS44UKHjs.suitIndex.call(void 0, topCard.suit);
|
|
111
|
+
if (_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, state.foundation[f], topCard)) moves.push({ from, to: { zone: "foundation", index: f }, count: 1 });
|
|
112
|
+
}
|
|
113
|
+
for (let i = pile.length - 1; i >= 0 && pile[i].faceUp; i--) {
|
|
114
|
+
const group = [pile[i]];
|
|
115
|
+
for (let t = 0; t < 7; t++) if (t !== col && canPlaceOnTableau(state.tableau[t], group)) moves.push({ from, to: { zone: "tableau", index: t }, count: pile.length - i });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return moves;
|
|
119
|
+
}
|
|
120
|
+
function createYukonController() {
|
|
121
|
+
function newGame(seed) {
|
|
122
|
+
const deck = _chunkW5CPH6TDjs.shuffledDeck.call(void 0, seed, 1);
|
|
123
|
+
let cursor = 0;
|
|
124
|
+
const tableau = [];
|
|
125
|
+
for (let col = 0; col < 7; col++) {
|
|
126
|
+
const count = col + 1;
|
|
127
|
+
const cards = [];
|
|
128
|
+
for (let i = 0; i < count; i++) {
|
|
129
|
+
const card = deck[cursor++];
|
|
130
|
+
const faceUp = i === count - 1;
|
|
131
|
+
cards.push({ ...card, faceUp });
|
|
132
|
+
}
|
|
133
|
+
tableau.push(cards);
|
|
134
|
+
}
|
|
135
|
+
for (let col = 1; col < 7; col++) {
|
|
136
|
+
for (let i = 0; i < 4; i++) {
|
|
137
|
+
const card = deck[cursor++];
|
|
138
|
+
tableau[col].push({ ...card, faceUp: true });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const foundation = [[], [], [], []];
|
|
142
|
+
return { tableau, foundation };
|
|
143
|
+
}
|
|
144
|
+
function tryMove(state, move) {
|
|
145
|
+
const { from, to, count } = move;
|
|
146
|
+
if (count < 1) return null;
|
|
147
|
+
if (from.zone === to.zone && from.index === to.index) return null;
|
|
148
|
+
if (from.zone === "foundation" && (to.zone !== "tableau" || count !== 1)) return null;
|
|
149
|
+
const sourcePile = getPile(state, from);
|
|
150
|
+
if (!sourcePile || sourcePile.length < count) return null;
|
|
151
|
+
if (count > 1 && from.zone !== "tableau") return null;
|
|
152
|
+
const movingCards = sourcePile.slice(sourcePile.length - count);
|
|
153
|
+
if (movingCards.some((c) => !c.faceUp)) return null;
|
|
154
|
+
const destPile = getPile(state, to);
|
|
155
|
+
if (!destPile) return null;
|
|
156
|
+
if (to.zone === "tableau") {
|
|
157
|
+
if (!canPlaceOnTableau(destPile, movingCards)) return null;
|
|
158
|
+
} else if (to.zone === "foundation") {
|
|
159
|
+
if (count !== 1) return null;
|
|
160
|
+
const movingCard = movingCards[0];
|
|
161
|
+
if (_chunkLSS44UKHjs.suitIndex.call(void 0, movingCard.suit) !== to.index) return null;
|
|
162
|
+
if (!_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, destPile, movingCard)) return null;
|
|
163
|
+
} else {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
const next = cloneState(state);
|
|
167
|
+
const nextSourcePile = getPile(next, from).slice();
|
|
168
|
+
const nextDestPile = getPile(next, to).slice();
|
|
169
|
+
setPile(next, from, nextSourcePile);
|
|
170
|
+
setPile(next, to, nextDestPile);
|
|
171
|
+
const moved = nextSourcePile.splice(nextSourcePile.length - count, count);
|
|
172
|
+
nextDestPile.push(...moved);
|
|
173
|
+
if (from.zone === "tableau") {
|
|
174
|
+
flipFaceUp(nextSourcePile);
|
|
175
|
+
}
|
|
176
|
+
return next;
|
|
177
|
+
}
|
|
178
|
+
function isWon(state) {
|
|
179
|
+
return state.foundation.every((pile) => {
|
|
180
|
+
const t = _chunkW5CPH6TDjs.top.call(void 0, pile);
|
|
181
|
+
return t !== void 0 && t.rank === 13;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function legalMoves(state) {
|
|
185
|
+
const moves = [];
|
|
186
|
+
const tryAdd = (from, to, count) => {
|
|
187
|
+
const result = tryMove(state, { from, to, count });
|
|
188
|
+
if (result) moves.push({ from, to, count });
|
|
189
|
+
};
|
|
190
|
+
for (let col = 0; col < 7; col++) {
|
|
191
|
+
const pile = state.tableau[col];
|
|
192
|
+
if (pile.length === 0) continue;
|
|
193
|
+
let runStart = pile.length - 1;
|
|
194
|
+
while (runStart > 0 && pile[runStart - 1].faceUp) {
|
|
195
|
+
runStart -= 1;
|
|
196
|
+
}
|
|
197
|
+
const from = { zone: "tableau", index: col };
|
|
198
|
+
for (let f = 0; f < 4; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
|
|
199
|
+
for (let count = 1; count <= pile.length - runStart; count++) {
|
|
200
|
+
const movingCards = pile.slice(pile.length - count);
|
|
201
|
+
const candidates = state.tableau.map((destPile, index) => ({ index, pile: destPile })).filter((c) => c.index !== col).filter((c) => canPlaceOnTableau(c.pile, movingCards));
|
|
202
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.alwaysValidRun);
|
|
203
|
+
if (best) tryAdd(from, { zone: "tableau", index: best.index }, count);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return moves;
|
|
207
|
+
}
|
|
208
|
+
function autoMoveHint(state, from, cardId) {
|
|
209
|
+
const pile = getPile(state, from);
|
|
210
|
+
if (!pile) return null;
|
|
211
|
+
const cardIndex = pile.findIndex((c) => c.id === cardId);
|
|
212
|
+
if (cardIndex === -1) return null;
|
|
213
|
+
const movingCards = pile.slice(cardIndex);
|
|
214
|
+
if (movingCards.some((c) => !c.faceUp)) return null;
|
|
215
|
+
const count = movingCards.length;
|
|
216
|
+
if (count > 1 && from.zone !== "tableau") return null;
|
|
217
|
+
if (count === 1) {
|
|
218
|
+
const movingCard = movingCards[0];
|
|
219
|
+
const f = _chunkLSS44UKHjs.suitIndex.call(void 0, movingCard.suit);
|
|
220
|
+
if (_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, state.foundation[f], movingCard)) {
|
|
221
|
+
return { from, to: { zone: "foundation", index: f }, count: 1 };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const candidates = state.tableau.map((destPile, index) => ({ index, pile: destPile })).filter((c) => !(from.zone === "tableau" && from.index === c.index)).filter((c) => canPlaceOnTableau(c.pile, movingCards));
|
|
225
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.alwaysValidRun);
|
|
226
|
+
if (!best) return null;
|
|
227
|
+
return { from, to: { zone: "tableau", index: best.index }, count };
|
|
228
|
+
}
|
|
229
|
+
function accessibleCards(state) {
|
|
230
|
+
const result = [];
|
|
231
|
+
state.tableau.forEach((pile, index) => {
|
|
232
|
+
const t = _chunkW5CPH6TDjs.top.call(void 0, pile);
|
|
233
|
+
if (t && t.faceUp) result.push({ ref: { zone: "tableau", index }, card: t });
|
|
234
|
+
});
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
const progress = {
|
|
238
|
+
id: "yukon",
|
|
239
|
+
moves: searchMoves,
|
|
240
|
+
rescueMoves: (state) => _chunkXV5OUPIWjs.foundationDownMoves.call(void 0, state.foundation, state.tableau, canPlaceOnTableau),
|
|
241
|
+
drawIsStep: false,
|
|
242
|
+
key: (state) => _chunkXV5OUPIWjs.columnsKey.call(void 0, state.tableau),
|
|
243
|
+
progress: (state) => [-_chunkXV5OUPIWjs.faceDownCount.call(void 0, state.tableau), state.foundation.reduce((n, p) => n + p.length, 0)]
|
|
244
|
+
};
|
|
245
|
+
return {
|
|
246
|
+
id: "yukon",
|
|
247
|
+
title: "Yukon",
|
|
248
|
+
newGame,
|
|
249
|
+
legalMoves,
|
|
250
|
+
tryMove,
|
|
251
|
+
isWon,
|
|
252
|
+
autoMoveHint,
|
|
253
|
+
accessibleCards,
|
|
254
|
+
scoreHintMove,
|
|
255
|
+
progress
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
function canAutoFinish(controller, state) {
|
|
259
|
+
return !controller.isWon(state) && state.tableau.every((column) => column.every((c) => c.faceUp)) && _chunk22DC2YH6js.isFullyAutoCompletable.call(void 0, controller, state);
|
|
260
|
+
}
|
|
261
|
+
function nextFinishMove(controller, state) {
|
|
262
|
+
if (!canAutoFinish(controller, state)) return null;
|
|
263
|
+
const entry = _chunk22DC2YH6js.nextAutoCompleteMove.call(void 0, controller, state);
|
|
264
|
+
return entry ? { entry, to: { zone: "foundation", index: _chunkLSS44UKHjs.suitIndex.call(void 0, entry.card.suit) } } : null;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
exports.createYukonController = createYukonController; exports.canAutoFinish = canAutoFinish; exports.nextFinishMove = nextFinishMove;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _class;// 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 = class {
|
|
10
|
+
constructor(controller, spec, start, mode) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);_class.prototype.__init7.call(this);_class.prototype.__init8.call(this);_class.prototype.__init9.call(this);
|
|
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
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
__init() {this.queue = []}
|
|
22
|
+
__init2() {this.head = 0}
|
|
23
|
+
__init3() {this.seen = /* @__PURE__ */ new Set()}
|
|
24
|
+
|
|
25
|
+
__init4() {this.progressDepth = Infinity}
|
|
26
|
+
__init5() {this.progressFirst = []}
|
|
27
|
+
__init6() {this.progressKeys = /* @__PURE__ */ new Set()}
|
|
28
|
+
__init7() {this.nodes = 0}
|
|
29
|
+
__init8() {this.done = false}
|
|
30
|
+
__init9() {this.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 = _nullishCoalesce(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
|
+
}, _class);
|
|
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 + (_nullishCoalesce(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
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
exports.ProgressSearch = ProgressSearch; exports.HINT_SEARCH_BUDGET = HINT_SEARCH_BUDGET; exports.HINT_SEARCH_MAX_MS = HINT_SEARCH_MAX_MS; exports.LOOKAHEAD_BUDGET = LOOKAHEAD_BUDGET; exports.LOOKAHEAD_MAX_MS = LOOKAHEAD_MAX_MS; exports.isConclusive = isConclusive; exports.allDestinations = allDestinations; exports.foundationDownMoves = foundationDownMoves; exports.pileKey = pileKey; exports.columnsKey = columnsKey; exports.faceDownCount = faceDownCount;
|