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.Key Motivation
The meaning of a word is its use in the language. — Wittgenstein, Philosophical Investigations §43State 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
3. A transformer learns the grammar
Adapted from Karpathy’s microgpt — a complete GPT in pure Python with autograd.| Parameter | Value |
|---|---|
| Vocabulary | 74 (game events) |
| Embedding dim | 32 |
| Layers | 2 |
| Context window | 64 |
| Heads | 4 |
| Parameters | ~31K |
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:| Metric | Result |
|---|---|
| Loss | 4.47 → 0.25 (random baseline: ln(74) ≈ 4.3) |
| Physical validity | 95% — moves are adjacent cells, positions in bounds |
| Rule validity | 100% — EAT→GROW+FOOD_SPAWN, DIE→EOS |
| Structural validity | 45%* |
- 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:| Game | Status | Tests | |
|---|---|---|---|
| 1 | Snake | Proof of Concept | Movement, growth, conditional self-collision |
| 2 | Pac-Man | Designed | Multi-entity, enemy AI, buff-conditional rules |
| 3 | Survivor | Fantasizing | Massive entity scale, build paths, wave structure |
| 4 | Chess | AlphaGone | No 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.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
