soliterminal 0.0.0-stage → 0.2.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 +21 -0
- package/README.ko.md +355 -0
- package/README.md +368 -2
- package/dist/app.js +1130 -0
- package/dist/cli.js +496 -0
- package/dist/core/card.js +57 -0
- package/dist/core/deck.js +27 -0
- package/dist/core/klondike.js +364 -0
- package/dist/core/solver.js +811 -0
- package/dist/culprit-file.js +93 -0
- package/dist/saved-game.js +164 -0
- package/dist/share.js +96 -0
- package/dist/solver-client.js +125 -0
- package/dist/solver-wasm.js +47 -0
- package/dist/solver-worker.js +29 -0
- package/dist/solver.wasm +0 -0
- package/dist/stats-file.js +112 -0
- package/dist/stats.js +196 -0
- package/dist/ui/ansi.js +93 -0
- package/dist/ui/help.js +154 -0
- package/dist/ui/input.js +40 -0
- package/dist/ui/keymap.js +46 -0
- package/dist/ui/layout.js +404 -0
- package/dist/ui/screen.js +88 -0
- package/dist/ui/theme.js +155 -0
- package/package.json +50 -4
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import { colorOf } from './card.js';
|
|
2
|
+
import { createDeck, mulberry32, shuffle } from './deck.js';
|
|
3
|
+
export const TABLEAU_COLUMNS = 7;
|
|
4
|
+
export const FOUNDATIONS = 4;
|
|
5
|
+
/** Redeals allowed by default: the waste can be turned over three times. */
|
|
6
|
+
export const DEFAULT_REDEALS = 3;
|
|
7
|
+
export const pile = {
|
|
8
|
+
waste: { kind: 'waste' },
|
|
9
|
+
foundation: (index) => ({ kind: 'foundation', index }),
|
|
10
|
+
tableau: (index) => ({ kind: 'tableau', index }),
|
|
11
|
+
};
|
|
12
|
+
const ok = (state) => ({ ok: true, state });
|
|
13
|
+
const fail = (error) => ({ ok: false, error });
|
|
14
|
+
const top = (cards) => cards[cards.length - 1];
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// Setup
|
|
17
|
+
/** Deals a new game. Column n gets n+1 cards with only the last one face up. */
|
|
18
|
+
export function deal(seed, drawMode = 1, redeals = DEFAULT_REDEALS) {
|
|
19
|
+
const cards = shuffle(createDeck(), mulberry32(seed));
|
|
20
|
+
const columns = Array.from({ length: TABLEAU_COLUMNS }, () => ({ down: [], up: [] }));
|
|
21
|
+
let next = 0;
|
|
22
|
+
for (let row = 0; row < TABLEAU_COLUMNS; row++) {
|
|
23
|
+
for (let col = row; col < TABLEAU_COLUMNS; col++) {
|
|
24
|
+
const card = cards[next++];
|
|
25
|
+
(col === row ? columns[col].up : columns[col].down).push(card);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
seed,
|
|
30
|
+
drawMode,
|
|
31
|
+
stock: cards.slice(next),
|
|
32
|
+
waste: [],
|
|
33
|
+
foundations: Array.from({ length: FOUNDATIONS }, () => []),
|
|
34
|
+
tableau: columns,
|
|
35
|
+
moves: 0,
|
|
36
|
+
redealsLeft: redeals,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Stock
|
|
41
|
+
/**
|
|
42
|
+
* Draws 1 or 3 cards (per `drawMode`) from the stock onto the waste.
|
|
43
|
+
* When the stock is empty, turns the waste back over into the stock.
|
|
44
|
+
*/
|
|
45
|
+
export function draw(state) {
|
|
46
|
+
if (state.stock.length === 0) {
|
|
47
|
+
if (state.waste.length === 0)
|
|
48
|
+
return fail('stock-empty');
|
|
49
|
+
if (state.redealsLeft <= 0)
|
|
50
|
+
return fail('no-redeals');
|
|
51
|
+
return ok({
|
|
52
|
+
...state,
|
|
53
|
+
stock: [...state.waste].reverse(),
|
|
54
|
+
waste: [],
|
|
55
|
+
moves: state.moves + 1,
|
|
56
|
+
redealsLeft: state.redealsLeft - 1,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
const n = Math.min(state.drawMode, state.stock.length);
|
|
60
|
+
// Cards are turned one at a time, so the last one turned ends up on top.
|
|
61
|
+
const turned = state.stock.slice(-n).reverse();
|
|
62
|
+
return ok({
|
|
63
|
+
...state,
|
|
64
|
+
stock: state.stock.slice(0, -n),
|
|
65
|
+
waste: [...state.waste, ...turned],
|
|
66
|
+
moves: state.moves + 1,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
// Moves
|
|
71
|
+
function checkPile(id) {
|
|
72
|
+
if (id.kind === 'foundation' && !(id.index >= 0 && id.index < FOUNDATIONS)) {
|
|
73
|
+
throw new RangeError(`No foundation ${id.index}`);
|
|
74
|
+
}
|
|
75
|
+
if (id.kind === 'tableau' && !(id.index >= 0 && id.index < TABLEAU_COLUMNS)) {
|
|
76
|
+
throw new RangeError(`No tableau column ${id.index}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export function samePile(a, b) {
|
|
80
|
+
if (a.kind === 'waste' || b.kind === 'waste')
|
|
81
|
+
return a.kind === b.kind;
|
|
82
|
+
return a.kind === b.kind && a.index === b.index;
|
|
83
|
+
}
|
|
84
|
+
/** The face-up cards of a pile, top last: the waste, a foundation, or a column's run. */
|
|
85
|
+
export function cardsIn(state, from) {
|
|
86
|
+
checkPile(from);
|
|
87
|
+
return from.kind === 'waste' ? state.waste : from.kind === 'foundation' ? state.foundations[from.index] : state.tableau[from.index].up;
|
|
88
|
+
}
|
|
89
|
+
/** How many cards could be picked up from a pile at most: a whole run, or elsewhere the top card. */
|
|
90
|
+
export function maxTake(state, from) {
|
|
91
|
+
const n = cardsIn(state, from).length;
|
|
92
|
+
return from.kind === 'tableau' ? n : Math.min(n, 1);
|
|
93
|
+
}
|
|
94
|
+
function take(state, from, count) {
|
|
95
|
+
const max = maxTake(state, from);
|
|
96
|
+
if (max === 0)
|
|
97
|
+
return 'empty-source';
|
|
98
|
+
if (!Number.isInteger(count) || count < 1 || count > max)
|
|
99
|
+
return 'bad-count';
|
|
100
|
+
return cardsIn(state, from).slice(-count);
|
|
101
|
+
}
|
|
102
|
+
/** Whether `cards` (bottom card first) may be placed on the destination pile. */
|
|
103
|
+
export function accepts(state, to, cards) {
|
|
104
|
+
checkPile(to);
|
|
105
|
+
const first = cards[0];
|
|
106
|
+
if (!first)
|
|
107
|
+
return false;
|
|
108
|
+
switch (to.kind) {
|
|
109
|
+
case 'waste':
|
|
110
|
+
return false;
|
|
111
|
+
case 'foundation': {
|
|
112
|
+
if (cards.length !== 1)
|
|
113
|
+
return false;
|
|
114
|
+
const target = top(state.foundations[to.index]);
|
|
115
|
+
if (!target)
|
|
116
|
+
return first.rank === 1;
|
|
117
|
+
return first.suit === target.suit && first.rank === target.rank + 1;
|
|
118
|
+
}
|
|
119
|
+
case 'tableau': {
|
|
120
|
+
const target = top(state.tableau[to.index].up);
|
|
121
|
+
if (!target)
|
|
122
|
+
return first.rank === 13;
|
|
123
|
+
return colorOf(target) !== colorOf(first) && target.rank === first.rank + 1;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function remove(state, from, count) {
|
|
128
|
+
switch (from.kind) {
|
|
129
|
+
case 'waste':
|
|
130
|
+
return { ...state, waste: state.waste.slice(0, -1) };
|
|
131
|
+
case 'foundation':
|
|
132
|
+
return {
|
|
133
|
+
...state,
|
|
134
|
+
foundations: state.foundations.map((f, i) => (i === from.index ? f.slice(0, -1) : f)),
|
|
135
|
+
};
|
|
136
|
+
case 'tableau':
|
|
137
|
+
return {
|
|
138
|
+
...state,
|
|
139
|
+
tableau: state.tableau.map((col, i) => {
|
|
140
|
+
if (i !== from.index)
|
|
141
|
+
return col;
|
|
142
|
+
const up = col.up.slice(0, -count);
|
|
143
|
+
// Turn over the next face-down card when the face-up run is gone.
|
|
144
|
+
if (up.length === 0 && col.down.length > 0) {
|
|
145
|
+
return { down: col.down.slice(0, -1), up: col.down.slice(-1) };
|
|
146
|
+
}
|
|
147
|
+
return { down: col.down, up };
|
|
148
|
+
}),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function add(state, to, cards) {
|
|
153
|
+
switch (to.kind) {
|
|
154
|
+
case 'waste':
|
|
155
|
+
throw new Error('Cards cannot be placed on the waste');
|
|
156
|
+
case 'foundation':
|
|
157
|
+
return {
|
|
158
|
+
...state,
|
|
159
|
+
foundations: state.foundations.map((f, i) => (i === to.index ? [...f, ...cards] : f)),
|
|
160
|
+
};
|
|
161
|
+
case 'tableau':
|
|
162
|
+
return {
|
|
163
|
+
...state,
|
|
164
|
+
tableau: state.tableau.map((col, i) => (i === to.index ? { down: col.down, up: [...col.up, ...cards] } : col)),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The foundation a single card can go on: the pile of its suit, or for an ace
|
|
170
|
+
* the first empty one. `undefined` if it fits nowhere yet.
|
|
171
|
+
*/
|
|
172
|
+
export function foundationFor(state, card) {
|
|
173
|
+
const i = state.foundations.findIndex((_, i) => accepts(state, pile.foundation(i), [card]));
|
|
174
|
+
return i >= 0 ? i : undefined;
|
|
175
|
+
}
|
|
176
|
+
/** Moves the top `count` cards from one pile to another, if the rules allow it. */
|
|
177
|
+
export function move(state, from, to, count = 1) {
|
|
178
|
+
checkPile(from);
|
|
179
|
+
checkPile(to);
|
|
180
|
+
if (samePile(from, to))
|
|
181
|
+
return fail('same-pile');
|
|
182
|
+
const cards = take(state, from, count);
|
|
183
|
+
if (typeof cards === 'string')
|
|
184
|
+
return fail(cards);
|
|
185
|
+
if (!accepts(state, to, cards))
|
|
186
|
+
return fail('not-allowed');
|
|
187
|
+
const next = add(remove(state, from, count), to, cards);
|
|
188
|
+
return ok({ ...next, moves: state.moves + 1 });
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The number of cards that makes `from → to` legal, or `undefined` if none does.
|
|
192
|
+
* In Klondike at most one count can work, so the UI never has to ask.
|
|
193
|
+
*/
|
|
194
|
+
export function fittingCount(state, from, to) {
|
|
195
|
+
if (samePile(from, to))
|
|
196
|
+
return undefined;
|
|
197
|
+
const max = maxTake(state, from);
|
|
198
|
+
for (let n = 1; n <= max; n++) {
|
|
199
|
+
const cards = take(state, from, n);
|
|
200
|
+
if (typeof cards !== 'string' && accepts(state, to, cards))
|
|
201
|
+
return n;
|
|
202
|
+
}
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Source key then destination key: moves however many cards fit.
|
|
207
|
+
* A foundation destination is exact. If the card belongs on a different
|
|
208
|
+
* foundation the error is `wrong-foundation`, so the UI can point at the key that finds the right one.
|
|
209
|
+
*/
|
|
210
|
+
export function moveTo(state, from, to) {
|
|
211
|
+
checkPile(from);
|
|
212
|
+
checkPile(to);
|
|
213
|
+
if (samePile(from, to))
|
|
214
|
+
return fail('same-pile');
|
|
215
|
+
if (maxTake(state, from) === 0)
|
|
216
|
+
return fail('empty-source');
|
|
217
|
+
const n = fittingCount(state, from, to);
|
|
218
|
+
if (n !== undefined)
|
|
219
|
+
return move(state, from, to, n);
|
|
220
|
+
if (to.kind === 'foundation' && from.kind !== 'foundation') {
|
|
221
|
+
const card = take(state, from, 1);
|
|
222
|
+
if (typeof card !== 'string' && foundationFor(state, card[0]) !== undefined)
|
|
223
|
+
return fail('wrong-foundation');
|
|
224
|
+
}
|
|
225
|
+
return fail('not-allowed');
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Sends the top card of the waste or a column to the foundation of its suit,
|
|
229
|
+
* or for an ace to the first empty foundation.
|
|
230
|
+
*/
|
|
231
|
+
export function moveToFoundation(state, from) {
|
|
232
|
+
checkPile(from);
|
|
233
|
+
if (from.kind === 'foundation')
|
|
234
|
+
return fail('same-pile');
|
|
235
|
+
const card = take(state, from, 1);
|
|
236
|
+
if (typeof card === 'string')
|
|
237
|
+
return fail(card);
|
|
238
|
+
const home = foundationFor(state, card[0]);
|
|
239
|
+
return home === undefined ? fail('not-allowed') : move(state, from, pile.foundation(home), 1);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Every legal destination for cards from `from`, in auto-move preference
|
|
243
|
+
* order: foundation, then a non-empty column, then an empty column. With
|
|
244
|
+
* `count`, for exactly that many cards; otherwise for however many fit.
|
|
245
|
+
*/
|
|
246
|
+
export function destinations(state, from, count) {
|
|
247
|
+
// An ace fits every empty foundation; offer only the first so auto move is predictable.
|
|
248
|
+
const card = from.kind === 'foundation' ? undefined : cardsIn(state, from).at(-1);
|
|
249
|
+
const home = card && foundationFor(state, card);
|
|
250
|
+
const columns = state.tableau.map((_, i) => i);
|
|
251
|
+
const filled = (i) => state.tableau[i].up.length > 0;
|
|
252
|
+
const targets = [...columns.filter(filled), ...columns.filter((i) => !filled(i))].map((i) => pile.tableau(i));
|
|
253
|
+
if (home !== undefined)
|
|
254
|
+
targets.unshift(pile.foundation(home));
|
|
255
|
+
return targets.filter((to) => {
|
|
256
|
+
const n = count === undefined ? fittingCount(state, from, to) : move(state, from, to, count).ok ? count : undefined;
|
|
257
|
+
if (n === undefined)
|
|
258
|
+
return false;
|
|
259
|
+
// Moving a whole column that has nothing under it into an empty column changes nothing.
|
|
260
|
+
if (from.kind === 'tableau' && to.kind === 'tableau') {
|
|
261
|
+
const col = state.tableau[from.index];
|
|
262
|
+
if (n === col.up.length && col.down.length === 0 && state.tableau[to.index].up.length === 0)
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
return true;
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/** Same key twice: sends cards from `from` to the best legal spot, as `destinations` orders them. */
|
|
269
|
+
export function autoMove(state, from) {
|
|
270
|
+
checkPile(from);
|
|
271
|
+
if (maxTake(state, from) === 0)
|
|
272
|
+
return fail('empty-source');
|
|
273
|
+
const to = destinations(state, from)[0];
|
|
274
|
+
return to ? moveTo(state, from, to) : fail('no-move');
|
|
275
|
+
}
|
|
276
|
+
// ---------------------------------------------------------------------------
|
|
277
|
+
// End of game
|
|
278
|
+
export function isWon(state) {
|
|
279
|
+
return state.foundations.every((f) => f.length === 13);
|
|
280
|
+
}
|
|
281
|
+
/** True once every card is face up and out of the stock, so the rest plays itself. */
|
|
282
|
+
export function canAutoComplete(state) {
|
|
283
|
+
return (!isWon(state) &&
|
|
284
|
+
state.stock.length === 0 &&
|
|
285
|
+
state.waste.length === 0 &&
|
|
286
|
+
state.tableau.every((col) => col.down.length === 0));
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Plays the remaining cards onto the foundations, lowest rank first.
|
|
290
|
+
* Returns every intermediate state so the UI can animate them.
|
|
291
|
+
*/
|
|
292
|
+
export function autoComplete(state) {
|
|
293
|
+
if (!canAutoComplete(state))
|
|
294
|
+
return [];
|
|
295
|
+
const steps = [];
|
|
296
|
+
let current = state;
|
|
297
|
+
while (!isWon(current)) {
|
|
298
|
+
// Runs descend toward the top, so the lowest top card is always the next one
|
|
299
|
+
// its foundation needs.
|
|
300
|
+
let column = -1;
|
|
301
|
+
let lowest;
|
|
302
|
+
current.tableau.forEach((col, i) => {
|
|
303
|
+
const card = top(col.up);
|
|
304
|
+
if (card && (!lowest || card.rank < lowest.rank)) {
|
|
305
|
+
lowest = card;
|
|
306
|
+
column = i;
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
if (!lowest)
|
|
310
|
+
break;
|
|
311
|
+
const home = foundationFor(current, lowest);
|
|
312
|
+
if (home === undefined)
|
|
313
|
+
break;
|
|
314
|
+
const result = move(current, pile.tableau(column), pile.foundation(home));
|
|
315
|
+
if (!result.ok)
|
|
316
|
+
break;
|
|
317
|
+
current = result.state;
|
|
318
|
+
steps.push(current);
|
|
319
|
+
}
|
|
320
|
+
return steps;
|
|
321
|
+
}
|
|
322
|
+
// ---------------------------------------------------------------------------
|
|
323
|
+
// Positions
|
|
324
|
+
const cardsKey = (cards) => cards.map((c) => c.suit + c.rank).join(',');
|
|
325
|
+
/**
|
|
326
|
+
* A string that is the same for two states exactly when the same game goes
|
|
327
|
+
* on from them. The move count is left out, and so is which foundation holds
|
|
328
|
+
* which suit, since foundations have no fixed suit.
|
|
329
|
+
*/
|
|
330
|
+
export function positionKey(state) {
|
|
331
|
+
const foundations = state.foundations.map(cardsKey).sort().join('/');
|
|
332
|
+
const tableau = state.tableau.map((c) => `${cardsKey(c.down)}|${cardsKey(c.up)}`).join('/');
|
|
333
|
+
return `${state.drawMode};${state.redealsLeft};${cardsKey(state.stock)};${cardsKey(state.waste)};${foundations};${tableau}`;
|
|
334
|
+
}
|
|
335
|
+
/** Every legal step from a position, a draw or a move of any number of cards, with where it leads. */
|
|
336
|
+
export function steps(state) {
|
|
337
|
+
const out = [];
|
|
338
|
+
const add = (step, r) => {
|
|
339
|
+
if (r.ok)
|
|
340
|
+
out.push({ step, state: r.state });
|
|
341
|
+
};
|
|
342
|
+
add({ draw: true }, draw(state));
|
|
343
|
+
const tableau = state.tableau.map((_, i) => pile.tableau(i));
|
|
344
|
+
const foundations = state.foundations.map((_, i) => pile.foundation(i));
|
|
345
|
+
for (const from of [pile.waste, ...foundations, ...tableau]) {
|
|
346
|
+
const max = maxTake(state, from);
|
|
347
|
+
for (let count = 1; count <= max; count++) {
|
|
348
|
+
for (const to of [...foundations, ...tableau])
|
|
349
|
+
if (!samePile(from, to))
|
|
350
|
+
add({ from, to, count }, move(state, from, to, count));
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return out;
|
|
354
|
+
}
|
|
355
|
+
/** Every position one step away, each once however many steps lead to it. */
|
|
356
|
+
export function successors(state) {
|
|
357
|
+
const out = new Map();
|
|
358
|
+
for (const { state: next } of steps(state)) {
|
|
359
|
+
const key = positionKey(next);
|
|
360
|
+
if (!out.has(key))
|
|
361
|
+
out.set(key, next);
|
|
362
|
+
}
|
|
363
|
+
return [...out.values()];
|
|
364
|
+
}
|