Skip to main content
Core types and data structures used throughout the Game Grammar system for representing game state, events, and actions.

Action

Enum representing directional actions in the game.
from game_grammar.core import Action

class Action(Enum):
    UP = "UP"
    DOWN = "DOWN"
    LEFT = "LEFT"
    RIGHT = "RIGHT"

Values

UP
str
Move up (value: “UP”)
DOWN
str
Move down (value: “DOWN”)
LEFT
str
Move left (value: “LEFT”)
RIGHT
str
Move right (value: “RIGHT”)

Salience

Integer enum defining event priority levels for the game-grammar pipeline.
from game_grammar.core import Salience

class Salience(IntEnum):
    TICK = 0
    MOVEMENT = 1
    COLLISION = 2
    RULE_EFFECT = 3
    PHASE = 4

Values

TICK
int
Lowest priority - time tick events (value: 0)
MOVEMENT
int
Movement events (value: 1)
COLLISION
int
Collision detection events (value: 2)
RULE_EFFECT
int
Rule application effects (value: 3)
PHASE
int
Highest priority - phase transitions (value: 4)

Event

Immutable dataclass representing a game event with metadata.
from game_grammar.core import Event, Salience

@dataclass(frozen=True)
class Event:
    type: str
    entity: str
    payload: dict
    tick: int
    salience: Salience

Fields

type
str
required
The type identifier for this event (e.g., “MOVE”, “COLLISION”, “SCORE”)
entity
str
required
The entity this event is associated with (e.g., “snake”, “food”)
payload
dict
required
Additional event data as a dictionary
tick
int
required
The game tick when this event occurred
salience
Salience
required
Priority level for event processing
Event is frozen (immutable) - instances cannot be modified after creation.

Example

from game_grammar.core import Event, Salience

event = Event(
    type="COLLISION",
    entity="snake",
    payload={"position": (5, 5)},
    tick=42,
    salience=Salience.COLLISION
)

SnakeState

Mutable dataclass representing the complete state of a Snake game.
from game_grammar.core import SnakeState, Action

@dataclass
class SnakeState:
    head: tuple[int, int]
    body: list[tuple[int, int]]
    direction: Action
    food: tuple[int, int]
    score: int
    alive: bool
    tick: int

Fields

head
tuple[int, int]
required
Current (x, y) position of the snake’s head
body
list[tuple[int, int]]
required
List of (x, y) positions representing the snake’s body segments
direction
Action
required
Current movement direction of the snake
food
tuple[int, int]
required
Current (x, y) position of the food
score
int
required
Current score (typically number of food items eaten)
alive
bool
required
Whether the snake is still alive (True) or has collided (False)
tick
int
required
Current game tick counter

Example

from game_grammar.core import SnakeState, Action

state = SnakeState(
    head=(5, 5),
    body=[(4, 5), (3, 5)],
    direction=Action.RIGHT,
    food=(8, 3),
    score=2,
    alive=True,
    tick=0
)

DIR_DELTA

Mapping from Action enum to coordinate deltas for movement.
from game_grammar.core import DIR_DELTA, Action

DIR_DELTA: dict[Action, tuple[int, int]] = {
    Action.UP:    ( 0, -1),
    Action.DOWN:  ( 0,  1),
    Action.LEFT:  (-1,  0),
    Action.RIGHT: ( 1,  0),
}

Usage

Action.UP
tuple[int, int]
(0, -1) - Move up decreases y coordinate
Action.DOWN
tuple[int, int]
(0, 1) - Move down increases y coordinate
Action.LEFT
tuple[int, int]
(-1, 0) - Move left decreases x coordinate
Action.RIGHT
tuple[int, int]
(1, 0) - Move right increases x coordinate

Example

from game_grammar.core import DIR_DELTA, Action

# Calculate new position
current_pos = (5, 5)
direction = Action.RIGHT
dx, dy = DIR_DELTA[direction]
new_pos = (current_pos[0] + dx, current_pos[1] + dy)  # (6, 5)

OPPOSITE

Mapping from each Action to its opposite direction.
from game_grammar.core import OPPOSITE, Action

OPPOSITE: dict[Action, Action] = {
    Action.UP:    Action.DOWN,
    Action.DOWN:  Action.UP,
    Action.LEFT:  Action.RIGHT,
    Action.RIGHT: Action.LEFT,
}

Usage

Action.UP
Action
Action.DOWN - Opposite of up is down
Action.DOWN
Action
Action.UP - Opposite of down is up
Action.LEFT
Action
Action.RIGHT - Opposite of left is right
Action.RIGHT
Action
Action.LEFT - Opposite of right is left

Example

from game_grammar.core import OPPOSITE, Action

# Prevent 180-degree turns
current_direction = Action.RIGHT
requested_direction = Action.LEFT

if requested_direction == OPPOSITE[current_direction]:
    print("Invalid move: cannot reverse direction")

Build docs developers (and LLMs) love