solitaire: add tap-to-move for Klondike and FreeCell cards
- Commit
166dd94bcc40b4377c12e679d6a6a53ef87d29a7- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-08 17:40 UTC (4 months ago)
- Parents
c6c0eeb
Diff
games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/DragAndDrop.kt
diff --git a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/DragAndDrop.kt b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/DragAndDrop.ktindex 65d560a92c..ccf575ae03 100644--- a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/DragAndDrop.kt+++ b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/ui/DragAndDrop.kt@@ -1,6 +1,7 @@ package com.vayunmathur.games.solitaire.ui import androidx.compose.foundation.gestures.detectDragGestures+import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.offset@@ -50,6 +51,11 @@ fun DraggableCard( startPos = coords.positionInRoot() cardSize = coords.size }+ .pointerInput(card, sourceId) {+ detectTapGestures(+ onTap = { actions.autoMove(sourceId) },+ )+ } .pointerInput(card, sourceId) { detectDragGestures( onDragStart = {
games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireUiContract.kt
diff --git a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireUiContract.kt b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireUiContract.ktindex 609f9ed444..02229c4fce 100644--- a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireUiContract.kt+++ b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireUiContract.kt@@ -34,6 +34,14 @@ interface SolitaireActions { fun pyramidTapCard(id: String) {} fun pyramidDealStock() {} + /**+ * Tap-to-move: send the card (or valid run) at [sourceId] to its best legal+ * destination — a foundation first, otherwise a valid tableau/free-cell spot.+ * A no-op when there is no legal move. Klondike and FreeCell only; Spider has no+ * foundations and Pyramid is already tap-based.+ */+ fun autoMove(sourceId: String) {}+ fun undo() {} fun restart() {} fun giveUp() {}
games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireViewModel.kt
diff --git a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireViewModel.kt b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireViewModel.ktindex b5d36a7d94..dd9f5865c1 100644--- a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireViewModel.kt+++ b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/util/SolitaireViewModel.kt@@ -887,6 +887,110 @@ class SolitaireViewModel(application: Application) : AndroidViewModel(applicatio } } + // --- Tap to move (auto) ---++ override fun autoMove(sourceId: String) {+ when (_uiState.value.gameMode) {+ GameMode.KLONDIKE -> klondikeAutoMove(sourceId)+ GameMode.FREECELL -> freeCellAutoMove(sourceId)+ else -> {} // Spider has no foundations; Pyramid is already tap-based.+ }+ }++ private fun klondikeAutoMove(sourceId: String) {+ val state = _uiState.value.klondike ?: return+ if (state.isWon) return+ val cards = draggedCards(sourceId)+ if (cards.isEmpty()) return+ val topCard = cards.first()++ val fromCol = if (sourceId.startsWith("tableau_"))+ sourceId.removePrefix("tableau_").substringBefore("_").toIntOrNull() else null+ val fromIdx = if (sourceId.startsWith("tableau_"))+ sourceId.removePrefix("tableau_").split("_").getOrNull(1)?.toIntOrNull() ?: 0 else 0++ // 1) Foundation — only a single top card can go up, and it is the preferred move.+ if (cards.size == 1) {+ for (fi in state.foundations.indices) {+ if (canPlaceOnFoundation(topCard, state.foundations[fi])) {+ when {+ sourceId == "waste" -> klondikeMoveWasteToFoundation(fi)+ fromCol != null -> klondikeMoveTableauToFoundation(fromCol, fi)+ else -> return+ }+ return+ }+ }+ }++ // 2) Tableau — first column that legally accepts the run.+ for (toCol in state.tableauPiles.indices) {+ if (toCol == fromCol) continue+ val toPile = state.tableauPiles[toCol]+ if (!canPlaceOnKlondikeTableau(topCard, toPile)) continue+ // Skip shuffling a lone King between empty columns — it accomplishes nothing.+ val toEmpty = toPile.faceUp.isEmpty() && toPile.faceDown.isEmpty()+ if (toEmpty && fromCol != null && fromIdx == 0 && state.tableauPiles[fromCol].faceDown.isEmpty()) continue+ when {+ sourceId == "waste" -> klondikeMoveWasteToTableau(toCol)+ fromCol != null -> klondikeMoveTableauToTableau(fromCol, fromIdx, toCol)+ else -> return+ }+ return+ }+ }++ private fun freeCellAutoMove(sourceId: String) {+ val state = _uiState.value.freeCell ?: return+ if (state.isWon) return+ val cards = draggedCards(sourceId)+ if (cards.isEmpty()) return+ val topCard = cards.first()+ val single = cards.size == 1++ val fromCol = if (sourceId.startsWith("tableau_"))+ sourceId.removePrefix("tableau_").substringBefore("_").toIntOrNull() else null+ val fromIdx = if (sourceId.startsWith("tableau_"))+ sourceId.removePrefix("tableau_").split("_").getOrNull(1)?.toIntOrNull() ?: 0 else 0+ val fromCell = if (sourceId.startsWith("freecell_"))+ sourceId.removePrefix("freecell_").toIntOrNull() else null++ // 1) Foundation (single card only), the preferred move.+ if (single) {+ for (fi in state.foundations.indices) {+ if (canPlaceOnFoundation(topCard, state.foundations[fi])) {+ when {+ fromCell != null -> freeCellMoveFreeCellToFoundation(fromCell, fi)+ fromCol != null -> freeCellMoveTableauToFoundation(fromCol, fi)+ else -> return+ }+ return+ }+ }+ }++ // 2) Tableau — first column that legally accepts the run.+ for (toCol in state.tableauPiles.indices) {+ if (toCol == fromCol) continue+ val toPile = state.tableauPiles[toCol]+ if (!canPlaceOnFreeCellTableau(topCard, toPile)) continue+ // Skip moving a lone card between empty columns — pointless churn.+ if (toPile.isEmpty() && fromCol != null && single && state.tableauPiles[fromCol].size == 1) continue+ when {+ fromCell != null -> freeCellMoveFromFreeCell(fromCell, toCol)+ fromCol != null -> freeCellMoveTableauToTableau(fromCol, fromIdx, toCol)+ else -> return+ }+ return+ }++ // 3) Empty free cell — last resort for a single card off a tableau column.+ if (single && fromCol != null) {+ val emptyCell = state.freeCells.indexOfFirst { it == null }+ if (emptyCell >= 0) freeCellMoveToFreeCell(fromCol, emptyCell)+ }+ }+ /** * Starts a drag from [sourceId], deriving the carried cards from current state. * Returns false (and starts nothing) when that source holds no card.