Coverage for src/retrospeedlab/games/hushbeat/rules.py: 100%
44 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 00:01 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 00:01 +0000
1from dataclasses import dataclass
3from retrospeedlab.games.hushbeat.codes import DASH_BIT, NO_MOVE, SOLID
4from retrospeedlab.games.hushbeat.grid import Room, neighbor
5from retrospeedlab.games.hushbeat.watch import Watch, spotted
7DIRECTION_MASK = 0x0F
10@dataclass(slots=True, frozen=True)
11class Outcome:
12 path: tuple[int, ...]
13 end: int
14 bumped: bool
15 caught: bool
18def move_code(direction: int, dash: bool) -> int:
19 return direction + 1 | (DASH_BIT if dash else 0)
22def heading(move: int) -> int:
23 return (move & DIRECTION_MASK) - 1
26def dashing(move: int) -> bool:
27 return bool(move & DASH_BIT)
30def loot_bit(room: Room, cell: int) -> int:
31 return 1 << room.loot.index(cell)
34def every_loot(room: Room) -> int:
35 return (1 << len(room.loot)) - 1
38def locked_cells(room: Room, taken: int) -> frozenset[int]:
39 prize = loot_bit(room, room.prize)
40 if taken & prize:
41 return frozenset()
42 if room.caged and taken | prize != every_loot(room):
43 return frozenset({room.exit, room.prize})
44 return frozenset({room.exit})
47def passable(room: Room, cell: int | None, locked: frozenset[int]) -> bool:
48 return cell is not None and room.tiles[cell] not in SOLID and cell not in locked
51def walk(room: Room, start: int, move: int, locked: frozenset[int]) -> tuple[tuple[int, ...], bool]:
52 if move == NO_MOVE:
53 return (), False
54 first = neighbor(start, heading(move))
55 if first is None or not passable(room, first, locked):
56 return (), True
57 second = neighbor(first, heading(move)) if dashing(move) else None
58 return ((first, second) if second is not None and passable(room, second, locked) else (first,)), False
61def swapped(now: Watch, before: Watch, start: int, path: tuple[int, ...]) -> bool:
62 return bool(path) and any(
63 was == path[0] and cell == start for (was, _), (cell, _) in zip(before.guards, now.guards)
64 )
67def resolve(room: Room, now: Watch, before: Watch, start: int, move: int, locked: frozenset[int]) -> Outcome:
68 path, bumped = walk(room, start, move, locked)
69 seen = any(spotted(room, now, cell) for cell in path or (start,))
70 return Outcome(path, path[-1] if path else start, bumped, seen or swapped(now, before, start, path))