File size: 3,575 Bytes
d747bc4
 
 
49f93dd
 
 
d747bc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
581c2fc
d747bc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49f93dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d747bc4
 
49f93dd
 
 
2de3d96
 
 
d747bc4
 
 
 
 
2de3d96
 
 
 
 
 
d747bc4
 
 
 
 
 
 
 
 
 
10dda8b
49f93dd
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from __future__ import annotations

from dataclasses import dataclass, field
from typing import Literal, List, Set, Dict, Optional
from datetime import datetime
import uuid


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:
    """Puzzle configuration and metadata.
    Fields
    - words: The list of placed words.
    - radar: Points used to render the UI radar (defaults to each word's last cell).
    - may_overlap: If True, words may overlap on shared letters (e.g., a crossword-style junction).
    - spacer: (2 to -3) Controls proximity and overlap rules between distinct words:
        * spacer = 0    -> words may be directly adjacent (touching next to each other).
        * spacer = 1    -> at least 1 blank cell must separate words (no immediate adjacency).
        * spacer > 1    -> enforce that many blank cells of separation.
        * spacer < 0    -> allow overlaps on a common letter; abs(spacer) is the maximum
                            number of trailing letters each word may extend beyond the
                            shared letter (e.g., -3 allows up to 3 letters past the overlap).

    Note: These are configuration hints for the generator/logic. Enforcement is not implemented here.
    """
    words: List[Word]
    radar: List[Coord] = field(default_factory=list)
    may_overlap: bool = False
    spacer: int = 1
    # Unique identifier for this puzzle instance (used for deterministic regen and per-session assets)
    uid: str = field(default_factory=lambda: uuid.uuid4().hex)
    # Cached set of all word cells (computed once, used by is_game_over check)
    _all_word_cells: Set[Coord] = field(default_factory=set, repr=False, init=False)

    def __post_init__(self):
        pulses = [w.last_cell for w in self.words]
        self.radar = pulses

        # Pre-compute all word cells once for faster is_game_over() checks
        all_cells: Set[Coord] = set()
        for w in self.words:
            all_cells.update(w.cells)
        object.__setattr__(self, '_all_word_cells', all_cells)


@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["classic", "too easy"] = "classic"
    points_by_word: Dict[str, int] = field(default_factory=dict)
    start_time: Optional[datetime] = None
    end_time: Optional[datetime] = None