games/solitaire: FreeCell auto-move only names targets the move accepts
- Commit
d4b3efa3a139b4edcbbc6206c1692ce88abeaae1- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 22:03 UTC (5 days ago)
- Parents
2585be5
Tapping a 3-card run auto-moved nowhere even when a legal landing existed: the resolver checked only landing colour/rank and picked an empty column whose supermove capacity (2 with one free cell occupied) couldn't take the 3-run; the move then rejected the drop and the tap did nothing. The contract promises 'resolves the best legal destination, a no-op when there is no legal move' โ here a legal move existed onto the 9 of hearts (capacity 4). FreeCell legality now lives in one shared freeCellMaxMove used by both the move and the resolver, and viable targets also require a valid sequence within capacity. No rule changed โ the resolver now only names targets the move would accept. New regression test failed pre-fix (moveCount 0) and passes now. Win conditions, stock/redeal, Spider/Pyramid and undo were audited and cleared. Verified: :games:solitaire:testDevUnitTest BUILD SUCCESSFUL; lint passes, 0 errors.
Diff
games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireFreeCellOps.kt
diff --git a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireFreeCellOps.kt b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireFreeCellOps.ktindex b153196e24..6f000374c9 100644--- a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireFreeCellOps.kt+++ b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireFreeCellOps.kt@@ -123,14 +123,7 @@ internal fun SolitaireViewModel.freeCellMoveTableauToTableauImpl(fromColumn: Int val movingCards = fromPile.subList(cardIndex, fromPile.size) if (!isFreeCellSequence(movingCards)) return val toPile = state.tableauPiles[toColumn]- if (movingCards.size > 1) {- val emptyFreeCells = state.freeCells.count { it == null }- val emptyColumns = state.tableauPiles.indices.count {- it != fromColumn && it != toColumn && state.tableauPiles[it].isEmpty()- }- val maxMove = (1 + emptyFreeCells) * (1 shl emptyColumns)- if (movingCards.size > maxMove) return- }+ if (movingCards.size > 1 && movingCards.size > freeCellMaxMove(state, fromColumn, toColumn)) return if (!canPlaceOnFreeCellTableau(movingCards.first(), toPile)) return saveHistory() val newPiles = state.tableauPiles.toMutableList()@@ -272,6 +265,22 @@ private fun isLoneCardChurn( return state.tableauPiles[origin.fromCol].size == SINGLE_CARD } +/**+ * The supermove limit for moving a run from [fromColumn] onto [toColumn]:+ * `(1 + empty free cells) * 2^empty columns`, with empty columns other than+ * the source and the target as buffers (the target column itself, empty or+ * not, can never hold a card mid-move). Shared by the move itself and by+ * auto-move target resolution, so a target is only ever resolved to when the+ * move it names would actually be accepted.+ */+private fun freeCellMaxMove(state: FreeCellState, fromColumn: Int, toColumn: Int): Int {+ val emptyFreeCells = state.freeCells.count { it == null }+ val emptyColumns = state.tableauPiles.indices.count {+ it != fromColumn && it != toColumn && state.tableauPiles[it].isEmpty()+ }+ return (1 + emptyFreeCells) * (1 shl emptyColumns)+}+ private fun SolitaireViewModel.isViableTableauTarget( state: FreeCellState, cards: List<Card>,@@ -280,6 +289,13 @@ private fun SolitaireViewModel.isViableTableauTarget( ): Boolean { val toPile = state.tableauPiles[toCol] if (!canPlaceOnFreeCellTableau(cards.first(), toPile)) return false+ // The run must be a legal supermove for this target, not just landable:+ // an empty target cannot serve as its own buffer, so a run that fits+ // onto a card (other empties counting) may exceed the limit there, and+ // resolving to it would strand the tap even though a legal move exists.+ if (!isFreeCellSequence(cards)) return false+ val fromCol = origin.fromCol+ if (fromCol != null && cards.size > 1 && cards.size > freeCellMaxMove(state, fromCol, toCol)) return false // Skip moving a lone card between empty columns โ pointless churn. return !isLoneCardChurn(state, cards, origin, toPile) }
games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/OpsAndRepositoriesCoverageTest.kt
diff --git a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/OpsAndRepositoriesCoverageTest.kt b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/OpsAndRepositoriesCoverageTest.ktindex 59c63606c4..6e1acf3323 100644--- a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/OpsAndRepositoriesCoverageTest.kt+++ b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/OpsAndRepositoriesCoverageTest.kt@@ -178,6 +178,37 @@ class OpsAndRepositoriesCoverageTest { assertEquals(2, vm.uiState.value.freeCell!!.tableauPiles[0].size) } + @Test+ fun freeCellAutoMoveSkipsEmptyColumnBeyondSupermoveCapacity() {+ val vm = vm()+ // A 3-card run (8-7-6) with one free cell: onto the empty column the+ // supermove limit is (1+1)*2^0 = 2, so that move is illegal; onto the+ // red 9 the empty column counts as a buffer, (1+1)*2^1 = 4, and the+ // same run is legal. Auto-move must skip the doomed empty column.+ setF(vm, FreeCellState(+ tableauPiles = listOf(+ listOf(c(Suit.SPADES, Rank.EIGHT), c(Suit.HEARTS, Rank.SEVEN), c(Suit.SPADES, Rank.SIX)),+ emptyList(),+ listOf(c(Suit.HEARTS, Rank.NINE)),+ listOf(c(Suit.CLUBS, Rank.TWO)),+ listOf(c(Suit.DIAMONDS, Rank.THREE)),+ listOf(c(Suit.CLUBS, Rank.FOUR)),+ listOf(c(Suit.DIAMONDS, Rank.FIVE)),+ listOf(c(Suit.CLUBS, Rank.TEN)),+ ),+ freeCells = listOf(c(Suit.SPADES, Rank.KING), c(Suit.HEARTS, Rank.KING), c(Suit.CLUBS, Rank.QUEEN), null),+ ))+ vm.autoMove("tableau_0_0")+ val f = vm.uiState.value.freeCell!!+ assertEquals(1, f.moveCount)+ assertTrue(f.tableauPiles[0].isEmpty())+ assertTrue(f.tableauPiles[1].isEmpty())+ assertEquals(+ listOf(c(Suit.HEARTS, Rank.NINE), c(Suit.SPADES, Rank.EIGHT), c(Suit.HEARTS, Rank.SEVEN), c(Suit.SPADES, Rank.SIX)),+ f.tableauPiles[2],+ )+ }+ @Test fun freeCellAutoMovePrefersFoundationThenColumnThenCell() { val vm = vm()