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