Play5sDeveloper notes

One Tap, Best Place: Solitaire Auto-Move and Finish Logic

A tap on a Play5s solitaire card sends it to the first place that makes sense, and the Finish button appears only when the rest of the deal is certain to play out. Klondike tries the foundation and then the columns. FreeCell adds a free cell and an empty column as last resorts, and both games refuse moves that only shuffle a card sideways. Finish checks the deal before it shows, then plays one card every 90 milliseconds.

This note covers the Klondike and FreeCell helpers. Yukon and Forty Thieves also have a tap helper and a Finish button, built the same way.

What does a tap try first?

Klondike: a tap tries1a foundation, for a single card2the first column that accepts it(empty columns come last)no place: the card shakes, refusal soundFreeCell: a tap tries1a foundation, for a single card2a column it builds on3a free cell (single card, not alone)4an empty column (not alone)no place: shake and refusal soundFinish shows only when the deal cannot be lostKlondikestock and waste empty,every column face upYukonno face-down cards,columns in falling rankFreeCellfoundation moves alonewould win (simulated)Then Finish plays one card every 90 ms through the normal move path.
Klondike: a tap tries1a foundation, for a single card2the first column that accepts itno place: shake and refusal soundFreeCell: a tap tries1a foundation, for a single card2a column it builds on3a free cell (single card, not alone)4an empty column (not alone)no place: shake and refusal soundFinish shows only when the deal cannot be lostKlondikestock and waste empty, every column face upYukonno face-down cards, columns in falling rankFreeCellfoundation moves alone would win (simulated)Then one card every 90 ms.
What a tap tries, in order, in the two games. The bottom panel is the test for the Finish button: a deal that cannot be lost, not merely a deal that looks tidy.

When a press ends without travelling, as the note on dragging describes, the game calls autoDest with the card's position. In Klondike the function asks two questions in order.

First, if the card is a single card that does not already sit on a foundation, can its suit's foundation take it? If so, that is the answer. Second, which of the seven columns accepts it? The columns are tried in index order with the empty ones moved to the end, so a card that fits on a real column never takes an empty one by accident. The column the card came from is skipped. If neither question has an answer, the function returns nothing, the card shakes and the soft refusal sound plays.

A tap is a small decision, so the ladder is short on purpose. The player can still drag a card anywhere legal. The tap handles the common case, a card with exactly one good place.

Why does FreeCell need a longer ladder?

FreeCell has two more kinds of place: four free cells and eight columns that can empty out. Its autoDest walks four steps:

// 1. a single card goes home if its foundation takes it
// 2. otherwise the first non-empty column it builds on
// 3. otherwise a free cell, but only for a single card from a column,
//    and not when it is alone in its column
// 4. otherwise an empty column (also not when the card is alone)

The third step carries the rule that matters. A lone card in its column would only trade the column for a cell, so the helper refuses to park it. Columns are the scarce resource in FreeCell. Wikipedia's article on FreeCell gives the formula: a move can carry the number of empty cells plus one cards, doubled for each empty column. The engine encodes it in maxRun, as (free cells + 1) times 2 to the power of the empty columns, with one empty column fewer counted when the destination is itself an empty column. A tap on the head of a run therefore moves the whole run, and does nothing when the run is too long for the free cells.

The tap ladder in the two games
StepKlondikeFreeCell
1Foundation, for a single cardFoundation, for a single card
2First column that accepts, empty columns lastFirst non-empty column the card builds on
3Nothing: shake and refusal soundA free cell, for a single card that is not alone in its column
4NoneAn empty column, unless the card is alone in its own

Which moves does the helper refuse on purpose?

Several legal moves change nothing a player can see, and the helpers filter them out. In Klondike a king that already sits at the base of a column is not moved to another empty column. In FreeCell a whole column is not moved into an empty column, because the board would look the same with the move counter one higher. A card never moves from one cell to another, or from a column onto itself. FreeCell's move() returns nothing for the last three, so even a drag cannot make them. The Klondike king case is filtered only in the helpers, which is enough, since nobody drags a king there by accident.

The keyboard path repeats the king filter in validTargets, with a code comment that a king at the bottom of a column gains nothing. A tap and the arrow keys offer the same set of moves, so a player who learns the rules on one input never meets a different rule on another.

There is a human reason too. A helper that proposes a pointless move teaches the player to distrust the helper, and a counter that rises for no reason makes a tidy game look untidy. Refusing quietly is cheaper than explaining.

When does the Finish button appear?

The button is hidden until the game can prove the rest is mechanical. The test is different in each game, because the games lose in different ways.

Klondike asks three things: the game is not already won, the stock and the waste are both empty, and every column is face up, or empty. In Klondike the face-up cards of a column always form a descending run of alternating colours, since only legal moves build them. With nothing hidden and nothing left to turn over, the lowest card still on the table is therefore the bottom of its column, and its foundation is ready for it. That repeats until the table is clear, so the deal can no longer be lost.

Yukon cannot lean on that argument, because it lets a player move any face-up group. Its test adds the missing piece and checks that each column is in descending rank order from top to bottom.

FreeCell does not reason about shapes at all. It simulates. On copies of the foundations, the columns and the cells, it repeatedly sends any cell card or any column's bottom card to its foundation when the rank is next, and stops when nothing moves. The button shows only if all four foundations reach 13. Finish therefore never needs a free-cell trick: if the foundation moves alone cannot win, the button stays hidden and the player keeps playing.

The check runs on every render, through finishBtn.hidden = !canFinish(state). It is cheap, because the table never holds more than 52 cards.

How does Finish play the cards?

finishStep returns the state after the first card that can go home: Klondike and Yukon scan the columns left to right, FreeCell looks at the cells first and then the columns. The interface calls it in a loop with a 90 millisecond pause, setTimeout(step, 90), and each step goes through the same commit path as a hand-made move. The cards fly one at a time, the move counter climbs, and the bell sound from the sound note rings for each card that goes home.

Pausing is a feature. A finish that played in a single frame would look like a glitch, and the pause gives the player a moment to see which cards went where. The player can still read the table while it runs, since every step leaves a valid state behind.

Each of the four solitaire games with a Finish button has its own version of the tap ladder and the Finish loop, built the same way, which is why the two buttons feel alike in Klondike, FreeCell, Yukon and Forty Thieves. The next note, how a solitaire Hint picks a move, uses the same engine functions in a different order.