arrows: animate an arrow's attempt to leave, and fix a stray ripple
- Commit
74bf674c306b0ade18b7120a65d1e65731fc2083- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-24 20:21 UTC (3 months ago)
- Parents
2a569fc
Diff
games/arrows/src/main/java/com/vayunmathur/games/arrows/domain/ArrowsRules.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/domain/ArrowsRules.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/domain/ArrowsRules.ktindex ada3a05983..d21da24b6a 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/domain/ArrowsRules.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/domain/ArrowsRules.kt@@ -16,6 +16,15 @@ import com.vayunmathur.games.arrows.data.TapOutcome */ object ArrowsRules { + /**+ * What happens when an arrow is tapped, in enough detail to animate it.+ *+ * @param route the piece's own cells followed by its head's escape path, tail first.+ * @param advance how many cells the head moves before it leaves or is stopped.+ * @param clears whether it gets off the board.+ */+ data class Travel(val route: List<Int>, val advance: Int, val clears: Boolean)+ /** * The cells an arrow's head visits after leaving [from], heading [direction], until it is off the * board.@@ -43,19 +52,40 @@ object ArrowsRules { } /**- * Whether [piece] can leave [state] right now.+ * How far [piece] gets if tapped, and what stops it. *- * The body follows the head, so the only thing that can stop it is an occupied cell on the head's- * route. The piece's own cells count as occupied too: normally the route leads away from its body- * so this never comes up, but a mirror can turn an arrow back into itself, and letting it pass- * through its own tail would be indefensible to a player watching it happen.+ * [Travel.route] runs from the piece's tail through its body and on along the head's escape path, so+ * a caller animating the move can read cell `i + advance` for the piece's `i`th cell and get snake+ * motion round corners for free.+ *+ * [Travel.advance] is how many cells the head can move: the full path length for a clean exit, or the+ * number of free cells before the obstruction. Zero means it is wedged against something already. */- fun isBlocked(state: ArrowsGameState, piece: ArrowPiece): Boolean {- val path = exitPath(state.puzzle, piece.head, piece.direction) ?: return true+ fun travel(state: ArrowsGameState, piece: ArrowPiece): Travel {+ val path = exitPath(state.puzzle, piece.head, piece.direction)+ // A route that never leaves is impassable; nothing moves.+ if (path == null) return Travel(piece.cells, advance = 0, clears = false)+ val occupied = state.occupancy- return path.any { occupied.containsKey(it) }+ val blockedAt = path.indexOfFirst { occupied.containsKey(it) }+ val route = piece.cells + path+ return if (blockedAt < 0) {+ Travel(route, advance = path.size, clears = true)+ } else {+ Travel(route, advance = blockedAt, clears = false)+ } } + /**+ * Whether [piece] can leave [state] right now.+ *+ * The body follows the head, so the only thing that can stop it is an occupied cell on the head's+ * route. The piece's own cells count as occupied too: normally the route leads away from its body so+ * this never comes up, but a mirror can turn an arrow back into itself, and letting it pass through+ * its own tail would be indefensible to a player watching it happen.+ */+ fun isBlocked(state: ArrowsGameState, piece: ArrowPiece): Boolean = !travel(state, piece).clears+ /** * Applies a tap on the arrow with [pieceId]. *
games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.ktindex e68d902f6e..d723dc9685 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsUiContract.kt@@ -17,6 +17,9 @@ interface ArrowsGameActions { /** Sends the tapped arrow on its way, or spends a heart if it is boxed in. */ fun tapArrow(pieceId: Int) {} + /** Called once the board has finished animating [ArrowsUiState.move], to commit its outcome. */+ fun commitMove() {}+ fun nextLevel() {} fun restartLevel() {} fun setGameMode(mode: GameMode) {}@@ -24,6 +27,25 @@ interface ArrowsGameActions { companion object { val Noop: ArrowsGameActions = object : ArrowsGameActions {} } } +/**+ * A tap that is being animated but has not yet taken effect.+ *+ * The outcome is decided the moment the arrow is tapped, but nothing is written until the board has+ * finished showing it: an arrow that clears has to stay drawn while it flies out, and one that is blocked+ * has to be drawn moving and coming back. [ArrowsGameActions.commitMove] is what applies it.+ *+ * @param route the piece's cells followed by its head's escape path, so cell `i` sits at+ * `route[i + advance]` at full advance.+ * @param advance how many cells the head travels.+ * @param clears whether the arrow gets out, which decides both the ending and whether a heart is spent.+ */+data class ArrowMove(+ val pieceId: Int,+ val route: List<Int>,+ val advance: Int,+ val clears: Boolean,+)+ /** What the board needs to draw itself. [game] is null only while a board is being generated. */ data class ArrowsUiState( val game: ArrowsGameState? = null,@@ -36,6 +58,8 @@ data class ArrowsUiState( val generating: Boolean = false, /** Set when even a widened seed search found no clearable board, so the board can offer a retry. */ val generationFailed: Boolean = false,+ /** The tap currently being animated, if any. Taps are ignored while this is set. */+ val move: ArrowMove? = null, ) interface SettingsActions {
games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.ktindex e0438a89d6..b27680ca2f 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt@@ -197,18 +197,50 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application), */ private var lastBlockedId: Int = -1 + /**+ * Starts a tap: works out what will happen and hands it to the board to animate.+ *+ * Nothing is committed here. The outcome is decided now, from the board as it stands, but applying it+ * waits for [commitMove] so the arrow can be seen travelling — and, if it is blocked, seen coming+ * back. Taps are ignored while an animation is in flight, which also stops two arrows moving at once.+ */ override fun tapArrow(pieceId: Int) { val state = _uiState.value val game = state.game ?: return- if (game.isOver) return+ if (game.isOver || state.move != null) return+ val piece = game.remaining.firstOrNull { it.id == pieceId } ?: return++ val travel = ArrowsRules.travel(game, piece)+ _uiState.value = state.copy(+ move = ArrowMove(+ pieceId = pieceId,+ route = travel.route,+ advance = travel.advance,+ clears = travel.clears,+ )+ )+ }++ /**+ * Applies the animated tap.+ *+ * Re-runs the rules rather than trusting the outcome worked out when the tap started: the board cannot+ * have changed in between - taps are locked out while animating - but deriving the result in one place+ * keeps [ArrowsRules.tap] the only thing that decides what a tap does.+ */+ override fun commitMove() {+ val state = _uiState.value+ val move = state.move ?: return+ val game = state.game ?: return - val (next, outcome) = ArrowsRules.tap(game, pieceId)- if (outcome == TapOutcome.IGNORED) return+ val (next, outcome) = ArrowsRules.tap(game, move.pieceId)+ if (outcome == TapOutcome.IGNORED) {+ _uiState.value = state.copy(move = null)+ return+ } lastBlockedId = next.blockedId- // Published immediately so the flash and the heart update land on this frame rather than- // waiting for the DataStore write to come back round through the flow.- _uiState.value = state.copy(game = next)+ _uiState.value = state.copy(game = next, move = null) val daily = state.mode == GameMode.DAILY viewModelScope.launch {@@ -223,17 +255,20 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application), val state = _uiState.value if (state.mode == GameMode.DAILY) return lastBlockedId = -1+ _uiState.value = state.copy(move = null) viewModelScope.launch { dataStore.saveLevel(state.level + 1) } } override fun restartLevel() { val daily = _uiState.value.mode == GameMode.DAILY lastBlockedId = -1+ _uiState.value = _uiState.value.copy(move = null) viewModelScope.launch { dataStore.resetBoard(daily) } } override fun setGameMode(mode: GameMode) { lastBlockedId = -1+ _uiState.value = _uiState.value.copy(move = null) viewModelScope.launch { dataStore.setGameMode(mode) } }
games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.ktindex ea386ad3e6..20ea3f2ee8 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/ArrowsGameScreen.kt@@ -122,7 +122,9 @@ fun ArrowsGameScreen( ArrowsBoard( game = game, showRoutes = state.showRoutes,+ move = state.move, onTapArrow = actions::tapArrow,+ onMoveFinished = actions::commitMove, modifier = Modifier .widthIn(max = BoardMaxWidth) .fillMaxWidth(),
games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoard.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoard.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoard.ktindex b7ffc952e8..bfbe708b2a 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoard.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoard.kt@@ -1,12 +1,24 @@ package com.vayunmathur.games.arrows.ui.components +import androidx.compose.animation.core.Animatable+import androidx.compose.animation.core.FastOutSlowInEasing+import androidx.compose.animation.core.LinearEasing+import androidx.compose.animation.core.LinearOutSlowInEasing+import androidx.compose.animation.core.tween import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.detectTapGestures+import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.getValue+import androidx.compose.runtime.key+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.geometry.Offset@@ -26,7 +38,9 @@ import com.vayunmathur.games.arrows.data.ArrowsGameState import com.vayunmathur.games.arrows.data.Direction import com.vayunmathur.games.arrows.data.Mirror import com.vayunmathur.games.arrows.domain.ArrowsRules+import com.vayunmathur.games.arrows.platform.ArrowMove import com.vayunmathur.library.ui.MaterialTheme+import kotlinx.coroutines.delay /** * The board.@@ -43,7 +57,9 @@ import com.vayunmathur.library.ui.MaterialTheme fun ArrowsBoard( game: ArrowsGameState, showRoutes: Boolean,+ move: ArrowMove?, onTapArrow: (Int) -> Unit,+ onMoveFinished: () -> Unit, modifier: Modifier = Modifier, ) { val scheme = MaterialTheme.colorScheme@@ -53,17 +69,37 @@ fun ArrowsBoard( val dotColor = scheme.outlineVariant val cols = game.puzzle.cols - // The route the last blocked arrow tried to take, up to and including what stopped it. Computed- // here rather than stored, because it is derivable from the board and would only go stale.+ // How far along its route the moving arrow currently is, in cells. Driven by one animation per tap;+ // `0` whenever nothing is moving, so a settled board draws from `game` alone.+ val advance = remember(move) { Animatable(0f) }+ // Turns red only once it has actually run into something, not from the first frame.+ var showBlocked by remember(move) { mutableStateOf(false) }++ LaunchedEffect(move) {+ if (move == null) return@LaunchedEffect+ if (move.clears) {+ // Past the end of the route, so the tail clears the edge too rather than winking out on it.+ val target = (move.advance + move.route.size).toFloat()+ advance.animateTo(target, tween(cellMillis(move.advance + move.route.size), easing = LinearEasing))+ } else {+ // Wedged arrows still get a nudge, or a tap on one reads as the game ignoring the input.+ val target = if (move.advance == 0) NudgeCells else move.advance.toFloat()+ advance.animateTo(target, tween(cellMillis(move.advance), easing = LinearOutSlowInEasing))+ showBlocked = true+ delay(BlockedHoldMillis)+ advance.animateTo(0f, tween(cellMillis(move.advance), easing = FastOutSlowInEasing))+ }+ onMoveFinished()+ }++ // The route the last blocked arrow tried to take, up to and including what stopped it. Derived rather+ // than stored, so it cannot go stale against the board. val blockedRoute = if (!showRoutes || game.blockedId < 0) emptyList() else { game.puzzle.pieces.firstOrNull { it.id == game.blockedId }?.let { piece ->- val occupied = game.occupancy- ArrowsRules.exitPath(game.puzzle, piece.head, piece.direction)- .orEmpty()- .let { route ->- val stop = route.indexOfFirst { it in occupied }- if (stop < 0) route else route.take(stop + 1)- }+ val travel = ArrowsRules.travel(game, piece)+ // route is body + path, so drop the body and keep one past where it stopped: the cell that+ // stopped it is the one the player needs to look at.+ travel.route.drop(piece.length).take(travel.advance + 1) }.orEmpty() } @@ -92,36 +128,59 @@ fun ArrowsBoard( drawRoute(blockedRoute, cols, cellPx, blockedColor) } for (piece in game.remaining) {- val color = if (piece.id == game.blockedId) blockedColor else arrowColor- drawArrow(piece, cols, cellPx, color)+ val moving = piece.id == move?.pieceId+ val color = when {+ moving && showBlocked -> blockedColor+ piece.id == game.blockedId -> blockedColor+ else -> arrowColor+ }+ if (moving && move != null) {+ drawTravellingArrow(piece, move, advance.value, cols, cellPx, color)+ } else {+ drawArrow(piece, cols, cellPx, color)+ } } } ) {- // Semantics only: invisible, one per arrow, sitting on its head cell.+ // Semantics only: invisible, one per arrow, sitting on its head cell. Taps anywhere along an+ // arrow are handled by the board's own pointerInput; these exist so a screen reader has+ // something to find and activate.+ //+ // `key` matters: without it, clearing an arrow shifts every later Box's identity onto a+ // different arrow, and any interaction state they hold - a half-finished ripple - replays on+ // an unrelated head. `indication = null` then makes sure there is no ripple to leak, since+ // the board draws its own feedback and a stray square flashing over a cell reads as a bug. for (piece in game.remaining) {- val description = stringResource(- R.string.cd_arrow,- stringResource(piece.direction.spokenNameRes),- piece.head / cols + 1,- piece.head % cols + 1,- )- Box(- Modifier- .offset(x = cell * (piece.head % cols), y = cell * (piece.head / cols))- .size(cell)- .clickable { onTapArrow(piece.id) }- .clearAndSetSemantics { contentDescription = description }- )+ key(piece.id) {+ val description = stringResource(+ R.string.cd_arrow,+ stringResource(piece.direction.spokenNameRes),+ piece.head / cols + 1,+ piece.head % cols + 1,+ )+ Box(+ Modifier+ .offset(x = cell * (piece.head % cols), y = cell * (piece.head / cols))+ .size(cell)+ .clickable(+ interactionSource = remember { MutableInteractionSource() },+ indication = null,+ ) { onTapArrow(piece.id) }+ .clearAndSetSemantics { contentDescription = description }+ )+ } } // Redirectors are not interactive, but a screen reader still has to know they are there. val redirector = stringResource(R.string.cd_redirector) for (index in game.puzzle.mirrors.keys) {- Box(- Modifier- .offset(x = cell * (index % cols), y = cell * (index / cols))- .size(cell)- .clearAndSetSemantics { contentDescription = redirector }- )+ key(index) {+ Box(+ Modifier+ .offset(x = cell * (index % cols), y = cell * (index / cols))+ .size(cell)+ .clearAndSetSemantics { contentDescription = redirector }+ )+ } } } }@@ -166,30 +225,101 @@ private fun DrawScope.drawMirror(cell: Int, mirror: Mirror, cols: Int, size: Flo drawLine(color, start, end, strokeWidth = size * 0.09f, cap = StrokeCap.Round) } +/**+ * One arrow part-way along its route.+ *+ * Cell `i` of the piece sits at route position `i + advance`, so the body follows wherever the head went+ * and turns corners with it. Fractional advance interpolates between the two positions either side, which+ * is what makes the motion continuous rather than a hop per cell.+ *+ * Once the route runs out the piece is leaving the board, so positions past the end are extrapolated from+ * the final step's direction — it slides off the edge instead of stopping dead on it.+ */+private fun DrawScope.drawTravellingArrow(+ piece: ArrowPiece,+ move: ArrowMove,+ advance: Float,+ cols: Int,+ cell: Float,+ color: Color,+) {+ val centres = piece.cells.indices.map { routePoint(move.route, it + advance, cols, cell) }+ // Sampled half a cell further along the route rather than taken from the last two body cells: a+ // one-cell arrow has no "last two", and sampling also keeps the head turning smoothly through a+ // mirror instead of snapping once the body reaches it.+ val headAt = piece.cells.lastIndex + advance+ val ahead = routePoint(move.route, headAt + 0.5f, cols, cell)+ val head = centres.last()+ val sampled = Offset(ahead.x - head.x, ahead.y - head.y)+ // A mirror ring leaves an arrow with no escape path at all, so there is nothing ahead to sample and+ // the arrowhead would collapse to a dot. Fall back to the direction it is painted pointing.+ val heading = if (kotlin.math.hypot(sampled.x, sampled.y) > 0.01f) sampled+ else Offset(piece.direction.dCol.toFloat(), piece.direction.dRow.toFloat())+ drawArrowShape(centres, heading, cell, color)+}++/**+ * Where route position [at] falls in pixels, interpolating between whole cells.+ *+ * Beyond the last cell it keeps going in the direction of the final step, so an exiting arrow carries on+ * off the board rather than piling up on the edge.+ */+private fun routePoint(route: List<Int>, at: Float, cols: Int, cell: Float): Offset {+ fun centre(index: Int) = Offset((index % cols + 0.5f) * cell, (index / cols + 0.5f) * cell)++ val last = route.size - 1+ if (at <= 0f) return centre(route[0])+ if (at >= last) {+ val end = centre(route[last])+ val previous = centre(route[(last - 1).coerceAtLeast(0)])+ val overshoot = at - last+ return Offset(+ end.x + (end.x - previous.x) * overshoot,+ end.y + (end.y - previous.y) * overshoot,+ )+ }+ val low = at.toInt()+ val t = at - low+ val from = centre(route[low])+ val to = centre(route[low + 1])+ return Offset(from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t)+}+ /** * One arrow: its body as a single joined stroke, and an arrowhead at the head. * * The stroke runs through cell centres, so a turn in the polyline becomes a rounded corner. The tip- * reaches past the head's centre by [HEAD_REACH] to leave room for the barbs while staying inside its- * own cell rather than poking into the next one.+ * reaches past the head's centre by [HEAD_REACH] to leave room for the barbs while staying inside its own+ * cell rather than poking into the next one. */ private fun DrawScope.drawArrow(piece: ArrowPiece, cols: Int, cell: Float, color: Color) {+ val centres = piece.cells.map { Offset((it % cols + 0.5f) * cell, (it / cols + 0.5f) * cell) }+ val heading = Offset(piece.direction.dCol.toFloat(), piece.direction.dRow.toFloat())+ drawArrowShape(centres, heading, cell, color)+}++/**+ * Draws the body through [centres] and an arrowhead pointing along [heading].+ *+ * [heading] need not be normalised - it is only used for its direction - which lets the animated path+ * supply the difference between its last two points and get a head that turns with the route.+ */+private fun DrawScope.drawArrowShape(+ centres: List<Offset>,+ heading: Offset,+ cell: Float,+ color: Color,+) { val stroke = cell * 0.11f- fun centre(index: Int) = Offset((index % cols + 0.5f) * cell, (index / cols + 0.5f) * cell)+ val length = kotlin.math.hypot(heading.x, heading.y).takeIf { it > 0.0001f } ?: 1f+ val unit = Offset(heading.x / length, heading.y / length) - val head = centre(piece.head)- val tip = Offset(- head.x + piece.direction.dCol * cell * HEAD_REACH,- head.y + piece.direction.dRow * cell * HEAD_REACH,- )+ val head = centres.last()+ val tip = Offset(head.x + unit.x * cell * HEAD_REACH, head.y + unit.y * cell * HEAD_REACH) val path = Path().apply {- val first = centre(piece.cells.first())- moveTo(first.x, first.y)- for (index in piece.cells.drop(1)) {- val point = centre(index)- lineTo(point.x, point.y)- }+ moveTo(centres.first().x, centres.first().y)+ for (point in centres.drop(1)) lineTo(point.x, point.y) lineTo(tip.x, tip.y) } drawPath(@@ -198,20 +328,16 @@ private fun DrawScope.drawArrow(piece: ArrowPiece, cols: Int, cell: Float, color style = Stroke(width = stroke, cap = StrokeCap.Round, join = StrokeJoin.Round), ) - // Two barbs angled back from the tip. For an axis-aligned heading the perpendicular is just the- // row and column components swapped, so no trigonometry is needed.+ // Two barbs angled back from the tip. The perpendicular of a 2D vector is its components swapped+ // with one negated, so no trigonometry is needed even once the heading is diagonal mid-turn. val barb = cell * 0.26f- val backRow = -piece.direction.dRow- val backCol = -piece.direction.dCol- val perpRow = piece.direction.dCol- val perpCol = piece.direction.dRow for (side in intArrayOf(1, -1)) { drawLine( color = color, start = tip, end = Offset(- tip.x + (backCol + perpCol * side) * barb,- tip.y + (backRow + perpRow * side) * barb,+ tip.x + (-unit.x + -unit.y * side) * barb,+ tip.y + (-unit.y + unit.x * side) * barb, ), strokeWidth = stroke, cap = StrokeCap.Round,@@ -222,6 +348,21 @@ private fun DrawScope.drawArrow(piece: ArrowPiece, cols: Int, cell: Float, color /** How far past the head cell's centre the tip reaches, as a fraction of a cell. */ private const val HEAD_REACH = 0.34f +/** How long the arrow takes per cell of travel. Fast enough not to be a wait, slow enough to follow. */+private const val MILLIS_PER_CELL = 45++/** Floor on the animation, so a one-cell move is still long enough to register. */+private const val MIN_MOVE_MILLIS = 120++private fun cellMillis(cells: Int): Int =+ (cells * MILLIS_PER_CELL).coerceAtLeast(MIN_MOVE_MILLIS)++/** How far a wedged arrow lurches before coming back, in cells. */+private const val NudgeCells = 0.3f++/** How long the arrow stays red at the far end before returning. */+private const val BlockedHoldMillis = 180L+ /** * The route a blocked arrow tried to take, drawn as a trail of dots behind the arrows. *
games/arrows/src/test/java/com/vayunmathur/games/arrows/domain/ArrowsRulesTest.kt
diff --git a/games/arrows/src/test/java/com/vayunmathur/games/arrows/domain/ArrowsRulesTest.kt b/games/arrows/src/test/java/com/vayunmathur/games/arrows/domain/ArrowsRulesTest.ktindex 3175f778d2..59dd314a70 100644--- a/games/arrows/src/test/java/com/vayunmathur/games/arrows/domain/ArrowsRulesTest.kt+++ b/games/arrows/src/test/java/com/vayunmathur/games/arrows/domain/ArrowsRulesTest.kt@@ -224,4 +224,65 @@ class ArrowsRulesTest { val free = ArrowsRules.clearableNow(state(puzzle(listOf(mover, blocker)))) assertEquals(listOf(1), free.map { it.id }) }++ @Test+ fun travelOfAClearArrowRunsTheWholePath() {+ val piece = arrow(0, 2, 2, Direction.RIGHT)+ val travel = ArrowsRules.travel(state(puzzle(listOf(piece))), piece)+ assertTrue(travel.clears)+ // (2,3) and (2,4), then off the board.+ assertEquals(2, travel.advance)+ // The route is the body followed by the escape path, so an animation can index straight into it.+ assertEquals(piece.cells + listOf(at(2, 3), at(2, 4)), travel.route)+ }++ @Test+ fun travelStopsShortOfWhateverIsInTheWay() {+ val mover = arrow(0, 2, 1, Direction.RIGHT)+ val blocker = arrow(1, 2, 4, Direction.UP)+ val travel = ArrowsRules.travel(state(puzzle(listOf(mover, blocker))), mover)+ assertFalse(travel.clears)+ // Head at (2,1) can reach (2,2) and (2,3); (2,4) is the blocker's head.+ assertEquals(2, travel.advance)+ }++ @Test+ fun travelIsZeroWhenWedgedAgainstTheNextCell() {+ // The blocker's body sits immediately ahead, so there is nowhere to go at all.+ val mover = ArrowPiece(0, listOf(at(2, 0), at(2, 1)), Direction.RIGHT)+ val blocker = ArrowPiece(1, listOf(at(1, 2), at(2, 2)), Direction.DOWN)+ val travel = ArrowsRules.travel(state(puzzle(listOf(mover, blocker))), mover)+ assertFalse(travel.clears)+ assertEquals(0, travel.advance, "a wedged arrow cannot advance")+ }++ @Test+ fun travelOnAnImpassableRouteReportsNoMovement() {+ // A ring of mirrors traps the head, so the arrow can never leave and must not appear to try.+ val ring = mapOf(+ at(1, 1) to Mirror.FORWARD,+ at(1, 3) to Mirror.BACK,+ at(3, 3) to Mirror.FORWARD,+ at(3, 1) to Mirror.BACK,+ )+ val piece = ArrowPiece(0, listOf(at(3, 1), at(2, 1)), Direction.UP)+ val travel = ArrowsRules.travel(state(puzzle(listOf(piece), ring)), piece)+ assertFalse(travel.clears)+ assertEquals(0, travel.advance)+ }++ @Test+ fun travelAndIsBlockedAlwaysAgree() {+ // isBlocked is defined in terms of travel, so the two can never drift apart - this pins that down.+ val a = arrow(0, 2, 2, Direction.RIGHT)+ val b = arrow(1, 2, 4, Direction.UP)+ val s = state(puzzle(listOf(a, b)))+ for (piece in listOf(a, b)) {+ assertEquals(+ ArrowsRules.isBlocked(s, piece),+ !ArrowsRules.travel(s, piece).clears,+ "arrow ${piece.id}",+ )+ }+ } }
games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt
diff --git a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.ktindex 66356fb468..5d0e9a0fc7 100644--- a/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt+++ b/games/sudoku/src/main/java/com/vayunmathur/games/sudoku/ui/components/NumberPad.kt@@ -102,7 +102,7 @@ private fun DigitKey( Text( text = label, color = scheme.onSurface,- fontSize = 19.sp,+ fontSize = FullFontSize, textAlign = TextAlign.Center, ) }@@ -114,11 +114,13 @@ private fun DigitKey( * * Lays out the full mini-grid and fills only this digit's slot, so every key positions its digit on the * same lattice — the pad reads as one cell's worth of marks spread across the keys.+ *+ * Sized well above a real pencil mark — see [NoteFontSize] — so the key stays readable, while its position+ * still tells the player exactly which mark the tap will place. */ @Composable private fun NoteSlot(digit: Int, label: String, size: BoardSize) { val slotHeight = KeySize / size.boxRows- val fontSize = (KeySize.value / size.boxCols * 0.62f).coerceIn(7f, 12f).sp val target = digit - 1 Column(Modifier.fillMaxSize()) { for (row in 0 until size.boxRows) {@@ -138,9 +140,9 @@ private fun NoteSlot(digit: Int, label: String, size: BoardSize) { Text( text = label, color = MaterialTheme.colorScheme.onSurfaceVariant,- fontSize = fontSize,- // Matched to the slot, or the glyph's own line box overflows it and- // the bottom row of a 3x4 pad is clipped.+ fontSize = NoteFontSize,+ // Matched to the slot, or the glyph's own line box overflows it and the+ // bottom row of a 3x4 pad is clipped. lineHeight = slotHeight.value.sp, fontWeight = FontWeight.Medium, textAlign = TextAlign.Center,@@ -157,3 +159,16 @@ private fun NoteSlot(digit: Int, label: String, size: BoardSize) { private val KeySize = 50.dp private val OutlineWidth = 1.dp++/** Size of a digit that a tap will write into the grid. */+private val FullFontSize = 19.sp++/**+ * Size of a digit a tap will pencil in.+ *+ * Deliberately much larger than a real pencil mark: the marks on the board are sized to fit twelve of+ * them in one cell, which is far smaller than a key needs, and a lone digit that small is hard to read.+ * Expressed as a fraction of [FullFontSize] so the two stay in step, and still fits the narrowest slot a+ * board asks for — a 12x12's 3x4 pad.+ */+private val NoteFontSize = FullFontSize * 0.7f
metadata_data/photos/games-arrows/1.png
diff --git a/metadata_data/photos/games-arrows/1.png b/metadata_data/photos/games-arrows/1.pngindex b5b94197c9..601f5b4857 100644Binary files a/metadata_data/photos/games-arrows/1.png and b/metadata_data/photos/games-arrows/1.png differ
metadata_data/photos/games-arrows/2.png
diff --git a/metadata_data/photos/games-arrows/2.png b/metadata_data/photos/games-arrows/2.pngindex 2e2aec4692..ee138d3bb8 100644Binary files a/metadata_data/photos/games-arrows/2.png and b/metadata_data/photos/games-arrows/2.png differ
metadata_data/photos/games-arrows/3.png
diff --git a/metadata_data/photos/games-arrows/3.png b/metadata_data/photos/games-arrows/3.pngindex 62180d181a..a974d03cca 100644Binary files a/metadata_data/photos/games-arrows/3.png and b/metadata_data/photos/games-arrows/3.png differ
metadata_data/photos/games-arrows/4.png
diff --git a/metadata_data/photos/games-arrows/4.png b/metadata_data/photos/games-arrows/4.pngindex 52d2339560..0f0fda893e 100644Binary files a/metadata_data/photos/games-arrows/4.png and b/metadata_data/photos/games-arrows/4.png differ
metadata_data/photos/games-sudoku/2.png
diff --git a/metadata_data/photos/games-sudoku/2.png b/metadata_data/photos/games-sudoku/2.pngindex 4c038414d2..a90c2027ae 100644Binary files a/metadata_data/photos/games-sudoku/2.png and b/metadata_data/photos/games-sudoku/2.png differ