@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,341 @@
|
|
|
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 _chunkXV5OUPIWjs = require('./chunk-XV5OUPIW.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
var _chunkW5CPH6TDjs = require('./chunk-W5CPH6TD.js');
|
|
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 = _chunkW5CPH6TDjs.top.call(void 0, 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 = _chunkW5CPH6TDjs.top.call(void 0, 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 _nullishCoalesce(state.tableau[ref.index], () => ( null));
|
|
73
|
+
case "foundation":
|
|
74
|
+
return _nullishCoalesce(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 = _chunkW5CPH6TDjs.shuffledDeck.call(void 0, 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 = _chunkW5CPH6TDjs.top.call(void 0, state.waste);
|
|
111
|
+
if (wasteTop) result.push({ ref: { zone: "waste", index: 0 }, card: wasteTop });
|
|
112
|
+
state.tableau.forEach((pile, index) => {
|
|
113
|
+
const t = _chunkW5CPH6TDjs.top.call(void 0, 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 && !_chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, 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 || !_chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, 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) => `${_chunkXV5OUPIWjs.columnsKey.call(void 0, state.tableau)}/${_chunkXV5OUPIWjs.pileKey.call(void 0, 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
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
exports.findFoundationSlot = findFoundationSlot; exports.createFortyThievesController = createFortyThievesController; exports.isFullyAutoCompletable = isFullyAutoCompletable; exports.nextAutoCompleteMove = nextAutoCompleteMove; exports.canAutoFinish = canAutoFinish; exports.nextFinishMove = nextFinishMove; exports.isFullyAutoCompletableWithDraws = isFullyAutoCompletableWithDraws; exports.nextAutoCompleteMoveWithDraws = nextAutoCompleteMoveWithDraws;
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkLSS44UKHjs = require('./chunk-LSS44UKH.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunkB56TRALOjs = require('./chunk-B56TRALO.js');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
var _chunkXV5OUPIWjs = require('./chunk-XV5OUPIW.js');
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
var _chunkW5CPH6TDjs = require('./chunk-W5CPH6TD.js');
|
|
18
|
+
|
|
19
|
+
// src/games/freecell/rules.ts
|
|
20
|
+
var NUM_TABLEAU = 8;
|
|
21
|
+
var NUM_FREECELLS = 4;
|
|
22
|
+
var NUM_FOUNDATIONS = 4;
|
|
23
|
+
function cloneState(state) {
|
|
24
|
+
return {
|
|
25
|
+
tableau: state.tableau.slice(),
|
|
26
|
+
freecells: state.freecells.slice(),
|
|
27
|
+
foundation: state.foundation.slice()
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function canPlaceOnTableau(dest, movingTop) {
|
|
31
|
+
const destTop = _chunkW5CPH6TDjs.top.call(void 0, dest);
|
|
32
|
+
if (destTop === void 0) return true;
|
|
33
|
+
return movingTop.rank === destTop.rank - 1 && _chunkW5CPH6TDjs.cardColor.call(void 0, movingTop.suit) !== _chunkW5CPH6TDjs.cardColor.call(void 0, destTop.suit);
|
|
34
|
+
}
|
|
35
|
+
function runLengthAtTop(column) {
|
|
36
|
+
if (column.length === 0) return 0;
|
|
37
|
+
let len = 1;
|
|
38
|
+
for (let i = column.length - 2; i >= 0; i--) {
|
|
39
|
+
const cur = column[i];
|
|
40
|
+
const next = column[i + 1];
|
|
41
|
+
if (next.rank !== cur.rank - 1 || _chunkW5CPH6TDjs.cardColor.call(void 0, next.suit) === _chunkW5CPH6TDjs.cardColor.call(void 0, cur.suit)) break;
|
|
42
|
+
len++;
|
|
43
|
+
}
|
|
44
|
+
return len;
|
|
45
|
+
}
|
|
46
|
+
function countEmptyFreecells(state) {
|
|
47
|
+
return state.freecells.filter((c) => c === null).length;
|
|
48
|
+
}
|
|
49
|
+
function countEmptyTableauColumns(state, excludeIndex) {
|
|
50
|
+
return state.tableau.filter((col, i) => i !== excludeIndex && col.length === 0).length;
|
|
51
|
+
}
|
|
52
|
+
function maxSupermoveCount(state, destIndex) {
|
|
53
|
+
const emptyFreecells = countEmptyFreecells(state);
|
|
54
|
+
const emptyCols = countEmptyTableauColumns(state, destIndex);
|
|
55
|
+
return (emptyFreecells + 1) * 2 ** emptyCols;
|
|
56
|
+
}
|
|
57
|
+
function newGame(seed) {
|
|
58
|
+
const deck = _chunkW5CPH6TDjs.shuffledDeck.call(void 0, seed, 1).map((c) => ({ ...c, faceUp: true }));
|
|
59
|
+
const tableau = [];
|
|
60
|
+
let cursor = 0;
|
|
61
|
+
for (let col = 0; col < NUM_TABLEAU; col++) {
|
|
62
|
+
const count = col < 4 ? 7 : 6;
|
|
63
|
+
tableau.push(deck.slice(cursor, cursor + count));
|
|
64
|
+
cursor += count;
|
|
65
|
+
}
|
|
66
|
+
const freecells = [null, null, null, null];
|
|
67
|
+
const foundation = [[], [], [], []];
|
|
68
|
+
return { tableau, freecells, foundation };
|
|
69
|
+
}
|
|
70
|
+
function tryMove(state, move) {
|
|
71
|
+
const { from, to, count } = move;
|
|
72
|
+
if (count < 1) return null;
|
|
73
|
+
if (from.zone === to.zone && from.index === to.index) return null;
|
|
74
|
+
if (from.zone === "foundation") return null;
|
|
75
|
+
let movingCards;
|
|
76
|
+
if (from.zone === "tableau") {
|
|
77
|
+
const col = state.tableau[from.index];
|
|
78
|
+
if (!col || col.length < count) return null;
|
|
79
|
+
movingCards = col.slice(col.length - count);
|
|
80
|
+
} else if (from.zone === "freecell") {
|
|
81
|
+
if (count !== 1) return null;
|
|
82
|
+
const card = state.freecells[from.index];
|
|
83
|
+
if (!card) return null;
|
|
84
|
+
movingCards = [card];
|
|
85
|
+
} else {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
if (count > 1 && (from.zone !== "tableau" || to.zone !== "tableau")) return null;
|
|
89
|
+
if (count > 1 && !_chunkW5CPH6TDjs.isDescendingAltColor.call(void 0, movingCards)) return null;
|
|
90
|
+
if (to.zone === "tableau") {
|
|
91
|
+
const destCol = state.tableau[to.index];
|
|
92
|
+
if (!destCol) return null;
|
|
93
|
+
if (count > 1 && count > maxSupermoveCount(state, to.index)) return null;
|
|
94
|
+
if (!canPlaceOnTableau(destCol, movingCards[0])) return null;
|
|
95
|
+
} else if (to.zone === "foundation") {
|
|
96
|
+
if (count !== 1) return null;
|
|
97
|
+
const destPile = state.foundation[to.index];
|
|
98
|
+
if (!destPile) return null;
|
|
99
|
+
const movingCard = movingCards[0];
|
|
100
|
+
if (_chunkLSS44UKHjs.suitIndex.call(void 0, movingCard.suit) !== to.index) return null;
|
|
101
|
+
if (!_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, destPile, movingCard)) return null;
|
|
102
|
+
} else if (to.zone === "freecell") {
|
|
103
|
+
if (count !== 1) return null;
|
|
104
|
+
if (to.index < 0 || to.index >= NUM_FREECELLS) return null;
|
|
105
|
+
if (state.freecells[to.index] !== null) return null;
|
|
106
|
+
} else {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
const next = cloneState(state);
|
|
110
|
+
if (from.zone === "tableau") {
|
|
111
|
+
next.tableau[from.index] = next.tableau[from.index].slice(0, next.tableau[from.index].length - count);
|
|
112
|
+
} else if (from.zone === "freecell") {
|
|
113
|
+
next.freecells[from.index] = null;
|
|
114
|
+
}
|
|
115
|
+
if (to.zone === "tableau") {
|
|
116
|
+
next.tableau[to.index] = next.tableau[to.index].concat(movingCards);
|
|
117
|
+
} else if (to.zone === "foundation") {
|
|
118
|
+
next.foundation[to.index] = next.foundation[to.index].concat(movingCards);
|
|
119
|
+
} else if (to.zone === "freecell") {
|
|
120
|
+
next.freecells[to.index] = movingCards[0];
|
|
121
|
+
}
|
|
122
|
+
return next;
|
|
123
|
+
}
|
|
124
|
+
function isWon(state) {
|
|
125
|
+
return state.foundation.every((pile) => {
|
|
126
|
+
const t = _chunkW5CPH6TDjs.top.call(void 0, pile);
|
|
127
|
+
return t !== void 0 && t.rank === 13;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function legalMoves(state) {
|
|
131
|
+
const moves = [];
|
|
132
|
+
const tryAdd = (from, to, count) => {
|
|
133
|
+
const result = tryMove(state, { from, to, count });
|
|
134
|
+
if (result) moves.push({ from, to, count });
|
|
135
|
+
};
|
|
136
|
+
for (let fc = 0; fc < NUM_FREECELLS; fc++) {
|
|
137
|
+
if (state.freecells[fc] === null) continue;
|
|
138
|
+
const from = { zone: "freecell", index: fc };
|
|
139
|
+
for (let f = 0; f < NUM_FOUNDATIONS; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
|
|
140
|
+
for (let t = 0; t < NUM_TABLEAU; t++) tryAdd(from, { zone: "tableau", index: t }, 1);
|
|
141
|
+
}
|
|
142
|
+
for (let col = 0; col < NUM_TABLEAU; col++) {
|
|
143
|
+
const pile = state.tableau[col];
|
|
144
|
+
if (pile.length === 0) continue;
|
|
145
|
+
const from = { zone: "tableau", index: col };
|
|
146
|
+
for (let f = 0; f < NUM_FOUNDATIONS; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
|
|
147
|
+
for (let fc = 0; fc < NUM_FREECELLS; fc++) tryAdd(from, { zone: "freecell", index: fc }, 1);
|
|
148
|
+
let runStart = pile.length - 1;
|
|
149
|
+
while (runStart > 0 && _chunkW5CPH6TDjs.isDescendingAltColor.call(void 0, pile.slice(runStart - 1))) {
|
|
150
|
+
runStart -= 1;
|
|
151
|
+
}
|
|
152
|
+
const maxRun = pile.length - runStart;
|
|
153
|
+
for (let count = 1; count <= maxRun; count++) {
|
|
154
|
+
const movingCards = pile.slice(pile.length - count);
|
|
155
|
+
const movingTop = movingCards[0];
|
|
156
|
+
const candidates = state.tableau.map((destPile, index) => ({ index, pile: destPile })).filter((c) => c.index !== col).filter((c) => canPlaceOnTableau(c.pile, movingTop)).filter((c) => count === 1 || count <= maxSupermoveCount(state, c.index));
|
|
157
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.isDescendingAltColor);
|
|
158
|
+
if (best) tryAdd(from, { zone: "tableau", index: best.index }, count);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return moves;
|
|
162
|
+
}
|
|
163
|
+
function autoMoveHint(state, from, cardId) {
|
|
164
|
+
let movingCards;
|
|
165
|
+
if (from.zone === "tableau") {
|
|
166
|
+
const col = state.tableau[from.index];
|
|
167
|
+
if (!col) return null;
|
|
168
|
+
const cardIndex = col.findIndex((c) => c.id === cardId);
|
|
169
|
+
if (cardIndex === -1) return null;
|
|
170
|
+
movingCards = col.slice(cardIndex);
|
|
171
|
+
if (movingCards.length > 1 && !_chunkW5CPH6TDjs.isDescendingAltColor.call(void 0, movingCards)) return null;
|
|
172
|
+
} else if (from.zone === "freecell") {
|
|
173
|
+
const c = state.freecells[from.index];
|
|
174
|
+
if (!c || c.id !== cardId) return null;
|
|
175
|
+
movingCards = [c];
|
|
176
|
+
} else {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
const count = movingCards.length;
|
|
180
|
+
const movingTop = movingCards[0];
|
|
181
|
+
if (count === 1) {
|
|
182
|
+
const f = _chunkLSS44UKHjs.suitIndex.call(void 0, movingTop.suit);
|
|
183
|
+
if (_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, state.foundation[f], movingTop)) {
|
|
184
|
+
return { from, to: { zone: "foundation", index: f }, count: 1 };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const candidates = state.tableau.map((destPile, index) => ({ index, pile: destPile })).filter((c) => !(from.zone === "tableau" && from.index === c.index)).filter((c) => canPlaceOnTableau(c.pile, movingTop)).filter((c) => count === 1 || count <= maxSupermoveCount(state, c.index));
|
|
188
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.isDescendingAltColor);
|
|
189
|
+
if (best) return { from, to: { zone: "tableau", index: best.index }, count };
|
|
190
|
+
if (count === 1 && from.zone !== "freecell") {
|
|
191
|
+
const emptyFreecell = state.freecells.findIndex((c) => c === null);
|
|
192
|
+
if (emptyFreecell !== -1) {
|
|
193
|
+
return { from, to: { zone: "freecell", index: emptyFreecell }, count: 1 };
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
function scoreHintMove(state, move) {
|
|
199
|
+
if (move.to.zone === "foundation") return 0;
|
|
200
|
+
const fromCol = move.from.zone === "tableau" ? state.tableau[move.from.index] : void 0;
|
|
201
|
+
const toCol = move.to.zone === "tableau" ? state.tableau[move.to.index] : void 0;
|
|
202
|
+
if (move.from.zone === "tableau" && !fromCol) return 0;
|
|
203
|
+
if (move.to.zone === "tableau" && !toCol) return 0;
|
|
204
|
+
let a = 0;
|
|
205
|
+
if (fromCol && fromCol.length >= move.count) {
|
|
206
|
+
const remainder = fromCol.slice(0, fromCol.length - move.count);
|
|
207
|
+
const below = remainder[remainder.length - 1];
|
|
208
|
+
const base = fromCol[fromCol.length - move.count];
|
|
209
|
+
if (below !== void 0 && _chunkW5CPH6TDjs.isDescendingAltColor.call(void 0, [below, base])) a = runLengthAtTop(remainder);
|
|
210
|
+
}
|
|
211
|
+
const b = toCol ? runLengthAtTop(toCol) : 0;
|
|
212
|
+
const netRunStructureChange = Math.max(move.count + b, a) - Math.max(move.count + a, b);
|
|
213
|
+
const fromBecomesEmpty = fromCol !== void 0 && move.count === fromCol.length;
|
|
214
|
+
const toWasEmpty = toCol !== void 0 && toCol.length === 0;
|
|
215
|
+
const netEmptyColumnDelta = (fromBecomesEmpty ? 1 : 0) - (toWasEmpty ? 1 : 0);
|
|
216
|
+
const freesFreecell = move.from.zone === "freecell" ? 1 : 0;
|
|
217
|
+
const consumesFreecell = move.to.zone === "freecell" ? 1 : 0;
|
|
218
|
+
const netFreecellDelta = freesFreecell - consumesFreecell;
|
|
219
|
+
let score = netRunStructureChange * 30;
|
|
220
|
+
score += netEmptyColumnDelta * 50;
|
|
221
|
+
score += netFreecellDelta * 25;
|
|
222
|
+
if (score <= 0) return score;
|
|
223
|
+
return score + (move.count + b) * 2;
|
|
224
|
+
}
|
|
225
|
+
function moveHeadCard(state, from, count) {
|
|
226
|
+
if (from.zone === "tableau") return _optionalChain([state, 'access', _ => _.tableau, 'access', _2 => _2[from.index], 'optionalAccess', _3 => _3[state.tableau[from.index].length - count]]);
|
|
227
|
+
if (from.zone === "freecell") return count === 1 ? _nullishCoalesce(state.freecells[from.index], () => ( void 0)) : void 0;
|
|
228
|
+
return void 0;
|
|
229
|
+
}
|
|
230
|
+
function accessibleCards(state) {
|
|
231
|
+
const result = [];
|
|
232
|
+
state.freecells.forEach((card, index) => {
|
|
233
|
+
if (card) result.push({ ref: { zone: "freecell", index }, card });
|
|
234
|
+
});
|
|
235
|
+
state.tableau.forEach((pile, index) => {
|
|
236
|
+
const t = _chunkW5CPH6TDjs.top.call(void 0, pile);
|
|
237
|
+
if (t) result.push({ ref: { zone: "tableau", index }, card: t });
|
|
238
|
+
});
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
function searchMoves(state) {
|
|
242
|
+
const moves = [];
|
|
243
|
+
const firstEmpty = state.freecells.indexOf(null);
|
|
244
|
+
const toFoundation = (from, card) => {
|
|
245
|
+
const f = _chunkLSS44UKHjs.suitIndex.call(void 0, card.suit);
|
|
246
|
+
if (_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, state.foundation[f], card)) moves.push({ from, to: { zone: "foundation", index: f }, count: 1 });
|
|
247
|
+
};
|
|
248
|
+
state.freecells.forEach((card, fc) => {
|
|
249
|
+
if (!card) return;
|
|
250
|
+
const from = { zone: "freecell", index: fc };
|
|
251
|
+
toFoundation(from, card);
|
|
252
|
+
for (let t = 0; t < NUM_TABLEAU; t++) if (canPlaceOnTableau(state.tableau[t], card)) moves.push({ from, to: { zone: "tableau", index: t }, count: 1 });
|
|
253
|
+
});
|
|
254
|
+
for (let col = 0; col < NUM_TABLEAU; col++) {
|
|
255
|
+
const pile = state.tableau[col];
|
|
256
|
+
if (pile.length === 0) continue;
|
|
257
|
+
const from = { zone: "tableau", index: col };
|
|
258
|
+
toFoundation(from, pile[pile.length - 1]);
|
|
259
|
+
if (firstEmpty !== -1) moves.push({ from, to: { zone: "freecell", index: firstEmpty }, count: 1 });
|
|
260
|
+
const runLen = runLengthAtTop(pile);
|
|
261
|
+
for (let t = 0; t < NUM_TABLEAU; t++) {
|
|
262
|
+
if (t === col) continue;
|
|
263
|
+
const capacity = maxSupermoveCount(state, t);
|
|
264
|
+
for (let count = 1; count <= Math.min(runLen, capacity); count++) {
|
|
265
|
+
if (canPlaceOnTableau(state.tableau[t], pile[pile.length - count])) moves.push({ from, to: { zone: "tableau", index: t }, count });
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return moves;
|
|
270
|
+
}
|
|
271
|
+
var progress = {
|
|
272
|
+
id: "freecell",
|
|
273
|
+
moves: searchMoves,
|
|
274
|
+
drawIsStep: false,
|
|
275
|
+
key: (state) => `${_chunkXV5OUPIWjs.columnsKey.call(void 0, state.tableau)}/${state.freecells.map((c) => c ? _chunkXV5OUPIWjs.pileKey.call(void 0, [c]) : "").sort().join("")}`,
|
|
276
|
+
progress: (state) => [state.foundation.reduce((n, p) => n + p.length, 0)]
|
|
277
|
+
};
|
|
278
|
+
var FreeCellController = {
|
|
279
|
+
id: "freecell",
|
|
280
|
+
title: "FreeCell",
|
|
281
|
+
newGame,
|
|
282
|
+
legalMoves,
|
|
283
|
+
tryMove,
|
|
284
|
+
isWon,
|
|
285
|
+
autoMoveHint,
|
|
286
|
+
scoreHintMove,
|
|
287
|
+
accessibleCards,
|
|
288
|
+
progress
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
exports.countEmptyFreecells = countEmptyFreecells; exports.countEmptyTableauColumns = countEmptyTableauColumns; exports.maxSupermoveCount = maxSupermoveCount; exports.moveHeadCard = moveHeadCard; exports.FreeCellController = FreeCellController;
|