@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,348 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isFullyAutoCompletable,
|
|
3
|
+
nextAutoCompleteMove
|
|
4
|
+
} from "./chunk-3BMUGHKQ.mjs";
|
|
5
|
+
import {
|
|
6
|
+
FOUNDATION_SUITS
|
|
7
|
+
} from "./chunk-SDZPP3HI.mjs";
|
|
8
|
+
import {
|
|
9
|
+
columnsKey,
|
|
10
|
+
foundationDownMoves,
|
|
11
|
+
pileKey
|
|
12
|
+
} from "./chunk-FDHHAPZA.mjs";
|
|
13
|
+
import {
|
|
14
|
+
SUITS,
|
|
15
|
+
cardColor,
|
|
16
|
+
shuffledDeck,
|
|
17
|
+
top
|
|
18
|
+
} from "./chunk-XSS5M6C7.mjs";
|
|
19
|
+
|
|
20
|
+
// src/games/canfield/rules.ts
|
|
21
|
+
function suitIndex(suit) {
|
|
22
|
+
return FOUNDATION_SUITS.indexOf(suit);
|
|
23
|
+
}
|
|
24
|
+
function cyclicPrevRank(rank) {
|
|
25
|
+
return rank === 1 ? 13 : rank - 1;
|
|
26
|
+
}
|
|
27
|
+
function cyclicNextRank(rank) {
|
|
28
|
+
return rank === 13 ? 1 : rank + 1;
|
|
29
|
+
}
|
|
30
|
+
function isDescendingAltColorCyclic(cards) {
|
|
31
|
+
for (let i = 1; i < cards.length; i++) {
|
|
32
|
+
const prev = cards[i - 1];
|
|
33
|
+
const cur = cards[i];
|
|
34
|
+
if (cur.rank !== cyclicPrevRank(prev.rank)) return false;
|
|
35
|
+
if (cardColor(cur.suit) === cardColor(prev.suit)) return false;
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
function canPlaceOnFoundationCyclic(dest, moving, foundationRank) {
|
|
40
|
+
const destTop = top(dest);
|
|
41
|
+
if (destTop === void 0) return moving.rank === foundationRank;
|
|
42
|
+
return destTop.suit === moving.suit && moving.rank === cyclicNextRank(destTop.rank);
|
|
43
|
+
}
|
|
44
|
+
function canPlaceOnTableau(reserve, dest, moving, fromZone) {
|
|
45
|
+
if (moving.length === 0) return false;
|
|
46
|
+
const movingTop = moving[0];
|
|
47
|
+
const destTop = top(dest);
|
|
48
|
+
if (destTop === void 0) {
|
|
49
|
+
return reserve.length > 0 ? fromZone === "reserve" : true;
|
|
50
|
+
}
|
|
51
|
+
if (!destTop.faceUp) return false;
|
|
52
|
+
return cardColor(movingTop.suit) !== cardColor(destTop.suit) && movingTop.rank === cyclicPrevRank(destTop.rank);
|
|
53
|
+
}
|
|
54
|
+
function runLengthAtTop(column) {
|
|
55
|
+
if (column.length === 0) return 0;
|
|
56
|
+
let runStart = column.length - 1;
|
|
57
|
+
while (runStart > 0 && column[runStart - 1].faceUp) {
|
|
58
|
+
if (!isDescendingAltColorCyclic(column.slice(runStart - 1))) break;
|
|
59
|
+
runStart -= 1;
|
|
60
|
+
}
|
|
61
|
+
return column.length - runStart;
|
|
62
|
+
}
|
|
63
|
+
function cloneState(state) {
|
|
64
|
+
return {
|
|
65
|
+
tableau: state.tableau.slice(),
|
|
66
|
+
foundation: state.foundation.slice(),
|
|
67
|
+
foundationRank: state.foundationRank,
|
|
68
|
+
reserve: state.reserve,
|
|
69
|
+
stock: state.stock,
|
|
70
|
+
waste: state.waste
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function getPile(state, ref) {
|
|
74
|
+
switch (ref.zone) {
|
|
75
|
+
case "tableau":
|
|
76
|
+
return state.tableau[ref.index] ?? null;
|
|
77
|
+
case "foundation":
|
|
78
|
+
return state.foundation[ref.index] ?? null;
|
|
79
|
+
case "reserve":
|
|
80
|
+
return state.reserve;
|
|
81
|
+
case "waste":
|
|
82
|
+
return state.waste;
|
|
83
|
+
default:
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function setPile(state, ref, pile) {
|
|
88
|
+
switch (ref.zone) {
|
|
89
|
+
case "tableau":
|
|
90
|
+
state.tableau[ref.index] = pile;
|
|
91
|
+
break;
|
|
92
|
+
case "foundation":
|
|
93
|
+
state.foundation[ref.index] = pile;
|
|
94
|
+
break;
|
|
95
|
+
case "reserve":
|
|
96
|
+
state.reserve = pile;
|
|
97
|
+
break;
|
|
98
|
+
case "waste":
|
|
99
|
+
state.waste = pile;
|
|
100
|
+
break;
|
|
101
|
+
case "stock":
|
|
102
|
+
state.stock = pile;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function flipFaceUp(pile) {
|
|
107
|
+
const i = pile.length - 1;
|
|
108
|
+
const c = pile[i];
|
|
109
|
+
if (c && !c.faceUp) pile[i] = { ...c, faceUp: true };
|
|
110
|
+
}
|
|
111
|
+
function newGame(seed) {
|
|
112
|
+
const deck = shuffledDeck(seed, 1);
|
|
113
|
+
let cursor = 0;
|
|
114
|
+
const starter = deck[cursor++];
|
|
115
|
+
const foundation = [[], [], [], []];
|
|
116
|
+
foundation[suitIndex(starter.suit)] = [{ ...starter, faceUp: true }];
|
|
117
|
+
const tableau = [];
|
|
118
|
+
for (let col = 0; col < 4; col++) {
|
|
119
|
+
tableau.push([{ ...deck[cursor++], faceUp: true }]);
|
|
120
|
+
}
|
|
121
|
+
const reserve = [];
|
|
122
|
+
for (let i = 0; i < 13; i++) {
|
|
123
|
+
reserve.push({ ...deck[cursor++], faceUp: i === 12 });
|
|
124
|
+
}
|
|
125
|
+
const stock = deck.slice(cursor).map((c) => ({ ...c, faceUp: false }));
|
|
126
|
+
return { tableau, foundation, foundationRank: starter.rank, reserve, stock, waste: [] };
|
|
127
|
+
}
|
|
128
|
+
function tryMove(state, move) {
|
|
129
|
+
const { from, to, count } = move;
|
|
130
|
+
if (count < 1) return null;
|
|
131
|
+
if (from.zone === to.zone && from.index === to.index) return null;
|
|
132
|
+
if (from.zone === "foundation" && (to.zone !== "tableau" || count !== 1)) return null;
|
|
133
|
+
if (from.zone === "stock") return null;
|
|
134
|
+
const sourcePile = getPile(state, from);
|
|
135
|
+
if (!sourcePile || sourcePile.length < count) return null;
|
|
136
|
+
if (count > 1 && from.zone !== "tableau") return null;
|
|
137
|
+
const movingCards = sourcePile.slice(sourcePile.length - count);
|
|
138
|
+
if (movingCards.some((c) => !c.faceUp)) return null;
|
|
139
|
+
if (from.zone === "tableau" && count > 1) {
|
|
140
|
+
if (!isDescendingAltColorCyclic(movingCards)) return null;
|
|
141
|
+
}
|
|
142
|
+
const destPile = getPile(state, to);
|
|
143
|
+
if (!destPile) return null;
|
|
144
|
+
if (to.zone === "tableau") {
|
|
145
|
+
if (!canPlaceOnTableau(state.reserve, destPile, movingCards, from.zone)) return null;
|
|
146
|
+
} else if (to.zone === "foundation") {
|
|
147
|
+
if (count !== 1) return null;
|
|
148
|
+
const movingCard = movingCards[0];
|
|
149
|
+
if (suitIndex(movingCard.suit) !== to.index) return null;
|
|
150
|
+
if (!canPlaceOnFoundationCyclic(destPile, movingCard, state.foundationRank)) return null;
|
|
151
|
+
} else {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
const next = cloneState(state);
|
|
155
|
+
const nextSourcePile = getPile(next, from).slice();
|
|
156
|
+
const nextDestPile = getPile(next, to).slice();
|
|
157
|
+
setPile(next, from, nextSourcePile);
|
|
158
|
+
setPile(next, to, nextDestPile);
|
|
159
|
+
const moved = nextSourcePile.splice(nextSourcePile.length - count, count);
|
|
160
|
+
nextDestPile.push(...moved);
|
|
161
|
+
if (from.zone === "reserve") {
|
|
162
|
+
flipFaceUp(nextSourcePile);
|
|
163
|
+
}
|
|
164
|
+
return next;
|
|
165
|
+
}
|
|
166
|
+
function makeDrawStock(drawCount) {
|
|
167
|
+
return function drawStock(state) {
|
|
168
|
+
if (state.stock.length > 0) {
|
|
169
|
+
const next = cloneState(state);
|
|
170
|
+
const nextStock = next.stock.slice();
|
|
171
|
+
const nextWaste = next.waste.slice();
|
|
172
|
+
const n = Math.min(drawCount, nextStock.length);
|
|
173
|
+
const drawn = nextStock.splice(nextStock.length - n, n);
|
|
174
|
+
for (const c of drawn) {
|
|
175
|
+
nextWaste.push(c.faceUp ? c : { ...c, faceUp: true });
|
|
176
|
+
}
|
|
177
|
+
setPile(next, { zone: "stock", index: 0 }, nextStock);
|
|
178
|
+
setPile(next, { zone: "waste", index: 0 }, nextWaste);
|
|
179
|
+
return next;
|
|
180
|
+
}
|
|
181
|
+
if (state.waste.length > 0) {
|
|
182
|
+
const next = cloneState(state);
|
|
183
|
+
const recycled = state.waste.slice().reverse().map((c) => ({ ...c, faceUp: false }));
|
|
184
|
+
setPile(next, { zone: "stock", index: 0 }, recycled);
|
|
185
|
+
setPile(next, { zone: "waste", index: 0 }, []);
|
|
186
|
+
return next;
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function isWon(state) {
|
|
192
|
+
return state.foundation.every((pile) => pile.length === 13);
|
|
193
|
+
}
|
|
194
|
+
function legalMoves(state) {
|
|
195
|
+
const moves = [];
|
|
196
|
+
const tryAdd = (from, to, count) => {
|
|
197
|
+
if (tryMove(state, { from, to, count })) moves.push({ from, to, count });
|
|
198
|
+
};
|
|
199
|
+
if (state.waste.length > 0) {
|
|
200
|
+
const from = { zone: "waste", index: 0 };
|
|
201
|
+
for (let f = 0; f < 4; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
|
|
202
|
+
for (let t = 0; t < 4; t++) tryAdd(from, { zone: "tableau", index: t }, 1);
|
|
203
|
+
}
|
|
204
|
+
if (state.reserve.length > 0) {
|
|
205
|
+
const from = { zone: "reserve", index: 0 };
|
|
206
|
+
for (let f = 0; f < 4; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
|
|
207
|
+
for (let t = 0; t < 4; t++) tryAdd(from, { zone: "tableau", index: t }, 1);
|
|
208
|
+
}
|
|
209
|
+
for (let col = 0; col < 4; col++) {
|
|
210
|
+
const pile = state.tableau[col];
|
|
211
|
+
if (pile.length === 0) continue;
|
|
212
|
+
const runLen = runLengthAtTop(pile);
|
|
213
|
+
const from = { zone: "tableau", index: col };
|
|
214
|
+
for (let f = 0; f < 4; f++) tryAdd(from, { zone: "foundation", index: f }, 1);
|
|
215
|
+
for (let count = 1; count <= runLen; count++) {
|
|
216
|
+
for (let t = 0; t < 4; t++) {
|
|
217
|
+
if (t === col) continue;
|
|
218
|
+
tryAdd(from, { zone: "tableau", index: t }, count);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return moves;
|
|
223
|
+
}
|
|
224
|
+
function isStuck(state) {
|
|
225
|
+
return state.stock.length === 0 && state.waste.length === 0 && legalMoves(state).length === 0;
|
|
226
|
+
}
|
|
227
|
+
function autoMoveHint(state, from, cardId) {
|
|
228
|
+
const pile = getPile(state, from);
|
|
229
|
+
if (!pile) return null;
|
|
230
|
+
const cardIndex = pile.findIndex((c) => c.id === cardId);
|
|
231
|
+
if (cardIndex === -1) return null;
|
|
232
|
+
const movingCards = pile.slice(cardIndex);
|
|
233
|
+
if (movingCards.some((c) => !c.faceUp)) return null;
|
|
234
|
+
const count = movingCards.length;
|
|
235
|
+
if (count > 1 && (from.zone !== "tableau" || !isDescendingAltColorCyclic(movingCards))) return null;
|
|
236
|
+
if (count === 1) {
|
|
237
|
+
const movingCard = movingCards[0];
|
|
238
|
+
const f = suitIndex(movingCard.suit);
|
|
239
|
+
if (canPlaceOnFoundationCyclic(state.foundation[f], movingCard, state.foundationRank)) {
|
|
240
|
+
return { from, to: { zone: "foundation", index: f }, count: 1 };
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
for (let t = 0; t < 4; t++) {
|
|
244
|
+
if (from.zone === "tableau" && from.index === t) continue;
|
|
245
|
+
if (canPlaceOnTableau(state.reserve, state.tableau[t], movingCards, from.zone)) {
|
|
246
|
+
return { from, to: { zone: "tableau", index: t }, count };
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
function scoreHintMove(state, move) {
|
|
252
|
+
if (move.to.zone !== "tableau") return 0;
|
|
253
|
+
const toCol = state.tableau[move.to.index];
|
|
254
|
+
if (!toCol) return 0;
|
|
255
|
+
const fromPile = getPile(state, move.from);
|
|
256
|
+
if (!fromPile || fromPile.length < move.count) return 0;
|
|
257
|
+
const fromZone = move.from.zone;
|
|
258
|
+
const remainder = fromPile.slice(0, fromPile.length - move.count);
|
|
259
|
+
const below = remainder[remainder.length - 1];
|
|
260
|
+
const base = fromPile[fromPile.length - move.count];
|
|
261
|
+
const linkedAtSource = fromZone === "tableau" && below !== void 0 && below.faceUp && isDescendingAltColorCyclic([below, base]);
|
|
262
|
+
const a = linkedAtSource ? runLengthAtTop(remainder) : 0;
|
|
263
|
+
const b = runLengthAtTop(toCol);
|
|
264
|
+
const netRunStructureChange = Math.max(move.count + b, a) - Math.max(move.count + a, b);
|
|
265
|
+
const fromBecomesEmpty = fromZone === "tableau" && move.count === fromPile.length;
|
|
266
|
+
const toWasEmpty = toCol.length === 0;
|
|
267
|
+
const netEmptyDelta = (fromBecomesEmpty ? 1 : 0) - (toWasEmpty ? 1 : 0);
|
|
268
|
+
const depletesReserve = fromZone === "reserve";
|
|
269
|
+
let score = depletesReserve ? 500 : 0;
|
|
270
|
+
score += netRunStructureChange * 30;
|
|
271
|
+
score += netEmptyDelta * 40;
|
|
272
|
+
if (score <= 0) return score;
|
|
273
|
+
return score + move.count * 2;
|
|
274
|
+
}
|
|
275
|
+
function accessibleCards(state) {
|
|
276
|
+
const result = [];
|
|
277
|
+
const wasteTop = top(state.waste);
|
|
278
|
+
if (wasteTop) result.push({ ref: { zone: "waste", index: 0 }, card: wasteTop });
|
|
279
|
+
const reserveTop = top(state.reserve);
|
|
280
|
+
if (reserveTop) result.push({ ref: { zone: "reserve", index: 0 }, card: reserveTop });
|
|
281
|
+
state.tableau.forEach((pile, index) => {
|
|
282
|
+
const t = top(pile);
|
|
283
|
+
if (t && t.faceUp) result.push({ ref: { zone: "tableau", index }, card: t });
|
|
284
|
+
});
|
|
285
|
+
return result;
|
|
286
|
+
}
|
|
287
|
+
function cyclicPosition(rank, foundationRank) {
|
|
288
|
+
return (rank - foundationRank + 13) % 13;
|
|
289
|
+
}
|
|
290
|
+
function isSafeAutoPlay(state, card) {
|
|
291
|
+
const oppositeColor = cardColor(card.suit) === "red" ? "black" : "red";
|
|
292
|
+
const oppositeSuits = SUITS.filter((s) => cardColor(s) === oppositeColor);
|
|
293
|
+
const oppMinLength = Math.min(...oppositeSuits.map((s) => state.foundation[suitIndex(s)].length));
|
|
294
|
+
return oppMinLength >= cyclicPosition(card.rank, state.foundationRank);
|
|
295
|
+
}
|
|
296
|
+
function findSafeAutoPlay(state, accessible) {
|
|
297
|
+
for (const entry of accessible) {
|
|
298
|
+
const f = suitIndex(entry.card.suit);
|
|
299
|
+
if (canPlaceOnFoundationCyclic(state.foundation[f], entry.card, state.foundationRank) && isSafeAutoPlay(state, entry.card)) {
|
|
300
|
+
return entry;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
function canAutoFinish(controller, state) {
|
|
306
|
+
if (controller.isWon(state)) return false;
|
|
307
|
+
if (state.reserve.length > 0 || state.stock.length > 0 || state.waste.length > 0) return false;
|
|
308
|
+
if (state.tableau.some((column) => column.some((c) => !c.faceUp))) return false;
|
|
309
|
+
return isFullyAutoCompletable(controller, state);
|
|
310
|
+
}
|
|
311
|
+
function nextFinishMove(controller, state) {
|
|
312
|
+
if (!canAutoFinish(controller, state)) return null;
|
|
313
|
+
const entry = nextAutoCompleteMove(controller, state);
|
|
314
|
+
return entry ? { entry, to: { zone: "foundation", index: suitIndex(entry.card.suit) } } : null;
|
|
315
|
+
}
|
|
316
|
+
function createCanfieldController(drawCount) {
|
|
317
|
+
const progress = {
|
|
318
|
+
id: `canfield-draw${drawCount}`,
|
|
319
|
+
moves: legalMoves,
|
|
320
|
+
rescueMoves: (state) => foundationDownMoves(state.foundation, state.tableau, (column, moving) => canPlaceOnTableau(state.reserve, column, moving, "foundation")),
|
|
321
|
+
drawIsStep: true,
|
|
322
|
+
key: (state) => `${columnsKey(state.tableau)}/${pileKey(state.reserve)}/${pileKey(state.stock)}/${pileKey(state.waste)}`,
|
|
323
|
+
progress: (state) => [state.foundation.reduce((n, p) => n + p.length, 0), -state.reserve.length]
|
|
324
|
+
};
|
|
325
|
+
return {
|
|
326
|
+
id: "canfield",
|
|
327
|
+
title: "Canfield",
|
|
328
|
+
newGame,
|
|
329
|
+
legalMoves,
|
|
330
|
+
tryMove,
|
|
331
|
+
drawStock: makeDrawStock(drawCount),
|
|
332
|
+
isWon,
|
|
333
|
+
isStuck,
|
|
334
|
+
autoMoveHint,
|
|
335
|
+
scoreHintMove,
|
|
336
|
+
accessibleCards,
|
|
337
|
+
progress
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export {
|
|
342
|
+
isDescendingAltColorCyclic,
|
|
343
|
+
isSafeAutoPlay,
|
|
344
|
+
findSafeAutoPlay,
|
|
345
|
+
canAutoFinish,
|
|
346
|
+
nextFinishMove,
|
|
347
|
+
createCanfieldController
|
|
348
|
+
};
|
|
@@ -0,0 +1,249 @@
|
|
|
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/spider/rules.ts
|
|
16
|
+
var NUM_COLUMNS = 10;
|
|
17
|
+
var DEAL_COUNTS = [6, 6, 6, 6, 5, 5, 5, 5, 5, 5];
|
|
18
|
+
function cloneState(state) {
|
|
19
|
+
return {
|
|
20
|
+
tableau: state.tableau.slice(),
|
|
21
|
+
stock: state.stock,
|
|
22
|
+
completed: state.completed,
|
|
23
|
+
completedRuns: state.completedRuns
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function resolveCompletedSequence(tableau, colIndex) {
|
|
27
|
+
const col = tableau[colIndex];
|
|
28
|
+
if (col.length < 13) return null;
|
|
29
|
+
const topRun = col.slice(col.length - 13);
|
|
30
|
+
if (topRun[0].rank !== 13) return null;
|
|
31
|
+
if (!topRun.every((c2) => c2.faceUp)) return null;
|
|
32
|
+
if (!_chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, topRun)) return null;
|
|
33
|
+
const remaining = col.slice(0, col.length - 13);
|
|
34
|
+
const i = remaining.length - 1;
|
|
35
|
+
const c = remaining[i];
|
|
36
|
+
if (c && !c.faceUp) remaining[i] = { ...c, faceUp: true };
|
|
37
|
+
tableau[colIndex] = remaining;
|
|
38
|
+
return { king: topRun[0], run: topRun };
|
|
39
|
+
}
|
|
40
|
+
function computeLegalMoves(state) {
|
|
41
|
+
const moves = [];
|
|
42
|
+
for (let from = 0; from < NUM_COLUMNS; from++) {
|
|
43
|
+
const col = state.tableau[from];
|
|
44
|
+
if (col.length === 0) continue;
|
|
45
|
+
let runLen = 1;
|
|
46
|
+
for (let i = col.length - 1; i > 0; i--) {
|
|
47
|
+
const cur = col[i];
|
|
48
|
+
const prev = col[i - 1];
|
|
49
|
+
if (!cur.faceUp || !prev.faceUp) break;
|
|
50
|
+
if (prev.rank !== cur.rank + 1 || prev.suit !== cur.suit) break;
|
|
51
|
+
runLen++;
|
|
52
|
+
}
|
|
53
|
+
for (let count = 1; count <= runLen; count++) {
|
|
54
|
+
const group = col.slice(col.length - count);
|
|
55
|
+
if (!_chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, group)) continue;
|
|
56
|
+
const movingBottom = group[0];
|
|
57
|
+
const candidates = state.tableau.map((pile, index) => ({ index, pile })).filter((c) => c.index !== from).filter((c) => {
|
|
58
|
+
const destTop = _chunkW5CPH6TDjs.top.call(void 0, c.pile);
|
|
59
|
+
return destTop === void 0 || destTop.faceUp && destTop.rank === movingBottom.rank + 1;
|
|
60
|
+
});
|
|
61
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.isDescendingSameSuit);
|
|
62
|
+
if (best) {
|
|
63
|
+
moves.push({ from: { zone: "tableau", index: from }, to: { zone: "tableau", index: best.index }, count });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return moves;
|
|
68
|
+
}
|
|
69
|
+
function runLengthAtTop(column) {
|
|
70
|
+
if (column.length === 0) return 0;
|
|
71
|
+
if (!column[column.length - 1].faceUp) return 0;
|
|
72
|
+
let len = 1;
|
|
73
|
+
for (let i = column.length - 2; i >= 0; i--) {
|
|
74
|
+
const cur = column[i];
|
|
75
|
+
const next = column[i + 1];
|
|
76
|
+
if (!cur.faceUp || cur.suit !== next.suit || cur.rank !== next.rank + 1) break;
|
|
77
|
+
len++;
|
|
78
|
+
}
|
|
79
|
+
return len;
|
|
80
|
+
}
|
|
81
|
+
function scoreHintMove(state, move) {
|
|
82
|
+
if (move.from.zone !== "tableau" || move.to.zone !== "tableau") return 0;
|
|
83
|
+
const fromCol = state.tableau[move.from.index];
|
|
84
|
+
const toCol = state.tableau[move.to.index];
|
|
85
|
+
if (!fromCol || !toCol) return 0;
|
|
86
|
+
const group = fromCol.slice(fromCol.length - move.count);
|
|
87
|
+
const merged = [...toCol, ...group];
|
|
88
|
+
if (merged.length >= 13) {
|
|
89
|
+
const topRun = merged.slice(merged.length - 13);
|
|
90
|
+
if (topRun[0].rank === 13 && topRun.every((c) => c.faceUp) && _chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, topRun)) return 1e3;
|
|
91
|
+
}
|
|
92
|
+
const remainder = fromCol.slice(0, fromCol.length - move.count);
|
|
93
|
+
const below = remainder[remainder.length - 1];
|
|
94
|
+
const base = group[0];
|
|
95
|
+
const linkedAtSource = below !== void 0 && below.faceUp && below.suit === base.suit && below.rank === base.rank + 1;
|
|
96
|
+
const a = linkedAtSource ? runLengthAtTop(remainder) : 0;
|
|
97
|
+
const destTop = toCol[toCol.length - 1];
|
|
98
|
+
const b = destTop !== void 0 && destTop.suit === base.suit ? runLengthAtTop(toCol) : 0;
|
|
99
|
+
const netRunStructureChange = Math.max(move.count + b, a) - Math.max(move.count + a, b);
|
|
100
|
+
const fromBecomesEmpty = move.count === fromCol.length;
|
|
101
|
+
const toWasEmpty = toCol.length === 0;
|
|
102
|
+
const netEmptyDelta = (fromBecomesEmpty ? 1 : 0) - (toWasEmpty ? 1 : 0);
|
|
103
|
+
const revealsHiddenCard = below !== void 0 && !below.faceUp;
|
|
104
|
+
let score = revealsHiddenCard ? 500 : 0;
|
|
105
|
+
score += netRunStructureChange * 30;
|
|
106
|
+
score += netEmptyDelta * 40;
|
|
107
|
+
if (score <= 0) return score;
|
|
108
|
+
return score + move.count * 2;
|
|
109
|
+
}
|
|
110
|
+
function canDealStock(state) {
|
|
111
|
+
if (state.stock.length < NUM_COLUMNS) return false;
|
|
112
|
+
const emptyColumn = state.tableau.some((col) => col.length === 0);
|
|
113
|
+
if (!emptyColumn) return true;
|
|
114
|
+
return state.tableau.reduce((sum, col) => sum + col.length, 0) < NUM_COLUMNS;
|
|
115
|
+
}
|
|
116
|
+
function searchMoves(state) {
|
|
117
|
+
const moves = [];
|
|
118
|
+
for (let from = 0; from < NUM_COLUMNS; from++) {
|
|
119
|
+
const col = state.tableau[from];
|
|
120
|
+
const runLen = runLengthAtTop(col);
|
|
121
|
+
for (let count = 1; count <= runLen; count++) {
|
|
122
|
+
const bottom = col[col.length - count];
|
|
123
|
+
for (let to = 0; to < NUM_COLUMNS; to++) {
|
|
124
|
+
if (to === from) continue;
|
|
125
|
+
const destTop = _chunkW5CPH6TDjs.top.call(void 0, state.tableau[to]);
|
|
126
|
+
if (destTop === void 0 || destTop.faceUp && destTop.rank === bottom.rank + 1) moves.push({ from: { zone: "tableau", index: from }, to: { zone: "tableau", index: to }, count });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return moves;
|
|
131
|
+
}
|
|
132
|
+
function createSpiderController(numSuits) {
|
|
133
|
+
const progress = {
|
|
134
|
+
id: `spider-${numSuits}suit`,
|
|
135
|
+
moves: searchMoves,
|
|
136
|
+
drawIsStep: false,
|
|
137
|
+
key: (state) => _chunkXV5OUPIWjs.columnsKey.call(void 0, state.tableau),
|
|
138
|
+
progress: (state) => [-_chunkXV5OUPIWjs.faceDownCount.call(void 0, state.tableau), state.completed.length]
|
|
139
|
+
};
|
|
140
|
+
return {
|
|
141
|
+
id: "spider",
|
|
142
|
+
title: "Spider",
|
|
143
|
+
newGame(seed) {
|
|
144
|
+
const numDecks = 8 / numSuits;
|
|
145
|
+
const suitFilter = _chunkW5CPH6TDjs.SUITS.slice(0, numSuits);
|
|
146
|
+
const deck = _chunkW5CPH6TDjs.shuffledDeck.call(void 0, seed, numDecks, suitFilter);
|
|
147
|
+
const tableau = Array.from({ length: NUM_COLUMNS }, () => []);
|
|
148
|
+
let cursor = 0;
|
|
149
|
+
for (let col = 0; col < NUM_COLUMNS; col++) {
|
|
150
|
+
const count = DEAL_COUNTS[col];
|
|
151
|
+
for (let i = 0; i < count; i++) {
|
|
152
|
+
const card = deck[cursor++];
|
|
153
|
+
card.faceUp = i === count - 1;
|
|
154
|
+
tableau[col].push(card);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const stock = deck.slice(cursor).map((c) => ({ ...c, faceUp: false }));
|
|
158
|
+
return { tableau, stock, completed: [], completedRuns: [] };
|
|
159
|
+
},
|
|
160
|
+
legalMoves(state) {
|
|
161
|
+
return computeLegalMoves(state);
|
|
162
|
+
},
|
|
163
|
+
tryMove(state, move) {
|
|
164
|
+
const { from, to, count } = move;
|
|
165
|
+
if (from.zone !== "tableau" || to.zone !== "tableau") return null;
|
|
166
|
+
if (from.index === to.index) return null;
|
|
167
|
+
const fromCol = state.tableau[from.index];
|
|
168
|
+
const toCol = state.tableau[to.index];
|
|
169
|
+
if (!fromCol || !toCol) return null;
|
|
170
|
+
if (count < 1 || count > fromCol.length) return null;
|
|
171
|
+
const group = fromCol.slice(fromCol.length - count);
|
|
172
|
+
if (!group.every((c) => c.faceUp)) return null;
|
|
173
|
+
if (!_chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, group)) return null;
|
|
174
|
+
const movingBottom = group[0];
|
|
175
|
+
if (toCol.length > 0) {
|
|
176
|
+
const destTop = toCol[toCol.length - 1];
|
|
177
|
+
if (!destTop.faceUp || destTop.rank !== movingBottom.rank + 1) return null;
|
|
178
|
+
}
|
|
179
|
+
const next = cloneState(state);
|
|
180
|
+
const nextFrom = next.tableau[from.index].slice();
|
|
181
|
+
const nextTo = next.tableau[to.index].slice();
|
|
182
|
+
const moved = nextFrom.splice(nextFrom.length - count, count);
|
|
183
|
+
nextTo.push(...moved);
|
|
184
|
+
next.tableau[from.index] = nextFrom;
|
|
185
|
+
next.tableau[to.index] = nextTo;
|
|
186
|
+
const topIndex = nextFrom.length - 1;
|
|
187
|
+
const newTop = nextFrom[topIndex];
|
|
188
|
+
if (newTop && !newTop.faceUp) nextFrom[topIndex] = { ...newTop, faceUp: true };
|
|
189
|
+
const completedSequence = resolveCompletedSequence(next.tableau, to.index);
|
|
190
|
+
if (completedSequence) {
|
|
191
|
+
next.completed = next.completed.concat(completedSequence.king);
|
|
192
|
+
next.completedRuns = next.completedRuns.concat([completedSequence.run]);
|
|
193
|
+
}
|
|
194
|
+
return next;
|
|
195
|
+
},
|
|
196
|
+
drawStock(state) {
|
|
197
|
+
if (!canDealStock(state)) return null;
|
|
198
|
+
const next = cloneState(state);
|
|
199
|
+
const nextStock = next.stock.slice();
|
|
200
|
+
for (let col = 0; col < NUM_COLUMNS; col++) {
|
|
201
|
+
const card = nextStock.shift();
|
|
202
|
+
const flipped = card.faceUp ? card : { ...card, faceUp: true };
|
|
203
|
+
next.tableau[col] = next.tableau[col].concat(flipped);
|
|
204
|
+
}
|
|
205
|
+
next.stock = nextStock;
|
|
206
|
+
for (let col = 0; col < NUM_COLUMNS; col++) {
|
|
207
|
+
const completedSequence = resolveCompletedSequence(next.tableau, col);
|
|
208
|
+
if (completedSequence) {
|
|
209
|
+
next.completed = next.completed.concat(completedSequence.king);
|
|
210
|
+
next.completedRuns = next.completedRuns.concat([completedSequence.run]);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return next;
|
|
214
|
+
},
|
|
215
|
+
isWon(state) {
|
|
216
|
+
return state.completed.length === 8;
|
|
217
|
+
},
|
|
218
|
+
isStuck(state) {
|
|
219
|
+
if (canDealStock(state)) return false;
|
|
220
|
+
return computeLegalMoves(state).length === 0;
|
|
221
|
+
},
|
|
222
|
+
autoMoveHint(state, from, cardId) {
|
|
223
|
+
if (from.zone !== "tableau") return null;
|
|
224
|
+
const col = state.tableau[from.index];
|
|
225
|
+
if (!col) return null;
|
|
226
|
+
const cardIndex = col.findIndex((c) => c.id === cardId);
|
|
227
|
+
if (cardIndex === -1) return null;
|
|
228
|
+
const movingCards = col.slice(cardIndex);
|
|
229
|
+
if (movingCards.some((c) => !c.faceUp)) return null;
|
|
230
|
+
if (!_chunkW5CPH6TDjs.isDescendingSameSuit.call(void 0, movingCards)) return null;
|
|
231
|
+
const movingBottom = movingCards[0];
|
|
232
|
+
const count = movingCards.length;
|
|
233
|
+
const candidates = state.tableau.map((destCol, index) => ({ index, pile: destCol })).filter((c) => c.index !== from.index).filter((c) => {
|
|
234
|
+
const destTop = _chunkW5CPH6TDjs.top.call(void 0, c.pile);
|
|
235
|
+
return destTop === void 0 || destTop.faceUp && destTop.rank === movingBottom.rank + 1;
|
|
236
|
+
});
|
|
237
|
+
const best = _chunkB56TRALOjs.pickBestTableauTarget.call(void 0, candidates, _chunkW5CPH6TDjs.isDescendingSameSuit);
|
|
238
|
+
if (!best) return null;
|
|
239
|
+
return { from, to: { zone: "tableau", index: best.index }, count };
|
|
240
|
+
},
|
|
241
|
+
scoreHintMove,
|
|
242
|
+
progress
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
exports.canDealStock = canDealStock; exports.createSpiderController = createSpiderController;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { AutoCompleteStep, FinishMove } from './index.mjs';
|
|
2
|
+
import { Card, GameController, AccessibleCard } from './index.mjs';
|
|
3
|
+
|
|
4
|
+
interface FortyThievesState {
|
|
5
|
+
/** 10 tableau columns, 4 cards each at deal, all face up. */
|
|
6
|
+
tableau: Card[][];
|
|
7
|
+
/** 8 independent foundation piles - see this file's header comment on why they aren't
|
|
8
|
+
* fixed one-per-suit. */
|
|
9
|
+
foundation: Card[][];
|
|
10
|
+
/** 64-card face-down draw pile at deal. No recycling once empty. */
|
|
11
|
+
stock: Card[];
|
|
12
|
+
waste: Card[];
|
|
13
|
+
}
|
|
14
|
+
/** Which foundation pile `card` belongs on: a non-empty pile of its own suit already waiting for
|
|
15
|
+
* exactly this rank, or - failing that - an unclaimed (empty) pile, but only for an Ace (nothing
|
|
16
|
+
* else may start a fresh pile). Returns null if neither exists (e.g. a non-Ace whose suit hasn't
|
|
17
|
+
* been started at all yet, or whose two piles are both already ahead of or behind this rank). */
|
|
18
|
+
declare function findFoundationSlot(foundation: Card[][], card: Card): number | null;
|
|
19
|
+
/** Closes over `runMoves` (settings.fortyThievesRunMoves) - the only rule this game's tableau move
|
|
20
|
+
* logic varies by. */
|
|
21
|
+
declare function createFortyThievesController(runMoves: boolean): GameController<FortyThievesState>;
|
|
22
|
+
/** True if the entire remaining game can be cleared via foundation moves alone, in some order -
|
|
23
|
+
* see engine/autoFoundation.ts's own isFullyAutoCompletable for the full "why a plain greedy
|
|
24
|
+
* simulation is correct here" reasoning, which applies unchanged to this game's own foundation
|
|
25
|
+
* shape. */
|
|
26
|
+
declare function isFullyAutoCompletable(controller: Pick<GameController<FortyThievesState>, 'isWon' | 'accessibleCards' | 'tryMove'>, state: FortyThievesState): boolean;
|
|
27
|
+
/** The next single move in a full auto-complete cascade - only meaningful once
|
|
28
|
+
* isFullyAutoCompletable has confirmed true; each successive call (after applying the previous
|
|
29
|
+
* move for real) advances the cascade by exactly one card. */
|
|
30
|
+
declare function nextAutoCompleteMove(controller: Pick<GameController<FortyThievesState>, 'accessibleCards' | 'tryMove'>, state: FortyThievesState): AccessibleCard | null;
|
|
31
|
+
/** True while the board is SOLVED: nothing hidden and no choice left - the stock and the waste are
|
|
32
|
+
* empty (the stock is the only place a face-down card can be here, see newGame/drawStock), the game
|
|
33
|
+
* isn't won yet, and every remaining tableau card can go up by foundation moves alone. The explicit
|
|
34
|
+
* isWon matters: isFullyAutoCompletable is true on a won board, and the finish would never end. Takes
|
|
35
|
+
* the controller because the screen's is per deal (controllerRef), though no foundation move depends
|
|
36
|
+
* on its runMoves. */
|
|
37
|
+
declare function canAutoFinish(controller: Pick<GameController<FortyThievesState>, 'isWon' | 'accessibleCards' | 'tryMove'>, state: FortyThievesState): boolean;
|
|
38
|
+
/** The next card the finish sends up and the foundation pile it goes to (findFoundationSlot's - one
|
|
39
|
+
* of two piles per suit, not a fixed one), or null unless the board is solved. */
|
|
40
|
+
declare function nextFinishMove(controller: Pick<GameController<FortyThievesState>, 'isWon' | 'accessibleCards' | 'tryMove'>, state: FortyThievesState): FinishMove | null;
|
|
41
|
+
/** Draws-aware counterpart of isFullyAutoCompletable above - see engine/autoFoundation.ts's shared
|
|
42
|
+
* isFullyAutoCompletableWithDraws for the full termination argument, which applies unchanged here. */
|
|
43
|
+
declare function isFullyAutoCompletableWithDraws(controller: Pick<GameController<FortyThievesState>, 'isWon' | 'accessibleCards' | 'tryMove' | 'drawStock'>, state: FortyThievesState): boolean;
|
|
44
|
+
/** The next single step in a draws-aware full auto-complete cascade - only meaningful once
|
|
45
|
+
* isFullyAutoCompletableWithDraws has confirmed true; each successive call (after applying the
|
|
46
|
+
* previous step for real) advances the cascade by exactly one step. */
|
|
47
|
+
declare function nextAutoCompleteMoveWithDraws(controller: Pick<GameController<FortyThievesState>, 'accessibleCards' | 'tryMove' | 'drawStock'>, state: FortyThievesState): AutoCompleteStep | null;
|
|
48
|
+
|
|
49
|
+
export { canAutoFinish, createFortyThievesController, findFoundationSlot, isFullyAutoCompletable, isFullyAutoCompletableWithDraws, nextAutoCompleteMove, nextAutoCompleteMoveWithDraws, nextFinishMove };
|
|
50
|
+
export type { FortyThievesState };
|