@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
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 Infinite Token LLC (https://www.infinitetoken.com/)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tastic/cardtastic-engine
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
CardTastic's solitaire rules: the eleven games (Klondike, Spider, FreeCell, Pyramid, Yukon, Golf, TriPeaks, Canfield, Scorpion,
|
|
4
|
+
Forty Thieves and Aces Up), their seeded deals and moves, and a replay that checks a played deal. Shared by the CardTastic app and
|
|
5
|
+
Tastic-Server, which replays every run sent to its daily leaderboards with these same rules. Plain TypeScript with no dependencies,
|
|
6
|
+
so it runs in React Native and in Node alike.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @tastic/cardtastic-engine
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Exports
|
|
13
|
+
|
|
14
|
+
The root (`@tastic/cardtastic-engine`) has everything shared by the games:
|
|
15
|
+
|
|
16
|
+
- **Cards and deals:** `Card`, `Suit`, `Rank`, `SUITS`, `cardColor`, `makeDeck`, `shuffledDeck(seed, decks?, suits?)`,
|
|
17
|
+
`mulberry32`, `shuffle`, `randomSeed`.
|
|
18
|
+
- **The rules' shape:** `GameController<State>` (`newGame(seed)`, `legalMoves`, `tryMove`, `drawStock`, `isWon` and the optional
|
|
19
|
+
hint and progress pieces), `Move`, `PileRef`, plus the pile, foundation and auto-move helpers the games are built from.
|
|
20
|
+
- **Hints:** `findHint`, `hintCountFor` and the look-ahead (`ProgressSearch` and its budgets).
|
|
21
|
+
- **Rule sets:** `VariantId` (one game under one rule set: `'klondike'` is Draw 1, `'klondike-draw3'` Draw 3, and so on),
|
|
22
|
+
`VARIANTS`, `variantInfo`, `gameOfVariant`, `SolitaireGameId`, `SOLITAIRE_GAME_IDS`.
|
|
23
|
+
- **Score:** `computeScore(moves, seconds)`, lower is better.
|
|
24
|
+
- **Replay:** `controllerFor(variant)`, `replayRun`, `checkActions`, `runMove`, `moveOf`, `RunAction`, `MAX_RUN_ACTIONS`,
|
|
25
|
+
`MAX_SEED`.
|
|
26
|
+
- **`RULES_VERSION`:** the package's version. A server only replays runs from an app on the same rules.
|
|
27
|
+
|
|
28
|
+
Each game's own rules are a subpath, since several games name their helpers alike:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { createKlondikeController, type KlondikeState } from '@tastic/cardtastic-engine/klondike'
|
|
32
|
+
import { PyramidController } from '@tastic/cardtastic-engine/pyramid'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The subpaths are `klondike`, `spider`, `freecell`, `pyramid`, `yukon`, `golf`, `tripeaks`, `canfield`, `scorpion`,
|
|
36
|
+
`fortythieves` and `acesup`.
|
|
37
|
+
|
|
38
|
+
## Runs
|
|
39
|
+
|
|
40
|
+
A run is every action that changed the board, in order, as the player made them:
|
|
41
|
+
|
|
42
|
+
- a move: `[fromZone, fromIndex, toZone, toIndex, cards]`, e.g. `['tableau', 3, 'foundation', 0, 1]` (`runMove(move)` writes one)
|
|
43
|
+
- `'d'`: a draw from the stock (a deal in Spider and Scorpion, a round in Aces Up, turning the waste over in Klondike)
|
|
44
|
+
- `'u'`: an Undo press, taking back the action before it
|
|
45
|
+
|
|
46
|
+
Auto-played cards are moves like any other. A draw straight onto a foundation is two actions, the draw and the move.
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const result = replayRun('klondike', seed, actions)
|
|
50
|
+
// { ok: true, won, moves, undos, state } or { ok: false, error, at }
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`replayRun` deals `seed` for the variant and plays each action with the rules. It fails at the first move or draw the rules refuse,
|
|
54
|
+
an undo with nothing to undo, anything after the deal is won, or a move that would make or lose a card; `at` is that action's index
|
|
55
|
+
(`-1` when the game, the seed or the run itself can't be read). `moves` counts every move and draw, undone or not, as the app's
|
|
56
|
+
score does, and `undos` the Undo presses. Runs are capped at `MAX_RUN_ACTIONS` (5,000).
|
|
57
|
+
|
|
58
|
+
## Development
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npm run verify # lint + test + typecheck + build
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Every game's rules tests came with its rules from the app. `replay.test.ts` plays each rule set with a random player (moves, draws
|
|
65
|
+
and undos) and replays the run to the same board, move count and undo count, and replays a deal Hint wins in every rule set Hint
|
|
66
|
+
wins within 40 seeds.
|
|
67
|
+
|
|
68
|
+
Releasing: `npm run release:patch` (or minor, major) sets `RULES_VERSION` to the new version as `npm version` runs, and a `v*` tag
|
|
69
|
+
publishes to npm through the fleet's shared workflow. A new version is new rules: release the server on it, then the app.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { GameController, Card } from './index.mjs';
|
|
2
|
+
|
|
3
|
+
interface AcesUpState {
|
|
4
|
+
/** 4 piles; only the top card of each is ever in play - everything under it is permanently
|
|
5
|
+
* buried until it's the top itself. */
|
|
6
|
+
piles: Card[][];
|
|
7
|
+
/** 48 cards after the initial 4-card deal (one per pile), dealt out 4 at a time thereafter (one
|
|
8
|
+
* more card onto each pile, including an empty one) - see drawStock. */
|
|
9
|
+
stock: Card[];
|
|
10
|
+
/** Every card ever discarded, in discard order (oldest first) - mirrors Pyramid's own `discard`
|
|
11
|
+
* field. A won deal leaves exactly 4 cards (the Aces) outside this pile, everything else here. */
|
|
12
|
+
discard: Card[];
|
|
13
|
+
}
|
|
14
|
+
declare const AcesUpController: GameController<AcesUpState>;
|
|
15
|
+
|
|
16
|
+
export { AcesUpController };
|
|
17
|
+
export type { AcesUpState };
|
package/dist/acesup.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { GameController, Card } from './index.js';
|
|
2
|
+
|
|
3
|
+
interface AcesUpState {
|
|
4
|
+
/** 4 piles; only the top card of each is ever in play - everything under it is permanently
|
|
5
|
+
* buried until it's the top itself. */
|
|
6
|
+
piles: Card[][];
|
|
7
|
+
/** 48 cards after the initial 4-card deal (one per pile), dealt out 4 at a time thereafter (one
|
|
8
|
+
* more card onto each pile, including an empty one) - see drawStock. */
|
|
9
|
+
stock: Card[];
|
|
10
|
+
/** Every card ever discarded, in discard order (oldest first) - mirrors Pyramid's own `discard`
|
|
11
|
+
* field. A won deal leaves exactly 4 cards (the Aces) outside this pile, everything else here. */
|
|
12
|
+
discard: Card[];
|
|
13
|
+
}
|
|
14
|
+
declare const AcesUpController: GameController<AcesUpState>;
|
|
15
|
+
|
|
16
|
+
export { AcesUpController };
|
|
17
|
+
export type { AcesUpState };
|
package/dist/acesup.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkPTRHGPCNjs = require('./chunk-PTRHGPCN.js');
|
|
4
|
+
require('./chunk-XV5OUPIW.js');
|
|
5
|
+
require('./chunk-W5CPH6TD.js');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.AcesUpController = _chunkPTRHGPCNjs.AcesUpController;
|
package/dist/acesup.mjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { A as AccessibleCard, P as PileRef, C as Card, G as GameController } from './gameTypes-BnfLkwzA.js';
|
|
2
|
+
|
|
3
|
+
/** True if no card of the opposite color could still need `card` as a tableau landing spot - see
|
|
4
|
+
* "Mechanism A" above. Does not check whether the move is legal right now; see findSafeAutoPlay. */
|
|
5
|
+
declare function isSafeAutoPlay(foundation: Card[][], card: Card): boolean;
|
|
6
|
+
/** The next single card to safely auto-play to its foundation, if any - legal on its own suit's
|
|
7
|
+
* foundation right now AND passing isSafeAutoPlay. Call again after each resulting move; a move
|
|
8
|
+
* can make the next card both accessible and safe.
|
|
9
|
+
*
|
|
10
|
+
* The waste top is scanned like any other accessible card, but only a cascade the player's own play
|
|
11
|
+
* of a card to a foundation set off ever runs this, and the player's next draw or other move stops
|
|
12
|
+
* it - a card they just drew or uncovered is never played for them (see useAutoFoundationCascade). */
|
|
13
|
+
declare function findSafeAutoPlay(accessible: AccessibleCard[], foundation: Card[][]): AccessibleCard | null;
|
|
14
|
+
/** True if the entire remaining game can be cleared via foundation moves alone, in some order -
|
|
15
|
+
* i.e. the player has already won and it's just a matter of mechanically sending every card up.
|
|
16
|
+
* See "Mechanism B" above for why a plain greedy simulation (via the controller's own tryMove,
|
|
17
|
+
* against a scratch copy of state - never the real one) is a correct decision procedure here. */
|
|
18
|
+
declare function isFullyAutoCompletable<TState>(controller: GameController<TState>, state: TState): boolean;
|
|
19
|
+
/** The next single move in a full auto-complete cascade - only meaningful once
|
|
20
|
+
* isFullyAutoCompletable has confirmed true; each successive call (after applying the previous
|
|
21
|
+
* move for real) advances the cascade by exactly one card. */
|
|
22
|
+
declare function nextAutoCompleteMove<TState>(controller: GameController<TState>, state: TState): AccessibleCard | null;
|
|
23
|
+
/** One card of a finish (games/shared/useAutoFinish): the card, and the foundation it goes to. */
|
|
24
|
+
interface FinishMove {
|
|
25
|
+
entry: AccessibleCard;
|
|
26
|
+
to: PileRef;
|
|
27
|
+
}
|
|
28
|
+
/** One step of a draws-aware auto-complete cascade (see isFullyAutoCompletableWithDraws below):
|
|
29
|
+
* either an ordinary foundation play, a plain stock draw/recycle with nothing new immediately
|
|
30
|
+
* playable, or a draw that itself exposes a card ready to go straight to its foundation. Never
|
|
31
|
+
* names a concrete state type - a draw's outcome is read back through the same accessibleCards()/
|
|
32
|
+
* tryMove() interface as everything else here, never by reaching into TState's own shape. */
|
|
33
|
+
type AutoCompleteStep = {
|
|
34
|
+
kind: 'play';
|
|
35
|
+
entry: AccessibleCard;
|
|
36
|
+
to: PileRef;
|
|
37
|
+
} | {
|
|
38
|
+
kind: 'draw';
|
|
39
|
+
} | {
|
|
40
|
+
kind: 'drawAndPlay';
|
|
41
|
+
entry: AccessibleCard;
|
|
42
|
+
to: PileRef;
|
|
43
|
+
};
|
|
44
|
+
/** Draws-aware counterpart of isFullyAutoCompletable, for games with a stock: also simulates
|
|
45
|
+
* drawing (and recycling, as many passes as it takes) once no foundation move is currently
|
|
46
|
+
* available, rather than giving up the instant accessibleCards() runs dry. See this file's header
|
|
47
|
+
* comment for why a forced, deterministic draw doesn't reopen the "full solver" problem mechanism
|
|
48
|
+
* B otherwise avoids.
|
|
49
|
+
*
|
|
50
|
+
* Termination: a 'play' step always strictly grows the foundation (at most 52 total, or however
|
|
51
|
+
* many cards this game deals), so any run of consecutive 'draw' steps is finite - the only way this
|
|
52
|
+
* loop could fail to terminate is by recycling through the exact same stock/waste arrangement
|
|
53
|
+
* forever with nothing ever playable in between, which `seen` catches (recycling can only ever
|
|
54
|
+
* reproduce a position that's already been visited, never a genuinely new one, once no play has
|
|
55
|
+
* happened since the last time this exact arrangement occurred). */
|
|
56
|
+
declare function isFullyAutoCompletableWithDraws<TState>(controller: GameController<TState>, state: TState): boolean;
|
|
57
|
+
/** The next single step in a draws-aware full auto-complete cascade - only meaningful once
|
|
58
|
+
* isFullyAutoCompletableWithDraws has confirmed true; each successive call (after applying the
|
|
59
|
+
* previous step for real) advances the cascade by exactly one step. */
|
|
60
|
+
declare function nextAutoCompleteMoveWithDraws<TState>(controller: GameController<TState>, state: TState): AutoCompleteStep | null;
|
|
61
|
+
|
|
62
|
+
export { isFullyAutoCompletableWithDraws as a, isSafeAutoPlay as b, nextAutoCompleteMoveWithDraws as c, findSafeAutoPlay as f, isFullyAutoCompletable as i, nextAutoCompleteMove as n };
|
|
63
|
+
export type { AutoCompleteStep as A, FinishMove as F };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { A as AccessibleCard, P as PileRef, C as Card, G as GameController } from './gameTypes-BnfLkwzA.mjs';
|
|
2
|
+
|
|
3
|
+
/** True if no card of the opposite color could still need `card` as a tableau landing spot - see
|
|
4
|
+
* "Mechanism A" above. Does not check whether the move is legal right now; see findSafeAutoPlay. */
|
|
5
|
+
declare function isSafeAutoPlay(foundation: Card[][], card: Card): boolean;
|
|
6
|
+
/** The next single card to safely auto-play to its foundation, if any - legal on its own suit's
|
|
7
|
+
* foundation right now AND passing isSafeAutoPlay. Call again after each resulting move; a move
|
|
8
|
+
* can make the next card both accessible and safe.
|
|
9
|
+
*
|
|
10
|
+
* The waste top is scanned like any other accessible card, but only a cascade the player's own play
|
|
11
|
+
* of a card to a foundation set off ever runs this, and the player's next draw or other move stops
|
|
12
|
+
* it - a card they just drew or uncovered is never played for them (see useAutoFoundationCascade). */
|
|
13
|
+
declare function findSafeAutoPlay(accessible: AccessibleCard[], foundation: Card[][]): AccessibleCard | null;
|
|
14
|
+
/** True if the entire remaining game can be cleared via foundation moves alone, in some order -
|
|
15
|
+
* i.e. the player has already won and it's just a matter of mechanically sending every card up.
|
|
16
|
+
* See "Mechanism B" above for why a plain greedy simulation (via the controller's own tryMove,
|
|
17
|
+
* against a scratch copy of state - never the real one) is a correct decision procedure here. */
|
|
18
|
+
declare function isFullyAutoCompletable<TState>(controller: GameController<TState>, state: TState): boolean;
|
|
19
|
+
/** The next single move in a full auto-complete cascade - only meaningful once
|
|
20
|
+
* isFullyAutoCompletable has confirmed true; each successive call (after applying the previous
|
|
21
|
+
* move for real) advances the cascade by exactly one card. */
|
|
22
|
+
declare function nextAutoCompleteMove<TState>(controller: GameController<TState>, state: TState): AccessibleCard | null;
|
|
23
|
+
/** One card of a finish (games/shared/useAutoFinish): the card, and the foundation it goes to. */
|
|
24
|
+
interface FinishMove {
|
|
25
|
+
entry: AccessibleCard;
|
|
26
|
+
to: PileRef;
|
|
27
|
+
}
|
|
28
|
+
/** One step of a draws-aware auto-complete cascade (see isFullyAutoCompletableWithDraws below):
|
|
29
|
+
* either an ordinary foundation play, a plain stock draw/recycle with nothing new immediately
|
|
30
|
+
* playable, or a draw that itself exposes a card ready to go straight to its foundation. Never
|
|
31
|
+
* names a concrete state type - a draw's outcome is read back through the same accessibleCards()/
|
|
32
|
+
* tryMove() interface as everything else here, never by reaching into TState's own shape. */
|
|
33
|
+
type AutoCompleteStep = {
|
|
34
|
+
kind: 'play';
|
|
35
|
+
entry: AccessibleCard;
|
|
36
|
+
to: PileRef;
|
|
37
|
+
} | {
|
|
38
|
+
kind: 'draw';
|
|
39
|
+
} | {
|
|
40
|
+
kind: 'drawAndPlay';
|
|
41
|
+
entry: AccessibleCard;
|
|
42
|
+
to: PileRef;
|
|
43
|
+
};
|
|
44
|
+
/** Draws-aware counterpart of isFullyAutoCompletable, for games with a stock: also simulates
|
|
45
|
+
* drawing (and recycling, as many passes as it takes) once no foundation move is currently
|
|
46
|
+
* available, rather than giving up the instant accessibleCards() runs dry. See this file's header
|
|
47
|
+
* comment for why a forced, deterministic draw doesn't reopen the "full solver" problem mechanism
|
|
48
|
+
* B otherwise avoids.
|
|
49
|
+
*
|
|
50
|
+
* Termination: a 'play' step always strictly grows the foundation (at most 52 total, or however
|
|
51
|
+
* many cards this game deals), so any run of consecutive 'draw' steps is finite - the only way this
|
|
52
|
+
* loop could fail to terminate is by recycling through the exact same stock/waste arrangement
|
|
53
|
+
* forever with nothing ever playable in between, which `seen` catches (recycling can only ever
|
|
54
|
+
* reproduce a position that's already been visited, never a genuinely new one, once no play has
|
|
55
|
+
* happened since the last time this exact arrangement occurred). */
|
|
56
|
+
declare function isFullyAutoCompletableWithDraws<TState>(controller: GameController<TState>, state: TState): boolean;
|
|
57
|
+
/** The next single step in a draws-aware full auto-complete cascade - only meaningful once
|
|
58
|
+
* isFullyAutoCompletableWithDraws has confirmed true; each successive call (after applying the
|
|
59
|
+
* previous step for real) advances the cascade by exactly one step. */
|
|
60
|
+
declare function nextAutoCompleteMoveWithDraws<TState>(controller: GameController<TState>, state: TState): AutoCompleteStep | null;
|
|
61
|
+
|
|
62
|
+
export { isFullyAutoCompletableWithDraws as a, isSafeAutoPlay as b, nextAutoCompleteMoveWithDraws as c, findSafeAutoPlay as f, isFullyAutoCompletable as i, nextAutoCompleteMove as n };
|
|
63
|
+
export type { AutoCompleteStep as A, FinishMove as F };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { FinishMove } from './index.mjs';
|
|
2
|
+
import { Card, Rank, GameController, AccessibleCard } from './index.mjs';
|
|
3
|
+
|
|
4
|
+
interface CanfieldState {
|
|
5
|
+
/** 4 tableau columns, each dealt 1 card face up. */
|
|
6
|
+
tableau: Card[][];
|
|
7
|
+
/** 4 foundation piles, indexed to match engine/foundation's FOUNDATION_SUITS order - each must start at
|
|
8
|
+
* `foundationRank` and proceeds through a full 13-card cycle (wrapping King -> Ace). */
|
|
9
|
+
foundation: Card[][];
|
|
10
|
+
/** The rank every foundation starts from - set once, at deal, from the very first card cut from
|
|
11
|
+
* the deck (which becomes that suit's first foundation card). */
|
|
12
|
+
foundationRank: Rank;
|
|
13
|
+
/** 13 cards, only the last (top) face up - a second, single-card-at-a-time source alongside
|
|
14
|
+
* waste, with its own empty-tableau-column priority (see canPlaceOnTableau). */
|
|
15
|
+
reserve: Card[];
|
|
16
|
+
stock: Card[];
|
|
17
|
+
waste: Card[];
|
|
18
|
+
}
|
|
19
|
+
/** Cyclic counterpart of engine/pile.ts's isDescendingAltColor - same alternating-color check, but
|
|
20
|
+
* a step from Ace down to King is valid here, where it wouldn't be anywhere else in this app.
|
|
21
|
+
* Exported so CanfieldScreen.tsx can pass it directly as the `isValidRun` predicate to
|
|
22
|
+
* engine/pile.ts's runCountFrom (drag-run detection) and engine/autoMove.ts's pickBestTableauTarget,
|
|
23
|
+
* rather than a second, independently-typed copy of the same cyclic-run check drifting out of sync
|
|
24
|
+
* with this one - same reasoning TriPeaks' rules.ts exports childIndices for its own screen to reuse
|
|
25
|
+
* directly instead of re-deriving. */
|
|
26
|
+
declare function isDescendingAltColorCyclic(cards: Card[]): boolean;
|
|
27
|
+
/** Cyclic-foundation counterpart of engine/autoFoundation.ts's isSafeAutoPlay: a card is safe to
|
|
28
|
+
* send to its foundation once both opposite-color foundations have already placed as many cards as
|
|
29
|
+
* this card's own cyclic position - i.e. every opposite-color card that could still land on top of
|
|
30
|
+
* this one in a tableau run (cyclicPrevRank(card.rank), one position earlier in the same cycle) is
|
|
31
|
+
* already on its own foundation. Kept local to this file rather than generalizing the shared
|
|
32
|
+
* engine/autoFoundation.ts, which hardcodes Ace-start via raw rank comparison (`card.rank - 1`) -
|
|
33
|
+
* that assumption is baked into every other game here and safe to leave alone; duplicating just the
|
|
34
|
+
* cyclic-aware piece here carries zero risk to Klondike/FreeCell/Yukon's own auto-play behavior. */
|
|
35
|
+
declare function isSafeAutoPlay(state: CanfieldState, card: Card): boolean;
|
|
36
|
+
/** Cyclic-foundation counterpart of engine/autoFoundation.ts's findSafeAutoPlay - same shape, using
|
|
37
|
+
* this file's own canPlaceOnFoundationCyclic/isSafeAutoPlay instead of the shared Ace-start ones. The
|
|
38
|
+
* waste top is scanned like any other accessible card, but only a cascade the player's own play of a
|
|
39
|
+
* card to a foundation set off ever runs this, and the player's next draw or other move stops it -
|
|
40
|
+
* a card they just drew or uncovered is never played for them (see useAutoFoundationCascade). */
|
|
41
|
+
declare function findSafeAutoPlay(state: CanfieldState, accessible: AccessibleCard[]): AccessibleCard | null;
|
|
42
|
+
/** Whether the board is SOLVED, so the finish (games/shared/useAutoFinish) may file every remaining card away on its own,
|
|
43
|
+
* after the player's play of a card to a foundation: not won yet, nothing hidden and nothing left to draw - the reserve (face-down under its
|
|
44
|
+
* top), the stock (face-down) and the waste all empty, and no face-down card in the tableau (it never holds one, see
|
|
45
|
+
* newGame/tryMove; checked anyway) - and every remaining card able to go up by foundation moves alone. The last part is
|
|
46
|
+
* engine/autoFoundation.ts's plain isFullyAutoCompletable, which suits these wrapping foundations because it only ever moves
|
|
47
|
+
* a card through this game's own cyclic tryMove. Deliberately NOT the draws-aware isFullyAutoCompletableWithDraws (the
|
|
48
|
+
* screen's cascade uses that one, after a foundation play): it reads the face-down stock to decide, and the finish must
|
|
49
|
+
* never give away a card the player can't see. isWon comes first because isFullyAutoCompletable is true on a won board.
|
|
50
|
+
* The reserve and waste checks are load-bearing: the plain check plays their tops one after another, down to the cards
|
|
51
|
+
* under them (tryMove turns each reserve card over as the one above it goes, so it would count on a card nobody has
|
|
52
|
+
* seen). The stock and tableau face-down checks are belt-and-braces: the plain check can't reach isWon past either
|
|
53
|
+
* (accessibleCards never offers a stock card or a face-down column top, and tryMove never moves a stock card or turns a
|
|
54
|
+
* column card over), so they change no answer today and no test can show them doing anything. They keep the rule from
|
|
55
|
+
* resting on those two facts.
|
|
56
|
+
* `controller` is the screen's per-deal one; its draw count plays no part here. */
|
|
57
|
+
declare function canAutoFinish(controller: GameController<CanfieldState>, state: CanfieldState): boolean;
|
|
58
|
+
/** The next card the finish sends up and the foundation it goes to (its own suit's - see tryMove), or null unless the
|
|
59
|
+
* board is solved (canAutoFinish). */
|
|
60
|
+
declare function nextFinishMove(controller: GameController<CanfieldState>, state: CanfieldState): FinishMove | null;
|
|
61
|
+
declare function createCanfieldController(drawCount: 1 | 3): GameController<CanfieldState>;
|
|
62
|
+
|
|
63
|
+
export { canAutoFinish, createCanfieldController, findSafeAutoPlay, isDescendingAltColorCyclic, isSafeAutoPlay, nextFinishMove };
|
|
64
|
+
export type { CanfieldState };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { FinishMove } from './index.js';
|
|
2
|
+
import { Card, Rank, GameController, AccessibleCard } from './index.js';
|
|
3
|
+
|
|
4
|
+
interface CanfieldState {
|
|
5
|
+
/** 4 tableau columns, each dealt 1 card face up. */
|
|
6
|
+
tableau: Card[][];
|
|
7
|
+
/** 4 foundation piles, indexed to match engine/foundation's FOUNDATION_SUITS order - each must start at
|
|
8
|
+
* `foundationRank` and proceeds through a full 13-card cycle (wrapping King -> Ace). */
|
|
9
|
+
foundation: Card[][];
|
|
10
|
+
/** The rank every foundation starts from - set once, at deal, from the very first card cut from
|
|
11
|
+
* the deck (which becomes that suit's first foundation card). */
|
|
12
|
+
foundationRank: Rank;
|
|
13
|
+
/** 13 cards, only the last (top) face up - a second, single-card-at-a-time source alongside
|
|
14
|
+
* waste, with its own empty-tableau-column priority (see canPlaceOnTableau). */
|
|
15
|
+
reserve: Card[];
|
|
16
|
+
stock: Card[];
|
|
17
|
+
waste: Card[];
|
|
18
|
+
}
|
|
19
|
+
/** Cyclic counterpart of engine/pile.ts's isDescendingAltColor - same alternating-color check, but
|
|
20
|
+
* a step from Ace down to King is valid here, where it wouldn't be anywhere else in this app.
|
|
21
|
+
* Exported so CanfieldScreen.tsx can pass it directly as the `isValidRun` predicate to
|
|
22
|
+
* engine/pile.ts's runCountFrom (drag-run detection) and engine/autoMove.ts's pickBestTableauTarget,
|
|
23
|
+
* rather than a second, independently-typed copy of the same cyclic-run check drifting out of sync
|
|
24
|
+
* with this one - same reasoning TriPeaks' rules.ts exports childIndices for its own screen to reuse
|
|
25
|
+
* directly instead of re-deriving. */
|
|
26
|
+
declare function isDescendingAltColorCyclic(cards: Card[]): boolean;
|
|
27
|
+
/** Cyclic-foundation counterpart of engine/autoFoundation.ts's isSafeAutoPlay: a card is safe to
|
|
28
|
+
* send to its foundation once both opposite-color foundations have already placed as many cards as
|
|
29
|
+
* this card's own cyclic position - i.e. every opposite-color card that could still land on top of
|
|
30
|
+
* this one in a tableau run (cyclicPrevRank(card.rank), one position earlier in the same cycle) is
|
|
31
|
+
* already on its own foundation. Kept local to this file rather than generalizing the shared
|
|
32
|
+
* engine/autoFoundation.ts, which hardcodes Ace-start via raw rank comparison (`card.rank - 1`) -
|
|
33
|
+
* that assumption is baked into every other game here and safe to leave alone; duplicating just the
|
|
34
|
+
* cyclic-aware piece here carries zero risk to Klondike/FreeCell/Yukon's own auto-play behavior. */
|
|
35
|
+
declare function isSafeAutoPlay(state: CanfieldState, card: Card): boolean;
|
|
36
|
+
/** Cyclic-foundation counterpart of engine/autoFoundation.ts's findSafeAutoPlay - same shape, using
|
|
37
|
+
* this file's own canPlaceOnFoundationCyclic/isSafeAutoPlay instead of the shared Ace-start ones. The
|
|
38
|
+
* waste top is scanned like any other accessible card, but only a cascade the player's own play of a
|
|
39
|
+
* card to a foundation set off ever runs this, and the player's next draw or other move stops it -
|
|
40
|
+
* a card they just drew or uncovered is never played for them (see useAutoFoundationCascade). */
|
|
41
|
+
declare function findSafeAutoPlay(state: CanfieldState, accessible: AccessibleCard[]): AccessibleCard | null;
|
|
42
|
+
/** Whether the board is SOLVED, so the finish (games/shared/useAutoFinish) may file every remaining card away on its own,
|
|
43
|
+
* after the player's play of a card to a foundation: not won yet, nothing hidden and nothing left to draw - the reserve (face-down under its
|
|
44
|
+
* top), the stock (face-down) and the waste all empty, and no face-down card in the tableau (it never holds one, see
|
|
45
|
+
* newGame/tryMove; checked anyway) - and every remaining card able to go up by foundation moves alone. The last part is
|
|
46
|
+
* engine/autoFoundation.ts's plain isFullyAutoCompletable, which suits these wrapping foundations because it only ever moves
|
|
47
|
+
* a card through this game's own cyclic tryMove. Deliberately NOT the draws-aware isFullyAutoCompletableWithDraws (the
|
|
48
|
+
* screen's cascade uses that one, after a foundation play): it reads the face-down stock to decide, and the finish must
|
|
49
|
+
* never give away a card the player can't see. isWon comes first because isFullyAutoCompletable is true on a won board.
|
|
50
|
+
* The reserve and waste checks are load-bearing: the plain check plays their tops one after another, down to the cards
|
|
51
|
+
* under them (tryMove turns each reserve card over as the one above it goes, so it would count on a card nobody has
|
|
52
|
+
* seen). The stock and tableau face-down checks are belt-and-braces: the plain check can't reach isWon past either
|
|
53
|
+
* (accessibleCards never offers a stock card or a face-down column top, and tryMove never moves a stock card or turns a
|
|
54
|
+
* column card over), so they change no answer today and no test can show them doing anything. They keep the rule from
|
|
55
|
+
* resting on those two facts.
|
|
56
|
+
* `controller` is the screen's per-deal one; its draw count plays no part here. */
|
|
57
|
+
declare function canAutoFinish(controller: GameController<CanfieldState>, state: CanfieldState): boolean;
|
|
58
|
+
/** The next card the finish sends up and the foundation it goes to (its own suit's - see tryMove), or null unless the
|
|
59
|
+
* board is solved (canAutoFinish). */
|
|
60
|
+
declare function nextFinishMove(controller: GameController<CanfieldState>, state: CanfieldState): FinishMove | null;
|
|
61
|
+
declare function createCanfieldController(drawCount: 1 | 3): GameController<CanfieldState>;
|
|
62
|
+
|
|
63
|
+
export { canAutoFinish, createCanfieldController, findSafeAutoPlay, isDescendingAltColorCyclic, isSafeAutoPlay, nextFinishMove };
|
|
64
|
+
export type { CanfieldState };
|
package/dist/canfield.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunkXPTQNUF3js = require('./chunk-XPTQNUF3.js');
|
|
9
|
+
require('./chunk-22DC2YH6.js');
|
|
10
|
+
require('./chunk-LSS44UKH.js');
|
|
11
|
+
require('./chunk-XV5OUPIW.js');
|
|
12
|
+
require('./chunk-W5CPH6TD.js');
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
exports.canAutoFinish = _chunkXPTQNUF3js.canAutoFinish; exports.createCanfieldController = _chunkXPTQNUF3js.createCanfieldController; exports.findSafeAutoPlay = _chunkXPTQNUF3js.findSafeAutoPlay; exports.isDescendingAltColorCyclic = _chunkXPTQNUF3js.isDescendingAltColorCyclic; exports.isSafeAutoPlay = _chunkXPTQNUF3js.isSafeAutoPlay; exports.nextFinishMove = _chunkXPTQNUF3js.nextFinishMove;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
canAutoFinish,
|
|
3
|
+
createCanfieldController,
|
|
4
|
+
findSafeAutoPlay,
|
|
5
|
+
isDescendingAltColorCyclic,
|
|
6
|
+
isSafeAutoPlay,
|
|
7
|
+
nextFinishMove
|
|
8
|
+
} from "./chunk-YKY4K67K.mjs";
|
|
9
|
+
import "./chunk-3BMUGHKQ.mjs";
|
|
10
|
+
import "./chunk-SDZPP3HI.mjs";
|
|
11
|
+
import "./chunk-FDHHAPZA.mjs";
|
|
12
|
+
import "./chunk-XSS5M6C7.mjs";
|
|
13
|
+
export {
|
|
14
|
+
canAutoFinish,
|
|
15
|
+
createCanfieldController,
|
|
16
|
+
findSafeAutoPlay,
|
|
17
|
+
isDescendingAltColorCyclic,
|
|
18
|
+
isSafeAutoPlay,
|
|
19
|
+
nextFinishMove
|
|
20
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkLSS44UKHjs = require('./chunk-LSS44UKH.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
var _chunkW5CPH6TDjs = require('./chunk-W5CPH6TD.js');
|
|
10
|
+
|
|
11
|
+
// src/engine/autoFoundation.ts
|
|
12
|
+
function foundationRank(foundation, suit) {
|
|
13
|
+
return _nullishCoalesce(_optionalChain([_chunkW5CPH6TDjs.top.call(void 0, foundation[_chunkLSS44UKHjs.suitIndex.call(void 0, suit)]), 'optionalAccess', _ => _.rank]), () => ( 0));
|
|
14
|
+
}
|
|
15
|
+
function isSafeAutoPlay(foundation, card) {
|
|
16
|
+
const oppositeColor = _chunkW5CPH6TDjs.cardColor.call(void 0, card.suit) === "red" ? "black" : "red";
|
|
17
|
+
const oppositeSuits = _chunkW5CPH6TDjs.SUITS.filter((s) => _chunkW5CPH6TDjs.cardColor.call(void 0, s) === oppositeColor);
|
|
18
|
+
const oppMin = Math.min(...oppositeSuits.map((s) => foundationRank(foundation, s)));
|
|
19
|
+
return oppMin >= card.rank - 1;
|
|
20
|
+
}
|
|
21
|
+
function findSafeAutoPlay(accessible, foundation) {
|
|
22
|
+
for (const entry of accessible) {
|
|
23
|
+
if (_chunkLSS44UKHjs.canPlaceOnFoundation.call(void 0, foundation[_chunkLSS44UKHjs.suitIndex.call(void 0, entry.card.suit)], entry.card) && isSafeAutoPlay(foundation, entry.card)) {
|
|
24
|
+
return entry;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
function findNextFoundationMove(controller, state) {
|
|
30
|
+
if (!controller.accessibleCards) return null;
|
|
31
|
+
for (const entry of controller.accessibleCards(state)) {
|
|
32
|
+
const to = { zone: "foundation", index: _chunkLSS44UKHjs.suitIndex.call(void 0, entry.card.suit) };
|
|
33
|
+
if (controller.tryMove(state, { from: entry.ref, to, count: 1 })) return entry;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function isFullyAutoCompletable(controller, state) {
|
|
38
|
+
if (!controller.accessibleCards) return false;
|
|
39
|
+
let cur = state;
|
|
40
|
+
while (!controller.isWon(cur)) {
|
|
41
|
+
const entry = findNextFoundationMove(controller, cur);
|
|
42
|
+
if (!entry) return false;
|
|
43
|
+
const to = { zone: "foundation", index: _chunkLSS44UKHjs.suitIndex.call(void 0, entry.card.suit) };
|
|
44
|
+
const result = controller.tryMove(cur, { from: entry.ref, to, count: 1 });
|
|
45
|
+
if (!result) return false;
|
|
46
|
+
cur = result;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
function nextAutoCompleteMove(controller, state) {
|
|
51
|
+
return findNextFoundationMove(controller, state);
|
|
52
|
+
}
|
|
53
|
+
function findNextAutoCompleteStepWithDraws(controller, state) {
|
|
54
|
+
const playEntry = findNextFoundationMove(controller, state);
|
|
55
|
+
if (playEntry) return { kind: "play", entry: playEntry, to: { zone: "foundation", index: _chunkLSS44UKHjs.suitIndex.call(void 0, playEntry.card.suit) } };
|
|
56
|
+
if (!controller.drawStock) return null;
|
|
57
|
+
const afterDraw = controller.drawStock(state);
|
|
58
|
+
if (!afterDraw) return null;
|
|
59
|
+
const drawEntry = findNextFoundationMove(controller, afterDraw);
|
|
60
|
+
if (drawEntry) return { kind: "drawAndPlay", entry: drawEntry, to: { zone: "foundation", index: _chunkLSS44UKHjs.suitIndex.call(void 0, drawEntry.card.suit) } };
|
|
61
|
+
return { kind: "draw" };
|
|
62
|
+
}
|
|
63
|
+
function isFullyAutoCompletableWithDraws(controller, state) {
|
|
64
|
+
if (!controller.accessibleCards || !controller.drawStock) return false;
|
|
65
|
+
const seen = /* @__PURE__ */ new Set();
|
|
66
|
+
let cur = state;
|
|
67
|
+
while (!controller.isWon(cur)) {
|
|
68
|
+
const step = findNextAutoCompleteStepWithDraws(controller, cur);
|
|
69
|
+
if (!step) return false;
|
|
70
|
+
if (step.kind === "play") {
|
|
71
|
+
cur = controller.tryMove(cur, { from: step.entry.ref, to: step.to, count: 1 });
|
|
72
|
+
} else if (step.kind === "drawAndPlay") {
|
|
73
|
+
const afterDraw = controller.drawStock(cur);
|
|
74
|
+
cur = controller.tryMove(afterDraw, { from: step.entry.ref, to: step.to, count: 1 });
|
|
75
|
+
} else {
|
|
76
|
+
const key = JSON.stringify(cur);
|
|
77
|
+
if (seen.has(key)) return false;
|
|
78
|
+
seen.add(key);
|
|
79
|
+
cur = controller.drawStock(cur);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
function nextAutoCompleteMoveWithDraws(controller, state) {
|
|
85
|
+
return findNextAutoCompleteStepWithDraws(controller, state);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
exports.isSafeAutoPlay = isSafeAutoPlay; exports.findSafeAutoPlay = findSafeAutoPlay; exports.isFullyAutoCompletable = isFullyAutoCompletable; exports.nextAutoCompleteMove = nextAutoCompleteMove; exports.isFullyAutoCompletableWithDraws = isFullyAutoCompletableWithDraws; exports.nextAutoCompleteMoveWithDraws = nextAutoCompleteMoveWithDraws;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {
|
|
2
|
+
canPlaceOnFoundation,
|
|
3
|
+
suitIndex
|
|
4
|
+
} from "./chunk-SDZPP3HI.mjs";
|
|
5
|
+
import {
|
|
6
|
+
SUITS,
|
|
7
|
+
cardColor,
|
|
8
|
+
top
|
|
9
|
+
} from "./chunk-XSS5M6C7.mjs";
|
|
10
|
+
|
|
11
|
+
// src/engine/autoFoundation.ts
|
|
12
|
+
function foundationRank(foundation, suit) {
|
|
13
|
+
return top(foundation[suitIndex(suit)])?.rank ?? 0;
|
|
14
|
+
}
|
|
15
|
+
function isSafeAutoPlay(foundation, card) {
|
|
16
|
+
const oppositeColor = cardColor(card.suit) === "red" ? "black" : "red";
|
|
17
|
+
const oppositeSuits = SUITS.filter((s) => cardColor(s) === oppositeColor);
|
|
18
|
+
const oppMin = Math.min(...oppositeSuits.map((s) => foundationRank(foundation, s)));
|
|
19
|
+
return oppMin >= card.rank - 1;
|
|
20
|
+
}
|
|
21
|
+
function findSafeAutoPlay(accessible, foundation) {
|
|
22
|
+
for (const entry of accessible) {
|
|
23
|
+
if (canPlaceOnFoundation(foundation[suitIndex(entry.card.suit)], entry.card) && isSafeAutoPlay(foundation, entry.card)) {
|
|
24
|
+
return entry;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
function findNextFoundationMove(controller, state) {
|
|
30
|
+
if (!controller.accessibleCards) return null;
|
|
31
|
+
for (const entry of controller.accessibleCards(state)) {
|
|
32
|
+
const to = { zone: "foundation", index: suitIndex(entry.card.suit) };
|
|
33
|
+
if (controller.tryMove(state, { from: entry.ref, to, count: 1 })) return entry;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function isFullyAutoCompletable(controller, state) {
|
|
38
|
+
if (!controller.accessibleCards) return false;
|
|
39
|
+
let cur = state;
|
|
40
|
+
while (!controller.isWon(cur)) {
|
|
41
|
+
const entry = findNextFoundationMove(controller, cur);
|
|
42
|
+
if (!entry) return false;
|
|
43
|
+
const to = { zone: "foundation", index: suitIndex(entry.card.suit) };
|
|
44
|
+
const result = controller.tryMove(cur, { from: entry.ref, to, count: 1 });
|
|
45
|
+
if (!result) return false;
|
|
46
|
+
cur = result;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
function nextAutoCompleteMove(controller, state) {
|
|
51
|
+
return findNextFoundationMove(controller, state);
|
|
52
|
+
}
|
|
53
|
+
function findNextAutoCompleteStepWithDraws(controller, state) {
|
|
54
|
+
const playEntry = findNextFoundationMove(controller, state);
|
|
55
|
+
if (playEntry) return { kind: "play", entry: playEntry, to: { zone: "foundation", index: suitIndex(playEntry.card.suit) } };
|
|
56
|
+
if (!controller.drawStock) return null;
|
|
57
|
+
const afterDraw = controller.drawStock(state);
|
|
58
|
+
if (!afterDraw) return null;
|
|
59
|
+
const drawEntry = findNextFoundationMove(controller, afterDraw);
|
|
60
|
+
if (drawEntry) return { kind: "drawAndPlay", entry: drawEntry, to: { zone: "foundation", index: suitIndex(drawEntry.card.suit) } };
|
|
61
|
+
return { kind: "draw" };
|
|
62
|
+
}
|
|
63
|
+
function isFullyAutoCompletableWithDraws(controller, state) {
|
|
64
|
+
if (!controller.accessibleCards || !controller.drawStock) return false;
|
|
65
|
+
const seen = /* @__PURE__ */ new Set();
|
|
66
|
+
let cur = state;
|
|
67
|
+
while (!controller.isWon(cur)) {
|
|
68
|
+
const step = findNextAutoCompleteStepWithDraws(controller, cur);
|
|
69
|
+
if (!step) return false;
|
|
70
|
+
if (step.kind === "play") {
|
|
71
|
+
cur = controller.tryMove(cur, { from: step.entry.ref, to: step.to, count: 1 });
|
|
72
|
+
} else if (step.kind === "drawAndPlay") {
|
|
73
|
+
const afterDraw = controller.drawStock(cur);
|
|
74
|
+
cur = controller.tryMove(afterDraw, { from: step.entry.ref, to: step.to, count: 1 });
|
|
75
|
+
} else {
|
|
76
|
+
const key = JSON.stringify(cur);
|
|
77
|
+
if (seen.has(key)) return false;
|
|
78
|
+
seen.add(key);
|
|
79
|
+
cur = controller.drawStock(cur);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
function nextAutoCompleteMoveWithDraws(controller, state) {
|
|
85
|
+
return findNextAutoCompleteStepWithDraws(controller, state);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export {
|
|
89
|
+
isSafeAutoPlay,
|
|
90
|
+
findSafeAutoPlay,
|
|
91
|
+
isFullyAutoCompletable,
|
|
92
|
+
nextAutoCompleteMove,
|
|
93
|
+
isFullyAutoCompletableWithDraws,
|
|
94
|
+
nextAutoCompleteMoveWithDraws
|
|
95
|
+
};
|