games: Klondike tableau moves validate the carried run is a real sequence
- Commit
c40ca9c3b96010d63f01f1f576adbd5c78628d7a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 13:59 UTC (4 days ago)
- Parents
da05708
klondikeMoveTableauToTableau checked only the carried run's leading card against the target: column [9S, 8S] (a same-colour pair, not a movable Klondike sequence) onto 10H was accepted. FreeCell and Spider enforce full-run shape at exactly this layer; Klondike had no sequence check anywhere, and no other layer validates a pile. isKlondikeSequence (descending by one, alternating colours) now gates the move impl (direct, drag, TalkBack and tap all funnel through it) and the tap-to-move viability resolver, so the resolver only names targets the move accepts. Legal moves are unchanged — solver winning lines only ever move valid sequences, so the proven-seed tables stay valid without regeneration (Solvable-deals note in AGENTS.md considered; SolverReplayTest passes over shipped tables). Three regression tests (illegal move, drag/tap paths, legal control) went red pre-fix on the illegal ones. The other variants and auto-complete were fuzz/reference-checked and cleared. Verified: :games:solitaire:testDevUnitTest 190 tests and lint BUILD SUCCESSFUL.
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 d91744cdac..131ccbdbef 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@@ -136,6 +136,7 @@ internal fun SolitaireViewModel.klondikeMoveTableauToTableauImpl(fromColumn: Int val fromPile = state.tableauPiles[fromColumn] if (cardIndex < 0 || cardIndex >= fromPile.faceUp.size) return val movingCards = fromPile.faceUp.subList(cardIndex, fromPile.faceUp.size)+ if (!isKlondikeSequence(movingCards)) return val topCard = movingCards.first() val toPile = state.tableauPiles[toColumn] if (!canPlaceOnKlondikeTableau(topCard, toPile)) return@@ -189,6 +190,17 @@ private fun remainingAfterRecycle(redealsRemaining: Int): Int = internal fun SolitaireViewModel.canPlaceOnKlondikeTableau(card: Card, pile: TableauPile): Boolean = canPlaceCardOnKlondikeTableau(card, pile) +/**+ * A movable Klondike run: descending by one rank with alternating colours+ * (the same shape FreeCell and Spider demand of their moving runs). Tableau+ * piles built by legal play always satisfy this, but the move itself must+ * enforce it — a pile read back from a saved state is not sequence-checked+ * anywhere else, so this guard is the only thing standing between a stated+ * non-sequence (e.g. 9♠ on 8♠) and an illegal multi-card move.+ */+internal fun SolitaireViewModel.isKlondikeSequence(cards: List<Card>): Boolean =+ cards.zipWithNext().all { (a, b) -> a.alternatesColorWith(b) && a.isOneHigherThan(b) }+ /** Klondike tableau rule: a King on an empty column, else alternate colour one rank lower. Pure. */ fun canPlaceCardOnKlondikeTableau(card: Card, pile: TableauPile): Boolean = when { pile.faceUp.isEmpty() && pile.faceDown.isEmpty() -> card.rank == Rank.KING@@ -320,6 +332,9 @@ private fun SolitaireViewModel.isViableKlondikeTarget( ): Boolean { val toPile = state.tableauPiles[toCol] if (!canPlaceOnKlondikeTableau(cards.first(), toPile)) return false+ // Only ever name a target the move itself would accept (a non-sequence+ // run is not movable, whatever its first card fits).+ if (!isKlondikeSequence(cards)) return false // Skip shuffling a lone King between empty columns — it accomplishes nothing. return !isLoneKingShuffle(state, origin, toPile) }
games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/KlondikeTableauSequenceTest.kt
diff --git a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/KlondikeTableauSequenceTest.kt b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/KlondikeTableauSequenceTest.ktnew file mode 100644index 0000000000..1fc47d87f6--- /dev/null+++ b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/KlondikeTableauSequenceTest.kt@@ -0,0 +1,120 @@+package com.vayunmathur.games.solitaire.platform++import android.app.Application+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.geometry.Rect+import androidx.compose.ui.unit.IntSize+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.games.solitaire.data.Card+import com.vayunmathur.games.solitaire.data.GameMode+import com.vayunmathur.games.solitaire.data.KlondikeState+import com.vayunmathur.games.solitaire.data.Rank+import com.vayunmathur.games.solitaire.data.SolitaireUiState+import com.vayunmathur.games.solitaire.data.Suit+import com.vayunmathur.games.solitaire.data.TableauPile+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * Klondike tableau-to-tableau may only carry a descending alternating-colour+ * sequence (the same rule FreeCell and Spider enforce on their moving runs).+ * A run that is not such a sequence must be rejected on every entry path:+ * the direct move, a drag, and tap-to-move.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class KlondikeTableauSequenceTest {++ private val app = ApplicationProvider.getApplicationContext<Application>()+ private fun vm() = SolitaireViewModel(app)+ private fun c(suit: Suit, rank: Rank) = Card(suit, rank)++ /** Col 0 holds [run] face-up; col 1 holds the red Ten it would land on. */+ private fun stateWith(run: List<Card>) = KlondikeState(+ tableauPiles = listOf(+ TableauPile(faceUp = run),+ TableauPile(faceUp = listOf(c(Suit.HEARTS, Rank.TEN))),+ ) + List(5) { TableauPile() },+ )++ private fun setK(vm: SolitaireViewModel, state: KlondikeState) {+ vm.uiStateInternal.value = SolitaireUiState(gameMode = GameMode.KLONDIKE, klondike = state)+ }++ @Test fun nonSequenceRunsAreRejectedByTheDirectMove() {+ // Same-colour descent (9S on 8S) is not a movable Klondike sequence,+ // even though its first card fits the red Ten.+ val vm = vm()+ val state = stateWith(listOf(c(Suit.SPADES, Rank.NINE), c(Suit.SPADES, Rank.EIGHT)))+ setK(vm, state)+ vm.klondikeMoveTableauToTableau(0, 0, 1)+ assertEquals(state, vm.uiState.value.klondike)+ assertTrue(vm.uiState.value.history.isEmpty())++ // Non-descending run (8S under 9H, grabbed at the Eight) also rejected.+ val vm2 = vm()+ val state2 = stateWith(listOf(c(Suit.SPADES, Rank.EIGHT), c(Suit.HEARTS, Rank.NINE)))+ setK(vm2, state2)+ vm2.klondikeMoveTableauToTableau(0, 0, 1)+ assertEquals(state2, vm2.uiState.value.klondike)++ // Same-colour pair through the other black suit.+ val vm3 = vm()+ val state3 = stateWith(listOf(c(Suit.SPADES, Rank.NINE), c(Suit.CLUBS, Rank.EIGHT)))+ setK(vm3, state3)+ vm3.klondikeMoveTableauToTableau(0, 0, 1)+ assertEquals(state3, vm3.uiState.value.klondike)+ }++ @Test fun nonSequenceRunIsRejectedByDragAndTapToMove() {+ val run = listOf(c(Suit.SPADES, Rank.NINE), c(Suit.SPADES, Rank.EIGHT))+ val state = stateWith(run)++ val dragVm = vm()+ setK(dragVm, state)+ dragVm.dropTargets["tableau_1"] = Rect(0f, 0f, 100f, 100f)+ dragVm.tryMoveByDrag("tableau_0_0", Offset(50f, 50f), IntSize(50, 50))+ assertEquals(state, dragVm.uiState.value.klondike)++ // Tap-to-move must not name the tableau target either.+ val tapVm = vm()+ setK(tapVm, state)+ tapVm.autoMove("tableau_0_0")+ assertEquals(state, tapVm.uiState.value.klondike)+ }++ @Test fun legalAlternatingSequenceStillMoves() {+ val vm = vm()+ val down = c(Suit.CLUBS, Rank.THREE)+ val state = KlondikeState(+ tableauPiles = listOf(+ TableauPile(+ faceDown = listOf(down),+ faceUp = listOf(c(Suit.SPADES, Rank.NINE), c(Suit.HEARTS, Rank.EIGHT)),+ ),+ TableauPile(faceUp = listOf(c(Suit.HEARTS, Rank.TEN))),+ ) + List(5) { TableauPile() },+ )+ setK(vm, state)+ vm.klondikeMoveTableauToTableau(0, 0, 1)+ val k = vm.uiState.value.klondike!!+ assertEquals(1, k.moveCount)+ assertEquals(+ listOf(c(Suit.HEARTS, Rank.TEN), c(Suit.SPADES, Rank.NINE), c(Suit.HEARTS, Rank.EIGHT)),+ k.tableauPiles[1].faceUp,+ )+ // Source auto-flips as before.+ assertEquals(listOf(down), k.tableauPiles[0].faceUp)+ assertTrue(k.tableauPiles[0].faceDown.isEmpty())++ // A single top card is a one-card sequence and still moves.+ val vm2 = vm()+ setK(vm2, stateWith(listOf(c(Suit.SPADES, Rank.NINE))))+ vm2.klondikeMoveTableauToTableau(0, 0, 1)+ assertEquals(1, vm2.uiState.value.klondike!!.moveCount)+ }+}