games/solitaire: Klondike foundation cards can tap-to-move back down
- Commit
821988af4a41b834e9ea8275845ff3af823c4d03- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 02:51 UTC (5 days ago)
- Parents
cea5da8
Moving a foundation's top card back onto the tableau is
legal and the drag path supported it, but tap-to-move
(autoMove / the TalkBack auto-move action) silently did
nothing: klondikeMoveOrigin only understood waste and
tableau_* sources and returned null for foundation_*, so
the resolver bailed before searching tableau targets even
though draggedCards() already supports foundation sources.
With Hearts A-5 founded and 6S on the tableau, drag accepts
5H -> 6S while a tap returned false — against the
documented contract ('resolves the best legal destination;
returns false when there is no legal move').
Foundation sources now resolve like the waste (no column to
exclude, and a founded King may still land on an empty
column, having left the tableau by going up), and the
resolver skips the foundation-first search for them. Rule
predicates are untouched, so solver-proven seed tables stay
valid. Three regression tests cover the instant move + undo,
the animated flight and the King/non-King empty-column cases
(the first failed pre-fix); the old test comment only ever
exercised a no-legal-landing board. All four variants'
legality, auto-complete, win detection, undo and deal tables
were audited (incl. 4,000-step fuzz probes) and cleared.
Verified: :games:solitaire:testDevUnitTest 183 tests and
lint pass.Diff
games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireKlondikeOps.kt
diff --git a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireKlondikeOps.kt b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireKlondikeOps.ktindex cd86bc0b07..d91744cdac 100644--- a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireKlondikeOps.kt+++ b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireKlondikeOps.kt@@ -274,8 +274,10 @@ internal fun SolitaireViewModel.resolveKlondikeAutoMoveTarget(sourceId: String): val cards = draggedCards(sourceId) if (cards.isEmpty()) return null val origin = klondikeMoveOrigin(sourceId) ?: return null- // Only a single top card can go up, and it is the preferred move.- if (cards.size == SINGLE_CARD) {+ // Only a single top card can go up, and it is the preferred move. A card+ // already on a foundation cannot go up, so it only looks for a tableau+ // landing (the drag path down, which tap-to-move must match).+ if (cards.size == SINGLE_CARD && !sourceId.startsWith(FOUNDATION_PREFIX)) { for (fi in state.foundations.indices) { if (canPlaceOnFoundation(cards.first(), state.foundations[fi], fi)) return "foundation_$fi" }@@ -289,6 +291,9 @@ private data class KlondikeMoveOrigin(val fromCol: Int?, val fromIdx: Int) private fun klondikeMoveOrigin(sourceId: String): KlondikeMoveOrigin? { if (sourceId == "waste") return KlondikeMoveOrigin(fromCol = null, fromIdx = 0)+ // A foundation top has no column to exclude and is never a lone-King+ // shuffle (it left the tableau by going up), so it resolves like the waste.+ if (sourceId.startsWith(FOUNDATION_PREFIX)) return KlondikeMoveOrigin(fromCol = null, fromIdx = 0) if (!sourceId.startsWith("tableau_")) return null val parts = sourceId.removePrefix("tableau_").split("_") return KlondikeMoveOrigin(
games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/FoundationToTableauTest.kt
diff --git a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/FoundationToTableauTest.kt b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/FoundationToTableauTest.ktindex 6369f2613a..222cec7918 100644--- a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/FoundationToTableauTest.kt+++ b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/FoundationToTableauTest.kt@@ -94,10 +94,52 @@ class FoundationToTableauTest { assertEquals(FlightTarget.Point(Offset(1f, 2f)), vm.flights.value.single().target) } + @Test fun tapAutoMovesFoundationCardDownToTableau() {+ val six = Card(Suit.SPADES, Rank.SIX)+ val five = Card(Suit.HEARTS, Rank.FIVE)+ val vm = vm(state(TableauPile(faceUp = listOf(six))))+ // The drag down is legal (see dragDownIsANormalMoveWithUndo), so the+ // tap-to-move resolver must name the same tableau target.+ vm.autoMove("foundation_0")+ val k = vm.uiState.value.klondike!!+ assertEquals(listOf(six, five), k.tableauPiles[0].faceUp)+ assertEquals(4, k.foundations[0].size)+ assertEquals(1, k.moveCount)+ assertEquals(1, vm.uiState.value.history.size)+ vm.undo()+ assertEquals(5, vm.uiState.value.klondike!!.foundations[0].size)+ assertEquals(listOf(six), vm.uiState.value.klondike!!.tableauPiles[0].faceUp)+ }++ @Test fun tapFlightFoundationCardDownToTableau() {+ val six = Card(Suit.SPADES, Rank.SIX)+ val vm = vm(state(TableauPile(faceUp = listOf(six))))+ assertTrue(vm.requestAutoMove("foundation_0", Offset.Zero, size))+ val k = vm.uiState.value.klondike!!+ assertEquals(listOf(six, Card(Suit.HEARTS, Rank.FIVE)), k.tableauPiles[0].faceUp)+ assertEquals(FlightTarget.Slot("tableau_0", 0, 1), vm.flights.value.single().target)+ }++ @Test fun tapAutoMovesFoundationKingToEmptyColumnOnly() {+ val kingUp = state().copy(+ foundations = listOf(Rank.entries.map { Card(Suit.HEARTS, it) }) + foundations.drop(1),+ )+ val vm = vm(kingUp)+ vm.autoMove("foundation_0")+ val k = vm.uiState.value.klondike!!+ assertEquals(listOf(Card(Suit.HEARTS, Rank.KING)), k.tableauPiles[0].faceUp)+ assertEquals(12, k.foundations[0].size)+ // A non-King foundation card has no landing on an empty column.+ val vm2 = vm(state())+ assertFalse(vm2.requestAutoMove("foundation_0", Offset.Zero, size))+ assertEquals(5, vm2.uiState.value.klondike!!.foundations[0].size)+ assertEquals(0, vm2.uiState.value.klondike!!.moveCount)+ }+ @Test fun emptyFoundationAndFreeCellFoundationsCannotBeDragged() { val vm = vm(state()) assertFalse(vm.startDrag("foundation_1", Offset.Zero, size))- // Tap on a foundation card has no move to make (unchanged behaviour).+ // No legal landing here (empty columns take only a King, and 5♥ is not one). assertFalse(vm.requestAutoMove("foundation_0", Offset.Zero, size)) vm.uiStateInternal.value = SolitaireUiState( gameMode = GameMode.FREECELL,