@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
|
+
import {
|
|
2
|
+
isFullyAutoCompletable,
|
|
3
|
+
nextAutoCompleteMove
|
|
4
|
+
} from "./chunk-3BMUGHKQ.mjs";
|
|
5
|
+
import {
|
|
6
|
+
canPlaceOnFoundation,
|
|
7
|
+
suitIndex
|
|
8
|
+
} from "./chunk-SDZPP3HI.mjs";
|
|
9
|
+
import {
|
|
10
|
+
pickBestTableauTarget
|
|
11
|
+
} from "./chunk-GFP5NUYK.mjs";
|
|
12
|
+
import {
|
|
13
|
+
columnsKey,
|
|
14
|
+
faceDownCount,
|
|
15
|
+
foundationDownMoves,
|
|
16
|
+
pileKey
|
|
17
|
+
} from "./chunk-FDHHAPZA.mjs";
|
|
18
|
+
import {
|
|
19
|
+
cardColor,
|
|
20
|
+
isDescendingAltColor,
|
|
21
|
+
shuffledDeck,
|
|
22
|
+
top
|
|
23
|
+
} from "./chunk-XSS5M6C7.mjs";
|
|
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 state.tableau[ref.index] ?? null;
|
|
38
|
+
case "foundation":
|
|
39
|
+
return 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 = top(dest);
|
|
73
|
+
if (destTop === void 0) {
|
|
74
|
+
return movingTop.rank === 13;
|
|
75
|
+
}
|
|
76
|
+
if (!destTop.faceUp) return false;
|
|
77
|
+
const destColor = cardColor(destTop.suit);
|
|
78
|
+
const movingColor = cardColor(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 || cardColor(next.suit) === cardColor(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 && cardColor(below.suit) !== cardColor(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 = suitIndex(card.suit);
|
|
123
|
+
if (canPlaceOnFoundation(state.foundation[f], card)) moves.push({ from, to: { zone: "foundation", index: f }, count: 1 });
|
|
124
|
+
};
|
|
125
|
+
const wasteTop = top(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 = shuffledDeck(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 (!isDescendingAltColor(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 (suitIndex(movingCard.suit) !== to.index) return null;
|
|
185
|
+
if (!canPlaceOnFoundation(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 = top(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 (isDescendingAltColor(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 = pickBestTableauTarget(candidates, 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" || !isDescendingAltColor(movingCards))) return null;
|
|
278
|
+
if (count === 1) {
|
|
279
|
+
const movingCard = movingCards[0];
|
|
280
|
+
const f = suitIndex(movingCard.suit);
|
|
281
|
+
if (canPlaceOnFoundation(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 = pickBestTableauTarget(candidates, 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 = top(state.waste);
|
|
293
|
+
if (wasteTop) result.push({ ref: { zone: "waste", index: 0 }, card: wasteTop });
|
|
294
|
+
state.tableau.forEach((pile, index) => {
|
|
295
|
+
const t = top(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) => foundationDownMoves(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) => `${columnsKey(state.tableau)}/${state.foundation.map((p) => p.length).join(",")}/${pileKey(state.stock)}/${pileKey(state.waste)}`,
|
|
308
|
+
progress: (state) => [-faceDownCount(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) && isFullyAutoCompletable(controller, state);
|
|
328
|
+
}
|
|
329
|
+
function nextFinishMove(controller, state) {
|
|
330
|
+
if (!canAutoFinish(controller, state)) return null;
|
|
331
|
+
const entry = nextAutoCompleteMove(controller, state);
|
|
332
|
+
return entry ? { entry, to: { zone: "foundation", index: suitIndex(entry.card.suit) } } : null;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export {
|
|
336
|
+
createKlondikeController,
|
|
337
|
+
canAutoFinish,
|
|
338
|
+
nextFinishMove
|
|
339
|
+
};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkW5CPH6TDjs = require('./chunk-W5CPH6TD.js');
|
|
5
|
+
|
|
6
|
+
// src/games/golf/rules.ts
|
|
7
|
+
function cloneState(state) {
|
|
8
|
+
return {
|
|
9
|
+
tableau: state.tableau.slice(),
|
|
10
|
+
waste: state.waste,
|
|
11
|
+
stock: state.stock
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function setPile(state, ref, pile) {
|
|
15
|
+
switch (ref.zone) {
|
|
16
|
+
case "tableau":
|
|
17
|
+
state.tableau[ref.index] = pile;
|
|
18
|
+
break;
|
|
19
|
+
case "waste":
|
|
20
|
+
state.waste = pile;
|
|
21
|
+
break;
|
|
22
|
+
case "stock":
|
|
23
|
+
state.stock = pile;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function newGame(seed) {
|
|
28
|
+
const deck = _chunkW5CPH6TDjs.shuffledDeck.call(void 0, seed, 1);
|
|
29
|
+
let cursor = 0;
|
|
30
|
+
const tableau = [];
|
|
31
|
+
for (let col = 0; col < 7; col++) {
|
|
32
|
+
const cards = [];
|
|
33
|
+
for (let i = 0; i < 5; i++) {
|
|
34
|
+
const card = deck[cursor++];
|
|
35
|
+
cards.push({ ...card, faceUp: true });
|
|
36
|
+
}
|
|
37
|
+
tableau.push(cards);
|
|
38
|
+
}
|
|
39
|
+
const starter = deck[cursor++];
|
|
40
|
+
const waste = [{ ...starter, faceUp: true }];
|
|
41
|
+
const stock = deck.slice(cursor).map((c) => ({ ...c, faceUp: false }));
|
|
42
|
+
return { tableau, waste, stock };
|
|
43
|
+
}
|
|
44
|
+
function isAdjacentRank(a, b, wrapAceKing) {
|
|
45
|
+
if (Math.abs(a.rank - b.rank) === 1) return true;
|
|
46
|
+
return wrapAceKing && (a.rank === 1 && b.rank === 13 || a.rank === 13 && b.rank === 1);
|
|
47
|
+
}
|
|
48
|
+
function drawStock(state) {
|
|
49
|
+
if (state.stock.length === 0) return null;
|
|
50
|
+
const next = cloneState(state);
|
|
51
|
+
const nextStock = next.stock.slice();
|
|
52
|
+
const nextWaste = next.waste.slice();
|
|
53
|
+
const card = nextStock.pop();
|
|
54
|
+
nextWaste.push(card.faceUp ? card : { ...card, faceUp: true });
|
|
55
|
+
setPile(next, { zone: "stock", index: 0 }, nextStock);
|
|
56
|
+
setPile(next, { zone: "waste", index: 0 }, nextWaste);
|
|
57
|
+
return next;
|
|
58
|
+
}
|
|
59
|
+
function isWon(state) {
|
|
60
|
+
return state.tableau.every((col) => col.length === 0);
|
|
61
|
+
}
|
|
62
|
+
function createGolfController(wrapAceKing) {
|
|
63
|
+
function tryMove(state, move) {
|
|
64
|
+
const { from, to, count } = move;
|
|
65
|
+
if (count !== 1) return null;
|
|
66
|
+
if (from.zone !== "tableau") return null;
|
|
67
|
+
if (to.zone !== "waste" || to.index !== 0) return null;
|
|
68
|
+
const col = state.tableau[from.index];
|
|
69
|
+
if (!col || col.length === 0) return null;
|
|
70
|
+
const movingCard = _chunkW5CPH6TDjs.top.call(void 0, col);
|
|
71
|
+
const wasteTop = _chunkW5CPH6TDjs.top.call(void 0, state.waste);
|
|
72
|
+
if (!wasteTop) return null;
|
|
73
|
+
if (!isAdjacentRank(movingCard, wasteTop, wrapAceKing)) return null;
|
|
74
|
+
const next = cloneState(state);
|
|
75
|
+
const nextCol = next.tableau[from.index].slice();
|
|
76
|
+
const moved = nextCol.pop();
|
|
77
|
+
setPile(next, from, nextCol);
|
|
78
|
+
const nextWaste = next.waste.slice();
|
|
79
|
+
nextWaste.push(moved);
|
|
80
|
+
setPile(next, to, nextWaste);
|
|
81
|
+
return next;
|
|
82
|
+
}
|
|
83
|
+
function legalMoves(state) {
|
|
84
|
+
const moves = [];
|
|
85
|
+
const wasteTop = _chunkW5CPH6TDjs.top.call(void 0, state.waste);
|
|
86
|
+
if (!wasteTop) return moves;
|
|
87
|
+
state.tableau.forEach((col, index) => {
|
|
88
|
+
const t = _chunkW5CPH6TDjs.top.call(void 0, col);
|
|
89
|
+
if (t && isAdjacentRank(t, wasteTop, wrapAceKing)) {
|
|
90
|
+
moves.push({ from: { zone: "tableau", index }, to: { zone: "waste", index: 0 }, count: 1 });
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return moves;
|
|
94
|
+
}
|
|
95
|
+
function isStuck(state) {
|
|
96
|
+
return !isWon(state) && state.stock.length === 0 && legalMoves(state).length === 0;
|
|
97
|
+
}
|
|
98
|
+
function autoMoveHint(state, from, cardId) {
|
|
99
|
+
if (from.zone !== "tableau") return null;
|
|
100
|
+
const col = state.tableau[from.index];
|
|
101
|
+
const t = _chunkW5CPH6TDjs.top.call(void 0, col);
|
|
102
|
+
if (!t || t.id !== cardId) return null;
|
|
103
|
+
const wasteTop = _chunkW5CPH6TDjs.top.call(void 0, state.waste);
|
|
104
|
+
if (!wasteTop || !isAdjacentRank(t, wasteTop, wrapAceKing)) return null;
|
|
105
|
+
return { from, to: { zone: "waste", index: 0 }, count: 1 };
|
|
106
|
+
}
|
|
107
|
+
function scoreHintMove(state, move) {
|
|
108
|
+
const next = tryMove(state, move);
|
|
109
|
+
if (!next) return 0;
|
|
110
|
+
const fromCol = state.tableau[move.from.index];
|
|
111
|
+
const emptiesColumn = fromCol.length === move.count;
|
|
112
|
+
const followUpMoves = legalMoves(next).length;
|
|
113
|
+
return 1 + followUpMoves * 10 + (emptiesColumn ? 100 : 0);
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
id: "golf",
|
|
117
|
+
title: "Golf",
|
|
118
|
+
newGame,
|
|
119
|
+
legalMoves,
|
|
120
|
+
tryMove,
|
|
121
|
+
drawStock,
|
|
122
|
+
isWon,
|
|
123
|
+
isStuck,
|
|
124
|
+
autoMoveHint,
|
|
125
|
+
scoreHintMove
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
exports.createGolfController = createGolfController;
|