games-solitaire: fix free-cell drop moving wrong card
- Commit
6df92529aec5f895daea4e21220e144317d2cc45- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:02 UTC (29 days ago)
- Parents
05b6fca
Symptom: in FreeCell, dragging a multi-card tableau stack and dropping it on a free cell moved the pile's top card into the cell, even though the player grabbed the whole stack (e.g. the bottom card of the run). Root cause: handleFreeCellDropImpl dispatched every tableau drop on a freecell target straight to freeCellMoveToFreeCellImpl, which moves pile.last(). The foundation branch of the same handler guards with isSingleCardDrag; the free-cell branch did not, so a stack drag landed a card the player never grabbed. Fix: require isSingleCardDrag(sourceId) for the freecell branch, matching the foundation branch. Single top-card drags onto a cell behave exactly as before; multi-card drags onto a cell are now ignored instead of moving a different card. Verification: new regression test freeCellDropRejectsMultiCardDragOntoCell in DragAndViewModelTest (stack drag is a no-op, top-card drag still lands in the cell). Ran :games:solitaire:testDevUnitTest for DragAndViewModelTest and SpiderPyramidOpsTest: BUILD SUCCESSFUL.
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 1209770e02..a16c4bb848 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@@ -182,7 +182,11 @@ internal fun SolitaireViewModel.handleFreeCellDropImpl(sourceId: String, targetI when { targetId.startsWith("freecell_") -> { val ci = targetId.removePrefix("freecell_").toInt()- if (sourceId.startsWith("tableau_")) {+ // A free cell holds one card, so only a single-card drag may+ // land here. Without this guard a multi-card drag dropped on+ // a cell silently moved the pile's top card instead of the+ // stack the player actually grabbed.+ if (isSingleCardDrag(sourceId) && sourceId.startsWith("tableau_")) { val col = sourceId.removePrefix("tableau_").substringBefore("_").toInt() freeCellMoveToFreeCell(col, ci) }
games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/DragAndViewModelTest.kt
diff --git a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/DragAndViewModelTest.kt b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/DragAndViewModelTest.ktindex ca878ccd5f..c6c8bf49e5 100644--- a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/DragAndViewModelTest.kt+++ b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/DragAndViewModelTest.kt@@ -201,6 +201,32 @@ class DragAndViewModelTest { assertEquals(1, f.foundations[0].size) } + @Test fun freeCellDropRejectsMultiCardDragOntoCell() {+ val vm = vm()+ val seven = c(Suit.SPADES, Rank.SEVEN)+ val six = c(Suit.HEARTS, Rank.SIX)+ vm.uiStateInternal.value = SolitaireUiState(+ gameMode = GameMode.FREECELL,+ freeCell = FreeCellState(+ tableauPiles = listOf(listOf(seven, six)) + List(7) { emptyList() },+ ),+ )+ vm.dropTargets["freecell_0"] = Rect(0f, 0f, 100f, 100f)+ // Dragging the two-card stack (grabbed at the seven) onto a free+ // cell must not move anything: a cell holds a single card.+ vm.tryMoveByDrag("tableau_0_0", Offset(50f, 50f), IntSize(50, 50))+ var f = vm.uiState.value.freeCell!!+ assertEquals(listOf(seven, six), f.tableauPiles[0])+ assertTrue(f.freeCells.all { it == null })+ assertEquals(0, f.moveCount)+ // Dragging just the top card onto the cell works as before.+ vm.tryMoveByDrag("tableau_0_1", Offset(50f, 50f), IntSize(50, 50))+ f = vm.uiState.value.freeCell!!+ assertEquals(listOf(seven), f.tableauPiles[0])+ assertEquals(six, f.freeCells[0])+ assertEquals(1, f.moveCount)+ }+ @Test fun klondikeDropFromTableauToFoundationAndTableau() { val vm = vm() val ace = c(Suit.DIAMONDS, Rank.ACE) // foundation index 1