Modern Apps git

← Modern-Apps.git

unblockjam: refuse stale slides through a newly blocked path

Commit
a53bca89d5ad28f3b9af7f6ee09a68d676724a29
Author
Vayun Mathur <[email protected]>
When
2026-12-02 01:18 UTC (4 days ago)
Parents
161db0f
Symptom: a drag that began while its path was clear could settle
several cells away after another finger (or a step) had meanwhile
moved a block into the cells it slides through. The reconciler
only checked the reported destination, so when that stayed free
the dragged block teleported through the intervening block.

Root cause: UnblockJamViewModel.reconcile validated the merged
block at its destination with isMoveValid and nothing else; none
of the intermediate positions of a multi-cell slide were ever
checked against the board as it stands.

Fix: after the destination check, step the block one cell at a
time from its current position to the reported one and require
every swept position to be legal on the current board.

Verified: new UnblockJamViewModelOverlapTest case staging a
shipped level failed before the fix (the stale slide landed) and
passes after; full :games:unblockjam:testDevUnitTest,
:games:unblockjam:lint and :games:unblockjam:lintVitalRelease
all pass.

Diff

games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModel.kt
diff --git a/games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModel.kt b/games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModel.ktindex 10c0b1c29d..6e7e4262e4 100644--- a/games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModel.kt+++ b/games/unblockjam/src/main/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModel.kt@@ -287,8 +287,9 @@ class UnblockJamViewModel(     /**      * [reported] as a move on [current]: only the block it says moved changes, taken from      * [reported]'s position for it, so a move committed in the meantime (its block sits wherever-     * that move left it) survives the report. Null when that block's reported cell has been-     * taken by a block moved in the meantime โ€” the dragged block stays where it is. A report+     * that move left it) survives the report. Null when that block's reported cell โ€” or any+     * cell its slide sweeps through โ€” has been taken by a block moved in the meantime: the+     * dragged block stays where it is. A report      * that does not name a moved block of this board is installed as reported.      */     private fun reconcile(@@ -304,8 +305,25 @@ class UnblockJamViewModel(         }         val merged = current.blocks.toMutableList()         merged[index] = currentBlock.copy(position = reportedBlock.position)-        val legal = isMoveValid(merged[index], merged - merged[index], current.dimension)-        return if (legal) current.copy(blocks = merged, lastMovedBlockIndex = index) else null+        val others = current.blocks.filterIndexed { i, _ -> i != index }+        if (!isMoveValid(merged[index], others, current.dimension)) return null+        // The destination being free is not enough for a multi-cell slide:+        // every cell swept on the way there must be free on the board as it+        // stands, or the block teleports through whatever a move committed+        // in the meantime placed in its path.+        val stepX = (reportedBlock.position.x - currentBlock.position.x).coerceIn(-1, 1)+        val stepY = (reportedBlock.position.y - currentBlock.position.y).coerceIn(-1, 1)+        var swept = currentBlock+        while (swept.position != reportedBlock.position) {+            swept = swept.copy(+                position = swept.position.copy(+                    x = swept.position.x + stepX,+                    y = swept.position.y + stepY,+                ),+            )+            if (!isMoveValid(swept, others, current.dimension)) return null+        }+        return current.copy(blocks = merged, lastMovedBlockIndex = index)     }      override fun onLevelWon() {
games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModelOverlapTest.kt
diff --git a/games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModelOverlapTest.kt b/games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModelOverlapTest.ktindex 63f6f47c97..e3b47d877b 100644--- a/games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModelOverlapTest.kt+++ b/games/unblockjam/src/test/java/com/vayunmathur/games/unblockjam/platform/UnblockJamViewModelOverlapTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.games.unblockjam.platform import android.app.Application import androidx.test.core.app.ApplicationProvider import com.vayunmathur.games.unblockjam.data.Block+import com.vayunmathur.games.unblockjam.data.Coord import com.vayunmathur.games.unblockjam.data.LevelData import com.vayunmathur.games.unblockjam.data.LevelPack import kotlinx.coroutines.Dispatchers@@ -152,6 +153,84 @@ class UnblockJamViewModelOverlapTest {         return if (taken) Triple(levelIndex, firstStep, secondStep) else null     } +    /** A staged swept-path clash: the intervening step, and the stale multi-cell report. */+    private data class SweptClash(+        val levelIndex: Int,+        val first: StepResult.Moved,+        val reported: LevelData,+    )++    /**+     * A shipped level where one block can slide at least two cells from the start, and+     * another block can step into a cell that slide sweeps through (but not into its+     * destination); null when no level stages it.+     */+    private fun findSweptClash(): SweptClash? {+        for ((levelIndex, level) in LevelPack.packs[0].levels.withIndex()) {+            for (a in level.blocks.indices) {+                val block = level.blocks[a]+                if (block.fixed) continue+                val horizontal = block.dimension.width > block.dimension.height+                if (!horizontal && block.dimension.height <= block.dimension.width) continue+                for (dist in listOf(-3, -2, 2, 3)) {+                    val destination = block.copy(+                        position = Coord(+                            block.position.x + if (horizontal) dist else 0,+                            block.position.y + if (horizontal) 0 else dist,+                        ),+                    )+                    if (!isMoveValid(destination, level.blocks - block, level.dimension)) continue+                    val swept = (1 until kotlin.math.abs(dist)).map { s ->+                        val offset = if (dist > 0) s else -s+                        block.copy(+                            position = Coord(+                                block.position.x + if (horizontal) offset else 0,+                                block.position.y + if (horizontal) 0 else offset,+                            ),+                        )+                    }+                    for (b in level.blocks.indices) {+                        if (b == a) continue+                        for ((dx, dy) in directions) {+                            val stepped = step(level, b, dx, dy) ?: continue+                            val movedBlock = stepped.level.blocks[b]+                            if (swept.any { overlaps(movedBlock, it) } && !overlaps(movedBlock, destination)) {+                                val reportedBlocks = level.blocks.toMutableList()+                                reportedBlocks[a] = destination+                                return SweptClash(+                                    levelIndex,+                                    stepped,+                                    level.copy(blocks = reportedBlocks, lastMovedBlockIndex = a),+                                )+                            }+                        }+                    }+                }+            }+        }+        return null+    }++    @Test+    fun staleReportThroughACellTakenInTheMeantimeIsRefused() {+        val clash = assertNotNull(findSweptClash(), "no shipped level stages the swept clash")++        val vm = UnblockJamViewModel(app)+        vm.loadLevel(0, clash.levelIndex)+        vm.onBlockMoved(clash.first.level)+        val before = vm.uiState.value+        assertEquals(1, vm.getCurrentMoves())++        vm.onBlockMoved(clash.reported)+        assertEquals(+            before,+            vm.uiState.value,+            "a stale slide whose path a committed move has since blocked must be " ++                "refused, not teleported through the intervening block",+        )+        assertEquals(1, vm.getCurrentMoves())+    }+     @Test     fun staleReportIntoACellTakenInTheMeantimeIsRefused() {         val (levelIndex, first, second) =