@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,339 @@
|
|
|
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
|
+
|
|
17
|
+
var _chunkXV5OUPIWjs = require('./chunk-XV5OUPIW.js');
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
var _chunkW5CPH6TDjs = require('./chunk-W5CPH6TD.js');
|
|
24
|
+
|
|
25
|
+
// src/games/klondike/rules.ts
|
|
26
|
+
function cloneState(state) {
|
|
27
|
+
return {
|
|
28
|
+
tableau: state.tableau.slice(),
|
|
29
|
+
foundation: state.foundation.slice(),
|
|
30
|
+
stock: state.stock,
|
|
31
|
+
waste: state.waste
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function getPile(state, ref) {
|
|
35
|
+
switch (ref.zone) {
|
|
36
|
+
case "tableau":
|
|
37
|
+
return _nullishCoalesce(state.tableau[ref.index], () => ( null));
|
|
38
|
+
case "foundation":
|
|
39
|
+
return _nullishCoalesce(state.foundation[ref.index], () => ( null));
|
|
40
|
+
case "stock":
|
|
41
|
+
return state.stock;
|
|
42
|
+
case "waste":
|
|
43
|
+
return state.waste;
|
|
44
|
+
default:
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function setPile(state, ref, pile) {
|
|
49
|
+
switch (ref.zone) {
|
|
50
|
+
case "tableau":
|
|
51
|
+
state.tableau[ref.index] = pile;
|
|
52
|
+
break;
|
|
53
|
+
case "foundation":
|
|
54
|
+
state.foundation[ref.index] = pile;
|
|
55
|
+
break;
|
|
56
|
+
case "stock":
|
|
57
|
+
state.stock = pile;
|
|
58
|
+
break;
|
|
59
|
+
case "waste":
|
|
60
|
+
state.waste = pile;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function flipFaceUp(pile) {
|
|
65
|
+
const i = pile.length - 1;
|
|
66
|
+
const c = pile[i];
|
|
67
|
+
if (c && !c.faceUp) pile[i] = { ...c, faceUp: true };
|
|
68
|
+
}
|
|
69
|
+
function canPlaceOnTableau(dest, moving) {
|
|
70
|
+
if (moving.length === 0) return false;
|
|
71
|
+
const movingTop = moving[0];
|
|
72
|
+
const destTop = _chunkW5CPH6TDjs.top.call(void 0, dest);
|
|
73
|
+
if (destTop === void 0) {
|
|
74
|
+
return movingTop.rank === 13;
|
|
75
|
+
}
|
|
76
|
+
if (!destTop.faceUp) return false;
|
|
77
|
+
const destColor = _chunkW5CPH6TDjs.cardColor.call(void 0, destTop.suit);
|
|
78
|
+
const movingColor = _chunkW5CPH6TDjs.cardColor.call(void 0, movingTop.suit);
|
|
79
|
+
return movingTop.rank === destTop.rank - 1 && destColor !== movingColor;
|
|
80
|
+
}
|
|
81
|
+
function runLengthAtTop(column) {
|
|
82
|
+
if (column.length === 0) return 0;
|
|
83
|
+
if (!column[column.length - 1].faceUp) return 0;
|
|
84
|
+
let len = 1;
|
|
85
|
+
for (let i = column.length - 2; i >= 0; i--) {
|
|
86
|
+
const cur = column[i];
|
|
87
|
+
const next = column[i + 1];
|
|
88
|
+
if (!cur.faceUp) break;
|
|
89
|
+
if (next.rank !== cur.rank - 1 || _chunkW5CPH6TDjs.cardColor.call(void 0, next.suit) === _chunkW5CPH6TDjs.cardColor.call(void 0, cur.suit)) break;
|
|
90
|
+
len++;
|
|
91
|
+
}
|
|
92
|
+
return len;
|
|
93
|
+
}
|
|
94
|
+
function scoreHintMove(state, move) {
|
|
95
|
+
if (move.to.zone !== "tableau") return 0;
|
|
96
|
+
if (move.from.zone !== "tableau" && move.from.zone !== "waste") return 0;
|
|
97
|
+
const fromWaste = move.from.zone === "waste";
|
|
98
|
+
const fromPile = fromWaste ? state.waste : state.tableau[move.from.index];
|
|
99
|
+
const toCol = state.tableau[move.to.index];
|
|
100
|
+
if (!fromPile || !toCol || fromPile.length < move.count) return 0;
|
|
101
|
+
const remainder = fromPile.slice(0, fromPile.length - move.count);
|
|
102
|
+
const below = remainder[remainder.length - 1];
|
|
103
|
+
const base = fromPile[fromPile.length - move.count];
|
|
104
|
+
const linkedAtSource = !fromWaste && below !== void 0 && below.faceUp && below.rank === base.rank + 1 && _chunkW5CPH6TDjs.cardColor.call(void 0, below.suit) !== _chunkW5CPH6TDjs.cardColor.call(void 0, base.suit);
|
|
105
|
+
const a = linkedAtSource ? runLengthAtTop(remainder) : 0;
|
|
106
|
+
const b = runLengthAtTop(toCol);
|
|
107
|
+
const netRunStructureChange = Math.max(move.count + b, a) - Math.max(move.count + a, b);
|
|
108
|
+
const fromBecomesEmpty = !fromWaste && move.count === fromPile.length;
|
|
109
|
+
const toWasEmpty = toCol.length === 0;
|
|
110
|
+
const netEmptyDelta = (fromBecomesEmpty ? 1 : 0) - (toWasEmpty ? 1 : 0);
|
|
111
|
+
const revealsHiddenCard = !fromWaste && below !== void 0 && !below.faceUp;
|
|
112
|
+
let score = revealsHiddenCard ? 500 : 0;
|
|
113
|
+
if (fromWaste) score += 50;
|
|
114
|
+
score += netRunStructureChange * 30;
|
|
115
|
+
score += netEmptyDelta * 40;
|
|
116
|
+
if (score <= 0) return score;
|
|
117
|
+
return score + move.count * 2;
|
|
118
|
+
}
|
|
119
|
+
function searchMoves(state) {
|
|
120
|
+
const moves = [];
|
|
121
|
+
const toFoundation = (from, card) => {
|
|
122
|
+
const f = _chunkLSS44UKHjs.suitIndex.call(void 0, card.suit);
|
|
123
|
+
if (_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, state.foundation[f], card)) moves.push({ from, to: { zone: "foundation", index: f }, count: 1 });
|
|
124
|
+
};
|
|
125
|
+
const wasteTop = _chunkW5CPH6TDjs.top.call(void 0, state.waste);
|
|
126
|
+
if (wasteTop) {
|
|
127
|
+
const from = { zone: "waste", index: 0 };
|
|
128
|
+
toFoundation(from, wasteTop);
|
|
129
|
+
for (let t = 0; t < 7; t++) if (canPlaceOnTableau(state.tableau[t], [wasteTop])) moves.push({ from, to: { zone: "tableau", index: t }, count: 1 });
|
|
130
|
+
}
|
|
131
|
+
for (let col = 0; col < 7; col++) {
|
|
132
|
+
const pile = state.tableau[col];
|
|
133
|
+
const runLen = runLengthAtTop(pile);
|
|
134
|
+
if (runLen === 0) continue;
|
|
135
|
+
const from = { zone: "tableau", index: col };
|
|
136
|
+
toFoundation(from, pile[pile.length - 1]);
|
|
137
|
+
for (let count = 1; count <= runLen; count++) {
|
|
138
|
+
const moving = [pile[pile.length - count]];
|
|
139
|
+
for (let t = 0; t < 7; t++) if (t !== col && canPlaceOnTableau(state.tableau[t], moving)) moves.push({ from, to: { zone: "tableau", index: t }, count });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return moves;
|
|
143
|
+
}
|
|
144
|
+
function createKlondikeController(drawCount) {
|
|
145
|
+
function newGame(seed) {
|
|
146
|
+
const deck = _chunkW5CPH6TDjs.shuffledDeck.call(void 0, seed, 1);
|
|
147
|
+
let cursor = 0;
|
|
148
|
+
const tableau = [];
|
|
149
|
+
for (let col = 0; col < 7; col++) {
|
|
150
|
+
const count = col + 1;
|
|
151
|
+
const cards = [];
|
|
152
|
+
for (let i = 0; i < count; i++) {
|
|
153
|
+
const card = deck[cursor++];
|
|
154
|
+
const faceUp = i === count - 1;
|
|
155
|
+
cards.push({ ...card, faceUp });
|
|
156
|
+
}
|
|
157
|
+
tableau.push(cards);
|
|
158
|
+
}
|
|
159
|
+
const stock = deck.slice(cursor).map((c) => ({ ...c, faceUp: false }));
|
|
160
|
+
const foundation = [[], [], [], []];
|
|
161
|
+
return { tableau, foundation, stock, waste: [] };
|
|
162
|
+
}
|
|
163
|
+
function tryMove(state, move) {
|
|
164
|
+
const { from, to, count } = move;
|
|
165
|
+
if (count < 1) return null;
|
|
166
|
+
if (from.zone === to.zone && from.index === to.index) return null;
|
|
167
|
+
if (from.zone === "foundation" && (to.zone !== "tableau" || count !== 1)) return null;
|
|
168
|
+
const sourcePile = getPile(state, from);
|
|
169
|
+
if (!sourcePile || sourcePile.length < count) return null;
|
|
170
|
+
if (from.zone === "stock") return null;
|
|
171
|
+
if (count > 1 && from.zone !== "tableau") return null;
|
|
172
|
+
const movingCards = sourcePile.slice(sourcePile.length - count);
|
|
173
|
+
if (movingCards.some((c) => !c.faceUp)) return null;
|
|
174
|
+
if (from.zone === "tableau" && count > 1) {
|
|
175
|
+
if (!_chunkW5CPH6TDjs.isDescendingAltColor.call(void 0, movingCards)) return null;
|
|
176
|
+
}
|
|
177
|
+
const destPile = getPile(state, to);
|
|
178
|
+
if (!destPile) return null;
|
|
179
|
+
if (to.zone === "tableau") {
|
|
180
|
+
if (!canPlaceOnTableau(destPile, movingCards)) return null;
|
|
181
|
+
} else if (to.zone === "foundation") {
|
|
182
|
+
if (count !== 1) return null;
|
|
183
|
+
const movingCard = movingCards[0];
|
|
184
|
+
if (_chunkLSS44UKHjs.suitIndex.call(void 0, movingCard.suit) !== to.index) return null;
|
|
185
|
+
if (!_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, destPile, movingCard)) return null;
|
|
186
|
+
} else if (to.zone === "waste") {
|
|
187
|
+
return null;
|
|
188
|
+
} else {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
const next = cloneState(state);
|
|
192
|
+
const nextSourcePile = getPile(next, from).slice();
|
|
193
|
+
const nextDestPile = getPile(next, to).slice();
|
|
194
|
+
setPile(next, from, nextSourcePile);
|
|
195
|
+
setPile(next, to, nextDestPile);
|
|
196
|
+
const moved = nextSourcePile.splice(nextSourcePile.length - count, count);
|
|
197
|
+
nextDestPile.push(...moved);
|
|
198
|
+
if (from.zone === "tableau") {
|
|
199
|
+
flipFaceUp(nextSourcePile);
|
|
200
|
+
} else if (from.zone === "waste") {
|
|
201
|
+
}
|
|
202
|
+
return next;
|
|
203
|
+
}
|
|
204
|
+
function drawStock(state) {
|
|
205
|
+
if (state.stock.length > 0) {
|
|
206
|
+
const next = cloneState(state);
|
|
207
|
+
const nextStock = next.stock.slice();
|
|
208
|
+
const nextWaste = next.waste.slice();
|
|
209
|
+
const n = Math.min(drawCount, nextStock.length);
|
|
210
|
+
const drawn = nextStock.splice(nextStock.length - n, n);
|
|
211
|
+
for (const c of drawn) {
|
|
212
|
+
nextWaste.push(c.faceUp ? c : { ...c, faceUp: true });
|
|
213
|
+
}
|
|
214
|
+
setPile(next, { zone: "stock", index: 0 }, nextStock);
|
|
215
|
+
setPile(next, { zone: "waste", index: 0 }, nextWaste);
|
|
216
|
+
return next;
|
|
217
|
+
}
|
|
218
|
+
if (state.waste.length > 0) {
|
|
219
|
+
const next = cloneState(state);
|
|
220
|
+
const recycled = state.waste.slice().reverse().map((c) => ({ ...c, faceUp: false }));
|
|
221
|
+
setPile(next, { zone: "stock", index: 0 }, recycled);
|
|
222
|
+
setPile(next, { zone: "waste", index: 0 }, []);
|
|
223
|
+
return next;
|
|
224
|
+
}
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
function isWon(state) {
|
|
228
|
+
return state.foundation.every((pile) => {
|
|
229
|
+
const t = _chunkW5CPH6TDjs.top.call(void 0, pile);
|
|
230
|
+
return t !== void 0 && t.rank === 13;
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
function legalMoves(state) {
|
|
234
|
+
const moves = [];
|
|
235
|
+
const tryAdd = (from, to, count) => {
|
|
236
|
+
const result = tryMove(state, { from, to, count });
|
|
237
|
+
if (result) moves.push({ from, to, count });
|
|
238
|
+
};
|
|
239
|
+
if (state.waste.length > 0) {
|
|
240
|
+
const from = { zone: "waste", index: 0 };
|
|
241
|
+
for (let f = 0; f < 4; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
|
|
242
|
+
for (let t = 0; t < 7; t++) tryAdd(from, { zone: "tableau", index: t }, 1);
|
|
243
|
+
}
|
|
244
|
+
for (let col = 0; col < 7; col++) {
|
|
245
|
+
const pile = state.tableau[col];
|
|
246
|
+
if (pile.length === 0) continue;
|
|
247
|
+
let runStart = pile.length - 1;
|
|
248
|
+
while (runStart > 0 && pile[runStart - 1].faceUp) {
|
|
249
|
+
const candidate = pile.slice(runStart - 1);
|
|
250
|
+
if (_chunkW5CPH6TDjs.isDescendingAltColor.call(void 0, candidate)) {
|
|
251
|
+
runStart -= 1;
|
|
252
|
+
} else {
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const from = { zone: "tableau", index: col };
|
|
257
|
+
for (let f = 0; f < 4; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
|
|
258
|
+
for (let count = 1; count <= pile.length - runStart; count++) {
|
|
259
|
+
const movingCards = pile.slice(pile.length - count);
|
|
260
|
+
const candidates = state.tableau.map((destPile, index) => ({ index, pile: destPile })).filter((c) => c.index !== col).filter((c) => canPlaceOnTableau(c.pile, movingCards));
|
|
261
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.isDescendingAltColor);
|
|
262
|
+
if (best) {
|
|
263
|
+
moves.push({ from, to: { zone: "tableau", index: best.index }, count });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return moves;
|
|
268
|
+
}
|
|
269
|
+
function autoMoveHint(state, from, cardId) {
|
|
270
|
+
const pile = getPile(state, from);
|
|
271
|
+
if (!pile) return null;
|
|
272
|
+
const cardIndex = pile.findIndex((c) => c.id === cardId);
|
|
273
|
+
if (cardIndex === -1) return null;
|
|
274
|
+
const movingCards = pile.slice(cardIndex);
|
|
275
|
+
if (movingCards.some((c) => !c.faceUp)) return null;
|
|
276
|
+
const count = movingCards.length;
|
|
277
|
+
if (count > 1 && (from.zone !== "tableau" || !_chunkW5CPH6TDjs.isDescendingAltColor.call(void 0, movingCards))) return null;
|
|
278
|
+
if (count === 1) {
|
|
279
|
+
const movingCard = movingCards[0];
|
|
280
|
+
const f = _chunkLSS44UKHjs.suitIndex.call(void 0, movingCard.suit);
|
|
281
|
+
if (_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, state.foundation[f], movingCard)) {
|
|
282
|
+
return { from, to: { zone: "foundation", index: f }, count: 1 };
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
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));
|
|
286
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.isDescendingAltColor);
|
|
287
|
+
if (!best) return null;
|
|
288
|
+
return { from, to: { zone: "tableau", index: best.index }, count };
|
|
289
|
+
}
|
|
290
|
+
function accessibleCards(state) {
|
|
291
|
+
const result = [];
|
|
292
|
+
const wasteTop = _chunkW5CPH6TDjs.top.call(void 0, state.waste);
|
|
293
|
+
if (wasteTop) result.push({ ref: { zone: "waste", index: 0 }, card: wasteTop });
|
|
294
|
+
state.tableau.forEach((pile, index) => {
|
|
295
|
+
const t = _chunkW5CPH6TDjs.top.call(void 0, pile);
|
|
296
|
+
if (t && t.faceUp) result.push({ ref: { zone: "tableau", index }, card: t });
|
|
297
|
+
});
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
const progress = {
|
|
301
|
+
id: `klondike-draw${drawCount}`,
|
|
302
|
+
moves: searchMoves,
|
|
303
|
+
// A card may come back down off a foundation (see tryMove), occasionally the only way on.
|
|
304
|
+
rescueMoves: (state) => _chunkXV5OUPIWjs.foundationDownMoves.call(void 0, state.foundation, state.tableau, canPlaceOnTableau),
|
|
305
|
+
// The stock turns over and comes round again: drawing is only a way to reach its cards.
|
|
306
|
+
drawIsStep: true,
|
|
307
|
+
key: (state) => `${_chunkXV5OUPIWjs.columnsKey.call(void 0, state.tableau)}/${state.foundation.map((p) => p.length).join(",")}/${_chunkXV5OUPIWjs.pileKey.call(void 0, state.stock)}/${_chunkXV5OUPIWjs.pileKey.call(void 0, state.waste)}`,
|
|
308
|
+
progress: (state) => [-_chunkXV5OUPIWjs.faceDownCount.call(void 0, state.tableau), state.foundation.reduce((n, p) => n + p.length, 0)]
|
|
309
|
+
};
|
|
310
|
+
return {
|
|
311
|
+
id: "klondike",
|
|
312
|
+
title: "Klondike",
|
|
313
|
+
newGame,
|
|
314
|
+
legalMoves,
|
|
315
|
+
tryMove,
|
|
316
|
+
drawStock,
|
|
317
|
+
isWon,
|
|
318
|
+
autoMoveHint,
|
|
319
|
+
accessibleCards,
|
|
320
|
+
scoreHintMove,
|
|
321
|
+
progress
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function canAutoFinish(controller, state) {
|
|
325
|
+
if (state.stock.length > 0 || state.waste.length > 0) return false;
|
|
326
|
+
if (state.tableau.some((column) => column.some((c) => !c.faceUp))) return false;
|
|
327
|
+
return !controller.isWon(state) && _chunk22DC2YH6js.isFullyAutoCompletable.call(void 0, controller, state);
|
|
328
|
+
}
|
|
329
|
+
function nextFinishMove(controller, state) {
|
|
330
|
+
if (!canAutoFinish(controller, state)) return null;
|
|
331
|
+
const entry = _chunk22DC2YH6js.nextAutoCompleteMove.call(void 0, controller, state);
|
|
332
|
+
return entry ? { entry, to: { zone: "foundation", index: _chunkLSS44UKHjs.suitIndex.call(void 0, entry.card.suit) } } : null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
exports.createKlondikeController = createKlondikeController; exports.canAutoFinish = canAutoFinish; exports.nextFinishMove = nextFinishMove;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
top
|
|
3
|
+
} from "./chunk-XSS5M6C7.mjs";
|
|
4
|
+
|
|
5
|
+
// src/engine/foundation.ts
|
|
6
|
+
var FOUNDATION_SUITS = ["spades", "diamonds", "clubs", "hearts"];
|
|
7
|
+
var FOUNDATION_LAYOUT = 1;
|
|
8
|
+
function canPlaceOnFoundation(dest, moving) {
|
|
9
|
+
const destTop = top(dest);
|
|
10
|
+
if (destTop === void 0) return moving.rank === 1;
|
|
11
|
+
return destTop.suit === moving.suit && moving.rank === destTop.rank + 1;
|
|
12
|
+
}
|
|
13
|
+
function suitIndex(suit) {
|
|
14
|
+
return FOUNDATION_SUITS.indexOf(suit);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
FOUNDATION_SUITS,
|
|
19
|
+
FOUNDATION_LAYOUT,
|
|
20
|
+
canPlaceOnFoundation,
|
|
21
|
+
suitIndex
|
|
22
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkB56TRALOjs = require('./chunk-B56TRALO.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunkXV5OUPIWjs = require('./chunk-XV5OUPIW.js');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
var _chunkW5CPH6TDjs = require('./chunk-W5CPH6TD.js');
|
|
14
|
+
|
|
15
|
+
// src/games/scorpion/rules.ts
|
|
16
|
+
var NUM_COLUMNS = 7;
|
|
17
|
+
var FRONT_COLUMNS = 4;
|
|
18
|
+
var FACE_DOWN_PER_FRONT_COLUMN = 3;
|
|
19
|
+
function cloneState(state) {
|
|
20
|
+
return {
|
|
21
|
+
tableau: state.tableau.slice(),
|
|
22
|
+
stock: state.stock
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function setColumn(state, index, column) {
|
|
26
|
+
state.tableau[index] = column;
|
|
27
|
+
}
|
|
28
|
+
function canPlaceOnTableau(dest, moving) {
|
|
29
|
+
if (moving.length === 0) return false;
|
|
30
|
+
const movingBottom = moving[0];
|
|
31
|
+
const destTop = _chunkW5CPH6TDjs.top.call(void 0, dest);
|
|
32
|
+
if (destTop === void 0) return true;
|
|
33
|
+
if (!destTop.faceUp) return false;
|
|
34
|
+
return destTop.suit === movingBottom.suit && movingBottom.rank === destTop.rank - 1;
|
|
35
|
+
}
|
|
36
|
+
function computeLegalMoves(state) {
|
|
37
|
+
const moves = [];
|
|
38
|
+
for (let from = 0; from < NUM_COLUMNS; from++) {
|
|
39
|
+
const col = state.tableau[from];
|
|
40
|
+
for (let idx = 0; idx < col.length; idx++) {
|
|
41
|
+
if (!col[idx].faceUp) continue;
|
|
42
|
+
const group = col.slice(idx);
|
|
43
|
+
const count = group.length;
|
|
44
|
+
const candidates = state.tableau.map((destCol, index) => ({ index, pile: destCol })).filter((c) => c.index !== from).filter((c) => canPlaceOnTableau(c.pile, group));
|
|
45
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.alwaysValidRun);
|
|
46
|
+
if (best) {
|
|
47
|
+
moves.push({ from: { zone: "tableau", index: from }, to: { zone: "tableau", index: best.index }, count });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return moves;
|
|
52
|
+
}
|
|
53
|
+
function runLengthAtTop(column) {
|
|
54
|
+
if (column.length === 0) return 0;
|
|
55
|
+
if (!column[column.length - 1].faceUp) return 0;
|
|
56
|
+
let len = 1;
|
|
57
|
+
for (let i = column.length - 2; i >= 0; i--) {
|
|
58
|
+
const cur = column[i];
|
|
59
|
+
const next = column[i + 1];
|
|
60
|
+
if (!cur.faceUp || cur.suit !== next.suit || cur.rank !== next.rank + 1) break;
|
|
61
|
+
len++;
|
|
62
|
+
}
|
|
63
|
+
return len;
|
|
64
|
+
}
|
|
65
|
+
function scoreHintMove(state, move) {
|
|
66
|
+
if (move.from.zone !== "tableau" || move.to.zone !== "tableau") return 0;
|
|
67
|
+
const fromCol = state.tableau[move.from.index];
|
|
68
|
+
const toCol = state.tableau[move.to.index];
|
|
69
|
+
if (!fromCol || !toCol) return 0;
|
|
70
|
+
const group = fromCol.slice(fromCol.length - move.count);
|
|
71
|
+
const merged = [...toCol, ...group];
|
|
72
|
+
const relocatesWholeColumnToEmpty = toCol.length === 0 && move.count === fromCol.length;
|
|
73
|
+
if (merged.length === 13 && !relocatesWholeColumnToEmpty) {
|
|
74
|
+
if (merged[0].rank === 13 && merged.every((c) => c.faceUp) && _chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, merged)) return 1e3;
|
|
75
|
+
}
|
|
76
|
+
const runBefore = Math.max(runLengthAtTop(fromCol), runLengthAtTop(toCol));
|
|
77
|
+
const newRunLenAtDest = runLengthAtTop(merged);
|
|
78
|
+
const remainder = fromCol.slice(0, fromCol.length - move.count);
|
|
79
|
+
const remainderRunLen = runLengthAtTop(remainder);
|
|
80
|
+
const netRunStructureChange = Math.max(newRunLenAtDest, remainderRunLen) - runBefore;
|
|
81
|
+
const fromBecomesEmpty = move.count === fromCol.length;
|
|
82
|
+
const toWasEmpty = toCol.length === 0;
|
|
83
|
+
const netEmptyDelta = (fromBecomesEmpty ? 1 : 0) - (toWasEmpty ? 1 : 0);
|
|
84
|
+
const newFromTop = fromCol[fromCol.length - move.count - 1];
|
|
85
|
+
const revealsHiddenCard = newFromTop !== void 0 && !newFromTop.faceUp;
|
|
86
|
+
let score = revealsHiddenCard ? 500 : 0;
|
|
87
|
+
score += netRunStructureChange * 30;
|
|
88
|
+
score += netEmptyDelta * 40;
|
|
89
|
+
if (score <= 0) return score;
|
|
90
|
+
return score + move.count * 2;
|
|
91
|
+
}
|
|
92
|
+
function searchMoves(state) {
|
|
93
|
+
const moves = [];
|
|
94
|
+
for (let col = 0; col < NUM_COLUMNS; col++) {
|
|
95
|
+
const pile = state.tableau[col];
|
|
96
|
+
for (let i = 0; i < pile.length; i++) {
|
|
97
|
+
if (!pile[i].faceUp) continue;
|
|
98
|
+
const group = [pile[i]];
|
|
99
|
+
for (let t = 0; t < NUM_COLUMNS; t++) if (t !== col && canPlaceOnTableau(state.tableau[t], group)) moves.push({ from: { zone: "tableau", index: col }, to: { zone: "tableau", index: t }, count: pile.length - i });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return moves;
|
|
103
|
+
}
|
|
104
|
+
function createScorpionController(assistedStock) {
|
|
105
|
+
function newGame(seed) {
|
|
106
|
+
const deck = _chunkW5CPH6TDjs.shuffledDeck.call(void 0, seed, 1);
|
|
107
|
+
let cursor = 0;
|
|
108
|
+
const tableau = [];
|
|
109
|
+
for (let col = 0; col < FRONT_COLUMNS; col++) {
|
|
110
|
+
const cards = [];
|
|
111
|
+
for (let i = 0; i < 7; i++) {
|
|
112
|
+
const card = deck[cursor++];
|
|
113
|
+
cards.push({ ...card, faceUp: i >= FACE_DOWN_PER_FRONT_COLUMN });
|
|
114
|
+
}
|
|
115
|
+
tableau.push(cards);
|
|
116
|
+
}
|
|
117
|
+
const backColumnCount = assistedStock ? 7 : 8;
|
|
118
|
+
for (let col = FRONT_COLUMNS; col < NUM_COLUMNS; col++) {
|
|
119
|
+
const cards = [];
|
|
120
|
+
for (let i = 0; i < backColumnCount; i++) {
|
|
121
|
+
const card = deck[cursor++];
|
|
122
|
+
cards.push({ ...card, faceUp: true });
|
|
123
|
+
}
|
|
124
|
+
tableau.push(cards);
|
|
125
|
+
}
|
|
126
|
+
const stock = assistedStock ? deck.slice(cursor).map((c) => ({ ...c, faceUp: false })) : [];
|
|
127
|
+
return { tableau, stock };
|
|
128
|
+
}
|
|
129
|
+
function tryMove(state, move) {
|
|
130
|
+
const { from, to, count } = move;
|
|
131
|
+
if (count < 1) return null;
|
|
132
|
+
if (from.zone !== "tableau" || to.zone !== "tableau") return null;
|
|
133
|
+
if (from.index === to.index) return null;
|
|
134
|
+
const fromCol = state.tableau[from.index];
|
|
135
|
+
const toCol = state.tableau[to.index];
|
|
136
|
+
if (!fromCol || !toCol) return null;
|
|
137
|
+
if (count > fromCol.length) return null;
|
|
138
|
+
const group = fromCol.slice(fromCol.length - count);
|
|
139
|
+
if (!group.every((c) => c.faceUp)) return null;
|
|
140
|
+
if (!canPlaceOnTableau(toCol, group)) return null;
|
|
141
|
+
const next = cloneState(state);
|
|
142
|
+
const nextFrom = fromCol.slice();
|
|
143
|
+
const nextTo = toCol.slice();
|
|
144
|
+
const moved = nextFrom.splice(nextFrom.length - count, count);
|
|
145
|
+
nextTo.push(...moved);
|
|
146
|
+
const newTop = nextFrom[nextFrom.length - 1];
|
|
147
|
+
if (newTop && !newTop.faceUp) nextFrom[nextFrom.length - 1] = { ...newTop, faceUp: true };
|
|
148
|
+
setColumn(next, from.index, nextFrom);
|
|
149
|
+
setColumn(next, to.index, nextTo);
|
|
150
|
+
return next;
|
|
151
|
+
}
|
|
152
|
+
function drawStock(state) {
|
|
153
|
+
if (state.stock.length === 0) return null;
|
|
154
|
+
const next = cloneState(state);
|
|
155
|
+
for (let i = 0; i < next.stock.length; i++) {
|
|
156
|
+
const col = next.tableau[FRONT_COLUMNS + i].slice();
|
|
157
|
+
col.push({ ...next.stock[i], faceUp: true });
|
|
158
|
+
setColumn(next, FRONT_COLUMNS + i, col);
|
|
159
|
+
}
|
|
160
|
+
next.stock = [];
|
|
161
|
+
return next;
|
|
162
|
+
}
|
|
163
|
+
function isWon(state) {
|
|
164
|
+
return state.tableau.every((col) => col.length === 0 || col.length === 13 && _chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, col));
|
|
165
|
+
}
|
|
166
|
+
function legalMoves(state) {
|
|
167
|
+
return computeLegalMoves(state);
|
|
168
|
+
}
|
|
169
|
+
function isStuck(state) {
|
|
170
|
+
if (isWon(state)) return false;
|
|
171
|
+
if (state.stock.length > 0) return false;
|
|
172
|
+
return computeLegalMoves(state).length === 0;
|
|
173
|
+
}
|
|
174
|
+
function autoMoveHint(state, from, cardId) {
|
|
175
|
+
if (from.zone !== "tableau") return null;
|
|
176
|
+
const col = state.tableau[from.index];
|
|
177
|
+
if (!col) return null;
|
|
178
|
+
const cardIndex = col.findIndex((c) => c.id === cardId);
|
|
179
|
+
if (cardIndex === -1) return null;
|
|
180
|
+
const movingCards = col.slice(cardIndex);
|
|
181
|
+
if (movingCards.some((c) => !c.faceUp)) return null;
|
|
182
|
+
const count = movingCards.length;
|
|
183
|
+
const candidates = state.tableau.map((destCol, index) => ({ index, pile: destCol })).filter((c) => c.index !== from.index).filter((c) => canPlaceOnTableau(c.pile, movingCards));
|
|
184
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.alwaysValidRun);
|
|
185
|
+
if (!best) return null;
|
|
186
|
+
return { from, to: { zone: "tableau", index: best.index }, count };
|
|
187
|
+
}
|
|
188
|
+
const progress = {
|
|
189
|
+
id: assistedStock ? "scorpion" : "scorpion-classic",
|
|
190
|
+
moves: searchMoves,
|
|
191
|
+
drawIsStep: false,
|
|
192
|
+
key: (state) => `${_chunkXV5OUPIWjs.columnsKey.call(void 0, state.tableau)}/${state.stock.length}`,
|
|
193
|
+
progress: (state) => [-_chunkXV5OUPIWjs.faceDownCount.call(void 0, state.tableau), state.tableau.filter((col) => col.length === 13 && _chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, col)).length]
|
|
194
|
+
};
|
|
195
|
+
return {
|
|
196
|
+
id: "scorpion",
|
|
197
|
+
title: "Scorpion",
|
|
198
|
+
newGame,
|
|
199
|
+
legalMoves,
|
|
200
|
+
tryMove,
|
|
201
|
+
drawStock,
|
|
202
|
+
isWon,
|
|
203
|
+
isStuck,
|
|
204
|
+
autoMoveHint,
|
|
205
|
+
scoreHintMove,
|
|
206
|
+
progress
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
exports.createScorpionController = createScorpionController;
|