Modern Apps git

← Modern-Apps.git

games-chess: defer terminal result until promotion chosen

Commit
be1f8119818478c8f5b930c8aac1968bfde7ad73
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:02 UTC (29 days ago)
Parents
ff5397d
onSquareClick computed check/checkmate/draw status on the board where the pawn had just stepped onto the last rank still unpromoted, and stored it in uiState while the promotion dialog was up. The unpromoted pawn attacks nothing from the last rank, so the verdict (and the board.isCheckmate answer behind it) can differ from the position after the player picks a piece; a premature Checkmate result also fires WinAchievements, whose claimWinScoring one-time guard would count the win before the promotion is even chosen.

Skip the terminal evaluation while promotionPosition is pending (nextTurn already stays with the mover in that state); onPromote already recomputes result and status on the promoted board.

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 75a08a5c18..67ef11017f 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@@ -114,7 +114,17 @@ class ChessViewModel(application: Application) : AndroidViewModel(application),             if (board.isValidMove(selectedPiece, position)) {                 val newBoard = board.movePiece(selectedPiece, position)                 val nextTurn = if (newBoard.promotionPosition != null) state.turn else state.turn.opposite-                val (result, status) = statusFor(newBoard, nextTurn)+                // While a promotion choice is pending the move is not+                // complete: the pawn sits unpromoted on the last rank, so a+                // check/checkmate verdict computed now can be wrong for the+                // piece the player is about to pick (and would fire the+                // win achievements + one-time win counter early). onPromote+                // computes the real result once the piece is chosen.+                val (result, status) = if (newBoard.promotionPosition != null) {+                    null to null+                } else {+                    statusFor(newBoard, nextTurn)+                }                 _uiState.update {                     it.copy(                         board = newBoard,