Spaces:
Running
Running
File size: 1,820 Bytes
d747bc4 581c2fc d747bc4 f7cc813 d747bc4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Literal, List, Set, Dict
Direction = Literal["H", "V"]
@dataclass(frozen=True, order=True)
class Coord:
x: int # row, 0-based
y: int # col, 0-based
def in_bounds(self, size: int) -> bool:
return 0 <= self.x < size and 0 <= self.y < size
@dataclass
class Word:
text: str
start: Coord
direction: Direction
cells: List[Coord] = field(default_factory=list)
def __post_init__(self):
self.text = self.text.upper()
if self.direction not in ("H", "V"):
raise ValueError("direction must be 'H' or 'V'")
if not self.text.isalpha():
raise ValueError("word must be alphabetic A–Z only")
# compute cells based on start and direction
length = len(self.text)
cells: List[Coord] = []
for i in range(length):
if self.direction == "H":
cells.append(Coord(self.start.x, self.start.y + i))
else:
cells.append(Coord(self.start.x + i, self.start.y))
object.__setattr__(self, "cells", cells)
@property
def length(self) -> int:
return len(self.text)
@property
def last_cell(self) -> Coord:
return self.cells[-1]
@dataclass
class Puzzle:
words: List[Word]
radar: List[Coord] = field(default_factory=list)
def __post_init__(self):
pulses = [w.last_cell for w in self.words]
self.radar = pulses
@dataclass
class GameState:
grid_size: int
puzzle: Puzzle
revealed: Set[Coord]
guessed: Set[str]
score: int
last_action: str
can_guess: bool
game_mode: Literal["standard", "too easy"] = "standard"
points_by_word: Dict[str, int] = field(default_factory=dict) |