solitaire: undo no longer refunds the game clock
- Commit
51f8546dd050235333d2d3f8c26ff26e98f5519b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 06:24 UTC (5 days ago)
- Parents
e88c5d8
undo() restored the pre-move snapshot wholesale, so elapsedSeconds rewound to whatever the timer read when the move was made: draw from stock at 10 s, think 3 s, undo — the clock reads 10 again. Any player could think indefinitely, undo, and the refunded time flowed into recordGameWon's timeSeconds (best times, and the under-3-minute Klondike achievement). The snapshot conflates board position with session metadata; the code already recognised this for one field (usedUndo is forced over the snapshot so the undo is never forgotten) but elapsedSeconds was not carried over. undo() now takes elapsedSeconds from the live state (falling back to the snapshot when no matching live variant exists) for all four variants; the move count still rewinds because the move itself is being taken back. Four new tests drive each variant through the real ViewModel asserting board and move count restored while the clock is preserved (Klondike also asserts ticking continues). Move guards, FreeCell capacity, foundations, auto-complete and solver replay were audited and cleared. Verified: :games:solitaire: testDevUnitTest 187 tests pass; :games:solitaire:lint 0 errors, warnings pre-existing in library modules.
Diff
games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireViewModel.kt
diff --git a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireViewModel.kt b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireViewModel.ktindex 7a39951817..083572f36e 100644--- a/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireViewModel.kt+++ b/games/solitaire/src/main/java/com/vayunmathur/games/solitaire/platform/SolitaireViewModel.kt@@ -266,22 +266,51 @@ class SolitaireViewModel(application: Application) : AndroidViewModel(applicatio override fun undo() { clearMotion()- val history = uiStateInternal.value.history+ val state = uiStateInternal.value+ val history = state.history if (history.isEmpty()) return val prev = history.last() val newHistory = history.dropLast(1)+ // The clock is session time, not board position: undoing a move takes+ // back the move (and its move count) but must not turn back the timer,+ // or a player could refund thinking time into a best time. Like+ // usedUndo, the live elapsed seconds are carried over the snapshot. when (prev) { is KlondikeState -> uiStateInternal.update {- it.copy(klondike = prev.copy(usedUndo = true), history = newHistory)+ it.copy(+ klondike = prev.copy(+ usedUndo = true,+ elapsedSeconds = state.klondike?.elapsedSeconds ?: prev.elapsedSeconds,+ ),+ history = newHistory,+ ) } is SpiderState -> uiStateInternal.update {- it.copy(spider = prev.copy(usedUndo = true), history = newHistory)+ it.copy(+ spider = prev.copy(+ usedUndo = true,+ elapsedSeconds = state.spider?.elapsedSeconds ?: prev.elapsedSeconds,+ ),+ history = newHistory,+ ) } is FreeCellState -> uiStateInternal.update {- it.copy(freeCell = prev.copy(usedUndo = true), history = newHistory)+ it.copy(+ freeCell = prev.copy(+ usedUndo = true,+ elapsedSeconds = state.freeCell?.elapsedSeconds ?: prev.elapsedSeconds,+ ),+ history = newHistory,+ ) } is PyramidState -> uiStateInternal.update {- it.copy(pyramid = prev.copy(usedUndo = true), history = newHistory)+ it.copy(+ pyramid = prev.copy(+ usedUndo = true,+ elapsedSeconds = state.pyramid?.elapsedSeconds ?: prev.elapsedSeconds,+ ),+ history = newHistory,+ ) } } }
games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/UndoTimerTest.kt
diff --git a/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/UndoTimerTest.kt b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/UndoTimerTest.ktnew file mode 100644index 0000000000..d01f5f397d--- /dev/null+++ b/games/solitaire/src/test/java/com/vayunmathur/games/solitaire/platform/UndoTimerTest.kt@@ -0,0 +1,106 @@+package com.vayunmathur.games.solitaire.platform++import android.app.Application+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++/**+ * Undo restores the board position from before the move, but the game clock+ * is session time, not board position: the seconds spent thinking after a+ * move must not be refunded by undoing it (best times are recorded from+ * elapsedSeconds). The move count still rewinds with the move itself.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class UndoTimerTest {++ private val app = ApplicationProvider.getApplicationContext<Application>()++ @Test+ fun klondikeUndoKeepsElapsedSeconds() {+ val vm = SolitaireViewModel(app)+ val ace = Card(Suit.HEARTS, Rank.ACE)+ vm.uiStateInternal.value = SolitaireUiState(+ gameMode = GameMode.KLONDIKE,+ klondike = KlondikeState(+ stock = listOf(ace),+ tableauPiles = List(7) { TableauPile() },+ elapsedSeconds = 10,+ ),+ )+ vm.drawFromStock()+ repeat(3) { vm.incrementTimer() }+ assertEquals(13, vm.uiState.value.klondike!!.elapsedSeconds)+ vm.undo()+ val k = vm.uiState.value.klondike!!+ // Board is restored exactly as before the draw...+ assertEquals(listOf(ace), k.stock)+ assertTrue(k.waste.isEmpty())+ assertEquals(0, k.moveCount)+ assertTrue(k.usedUndo)+ // ...but the clock is not turned back.+ assertEquals(13, k.elapsedSeconds)+ // And it keeps ticking from the live time, not the snapshot's.+ vm.incrementTimer()+ assertEquals(14, vm.uiState.value.klondike!!.elapsedSeconds)+ }++ @Test+ fun spiderUndoKeepsElapsedSeconds() {+ val vm = SolitaireViewModel(app)+ vm.selectMode(GameMode.SPIDER)+ repeat(5) { vm.incrementTimer() }+ vm.dealSpiderStock()+ repeat(2) { vm.incrementTimer() }+ assertEquals(7, vm.uiState.value.spider!!.elapsedSeconds)+ vm.undo()+ val s = vm.uiState.value.spider!!+ assertEquals(0, s.moveCount)+ assertTrue(s.usedUndo)+ assertEquals(7, s.elapsedSeconds)+ }++ @Test+ fun freeCellUndoKeepsElapsedSeconds() {+ val vm = SolitaireViewModel(app)+ vm.selectMode(GameMode.FREECELL)+ val col = vm.uiState.value.freeCell!!.tableauPiles.indexOfFirst { it.isNotEmpty() }+ repeat(4) { vm.incrementTimer() }+ vm.freeCellMoveToFreeCell(col, 0)+ vm.incrementTimer()+ assertEquals(5, vm.uiState.value.freeCell!!.elapsedSeconds)+ vm.undo()+ val f = vm.uiState.value.freeCell!!+ assertEquals(0, f.moveCount)+ assertTrue(f.usedUndo)+ assertEquals(5, f.elapsedSeconds)+ }++ @Test+ fun pyramidUndoKeepsElapsedSeconds() {+ val vm = SolitaireViewModel(app)+ vm.selectMode(GameMode.PYRAMID)+ repeat(6) { vm.incrementTimer() }+ vm.pyramidDealStock()+ repeat(3) { vm.incrementTimer() }+ assertEquals(9, vm.uiState.value.pyramid!!.elapsedSeconds)+ vm.undo()+ val p = vm.uiState.value.pyramid!!+ assertEquals(0, p.moveCount)+ assertTrue(p.usedUndo)+ assertTrue(p.waste.isEmpty())+ assertEquals(9, p.elapsedSeconds)+ }+}