Modern Apps git

← Modern-Apps.git

games-chess: reselect own piece on tap in game VM

Commit
f5bd5da04ab3b5f314c735f1a2f46e33ab7ebe10
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:02 UTC (29 days ago)
Parents
6fe09da
With a piece selected, tapping a different own piece called isValidMove (false, since the target holds a friendly piece) and fell through to deselect. Players had to tap a third time to select the piece they actually wanted. PuzzleViewModel and LearnViewModel already reselect in this case; make ChessViewModel match them. Tapping the selected square itself still deselects, and illegal empty-square taps still just deselect.

Adds a ChessViewModelTest covering select -> reselect -> deselect.

Diff

games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.ktindex a031d90a7c..75a08a5c18 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/util/ChessViewModel.kt@@ -104,6 +104,12 @@ class ChessViewModel(application: Application) : AndroidViewModel(application),             if (pieceAtPosition != null && pieceAtPosition.color == state.turn) {                 _uiState.update { it.copy(selectedPiece = position) }             }+        } else if (position != selectedPiece && pieceAtPosition != null &&+            pieceAtPosition.color == state.turn+        ) {+            // Tapping another of the player's own pieces reselects it,+            // matching PuzzleViewModel and LearnViewModel.+            _uiState.update { it.copy(selectedPiece = position) }         } else {             if (board.isValidMove(selectedPiece, position)) {                 val newBoard = board.movePiece(selectedPiece, position)
games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessViewModelTest.kt
diff --git a/games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessViewModelTest.kt b/games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessViewModelTest.ktindex d97fd4d02a..2244ccfc23 100644--- a/games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessViewModelTest.kt+++ b/games/chess/src/test/java/com/vayunmathur/games/chess/util/ChessViewModelTest.kt@@ -55,6 +55,16 @@ class ChessViewModelTest {         assertEquals(PieceColor.WHITE, vm.uiState.value.turn)     } +    @Test fun `tapping another own piece reselects it`() {+        val vm = ChessViewModel(app)+        vm.onSquareClick(sq('e', 2))+        assertEquals(sq('e', 2), vm.uiState.value.selectedPiece)+        vm.onSquareClick(sq('d', 2)) // own pawn: reselect, not deselect+        assertEquals(sq('d', 2), vm.uiState.value.selectedPiece)+        vm.onSquareClick(sq('d', 2)) // same square again: deselect+        assertNull(vm.uiState.value.selectedPiece)+    }+     @Test fun `fools mate ends the game and blocks further clicks`() {         val vm = ChessViewModel(app)         fun move(f1: Char, r1: Int, f2: Char, r2: Int) { vm.onSquareClick(sq(f1, r1)); vm.onSquareClick(sq(f2, r2)) }