Skip to main content

What is Game Grammar?

Game Grammar is a transformer-based system that learns game rules, physics, and player behaviors from raw gameplay sequences. It treats gameplay as a language — a structured sequence of events where meaning emerges through use, not explicit programming. Gameplay is atomized into discrete events (movements, collisions, collections, deaths) and tokenized into a sequence. A causal transformer trained on next-token prediction learns the rules, physics, and behavioral patterns of the game from this sequence alone.
Game (any) → Event Stream → Token Sequence → Transformer → Grammar
Built in pure Python with zero dependencies — no PyTorch, TensorFlow, or external frameworks. The entire system, including custom autograd, is implemented from scratch.

Key Motivation

The meaning of a word is its use in the language. — Wittgenstein, Philosophical Investigations §43
State has no intrinsic meaning. Meaning arises only through events — a coin is the thing that increments your score when you touch it. Entities are defined solely by their behavior under collision. The structure of what can follow what is the game’s grammar. A causal transformer learns this grammar by predicting the next event token, the same way a language model learns syntax. From this single objective it learns:
  • Physical regularities (movement is one cell per tick)
  • Rule mappings (eating triggers growth and food respawn)
  • Temporal dependencies (death ends the episode)
  • Long-horizon behavioral patterns (wall following, food chasing)
Unlike MuZero, which learns what a player should do, Game Grammar learns what players actually do — and the output is readable by construction.

How It Works

The system operates in four stages:

1. Any game emits events

A game implements a minimal protocol — reset(), step(action), legal_actions(state) — and emits structured events tagged with salience levels. The event stream layer converts state transitions into discrete, named events without knowing anything about the specific game.

2. Events become tokens

Gameplay is represented as a sequence of state transitions, not static facts. Tokens encode changes — inputs, movements, collisions, rule effects. For Snake, the vocabulary contains 74 tokens:
  • Structural markers (4): BOS, EOS, TICK, SNAP
  • Entities (3): PLAYER, FOOD, WALL
  • Directions (4): DIR_U, DIR_D, DIR_L, DIR_R
  • Inputs (4): INPUT_U, INPUT_D, INPUT_L, INPUT_R
  • Positions (20): X0-X9, Y0-Y9
  • Event types (7): MOVE, EAT, GROW, DIE_WALL, DIE_SELF, FOOD_SPAWN, SCORE
  • Values (11): V0-V10
  • Lengths (21): LEN1-LEN20, LEN_LONG
Sample token sequence:
BOS SNAP PLAYER X5 Y5 DIR_R LEN1 FOOD X8 Y6 SCORE V0
  TICK INPUT_L MOVE X4 Y5
  TICK INPUT_D MOVE X4 Y6
  TICK INPUT_D MOVE X4 Y7
  TICK INPUT_R MOVE X5 Y7
  TICK INPUT_R MOVE X6 Y7
  ...
The recommended approach is a hybrid: periodic keyframe snapshots (SNAP) + high-frequency delta events (TICK), like video codecs (I-frames + P-frames).

3. A transformer learns the grammar

Adapted from Karpathy’s microgpt — a complete GPT in pure Python with autograd.
ParameterValue
Vocabulary74 (game events)
Embedding dim32
Layers2
Context window64
Heads4
Parameters~31K
Same architecture: token + position embeddings, multi-head causal attention, RMSNorm, ReLU MLPs, Adam optimizer.

4. Archetypes emerge

The trained model predicts valid event continuations and recurrent gameplay patterns. Distinct player behaviors emerge as stable statistical regularities — trajectories biased toward power-ups vs. scoring while avoiding enemies. These behaviors are not explicitly labeled; they arise as predictive abstractions over event sequences.

Current Results

First pass — 31K parameters, 200 episodes, 5000 training steps on Snake:
MetricResult
Loss4.47 → 0.25 (random baseline: ln(74) ≈ 4.3)
Physical validity95% — moves are adjacent cells, positions in bounds
Rule validity100% — EAT→GROW+FOOD_SPAWN, DIE→EOS
Structural validity45%*
*Structural validity is low because the model often hits the 64-token context limit mid-sequence without generating EOS. The test expects complete BOS→EOS episodes — the model generates valid gameplay that simply runs longer than the context window allows.
Sampled sequences read like real Snake games. The model learned that:
  • Movement is one cell per tick
  • Eating triggers growth and food respawn
  • Death ends the episode
  • Physical boundaries constrain movement

Project Status and Roadmap

The system is game-agnostic. These games test it at increasing complexity:
GameStatusTests
1SnakeProof of ConceptMovement, growth, conditional self-collision
2Pac-ManDesignedMulti-entity, enemy AI, buff-conditional rules
3SurvivorFantasizingMassive entity scale, build paths, wave structure
4ChessAlphaGoneNo physics, turn-based, two-player

What’s working

  • Theoretical framing and premise
  • Snake game + event stream
  • Hybrid keyframe+delta tokenizer (74 tokens)
  • GameGPT transformer (31K params, pure Python)
  • Train on Snake traces — Tier 1 (valid behavior): 95% physical, 100% rule

What’s next

  • Evaluate Tier 2 (conditional rule emergence)
  • Evaluate Tier 3 (play style / archetype emergence)
  • Visualizers
  • Performance optimizations
  • Ouroboros (model generates training data for itself)
  • Pac-Man implementation and training
  • Archetype-based progression system prototype

The Endgame

Identify player archetypes from learned event grammar and use them to drive progression systems — give players unique passives and skills that complement their natural play style.
1. Player plays normally
2. Event stream records gameplay
3. Trained model classifies trace → archetype
4. Game offers skills that complement their natural style

   Wall Hugger  → "Wall Slide": speed +1 when moving parallel to a wall
   Food Chaser  → "Scent": directional arrow toward food from anywhere
   Coiler       → "Phase": pass through your own tail once per life
   Space Filler → "Momentum": every 5th move without turning is free
Not because anyone defined “berserker” — because the model found the pattern.

Next Steps

Quick Start

Get up and running in three steps

Theory

Dive into the Wittgensteinian foundation

Architecture

Explore the transformer implementation

API Reference

Read the API documentation

Build docs developers (and LLMs) love