How rules work
A rules file describes a game as data. It doesn’t say how to draw the board or what happens when someone clicks; it says what the game’s state is, what each player may do, and what changes when they do it. The engine does the rest: it works out every legal move, applies the one a player makes, hides what each player shouldn’t see, and decides when the game is over.
The sections of a file
Section titled “The sections of a file”A rules file is one YAML document. Only dsl, game and flow are required, and a key the language doesn’t know is a
mistake the checker reports, which catches typos. The sections most games use:
| Section | Holds |
|---|---|
dsl |
The language version, such as "1.1" |
game |
The game’s id and name, how many players it takes, and its seats |
enums |
Named sets of values, such as suits or piece kinds |
consts |
Named values the rules use, such as a hand size |
boards |
Grids, graphs of connected spaces, and tracks |
components |
The kinds of component in the game, such as cards or pieces, and each one’s properties |
zones |
Where components are, such as decks, hands and board cells, and who can see them |
state |
Values the game keeps track of: for the whole game, and for each seat |
functions |
Named expressions, such as “does this seat have three in a row?” |
procedures |
Named lists of effects, your game’s own verbs |
setup |
Effects run once, to build the starting position |
flow |
The order of play: phases, rounds, turns, and steps everyone takes at once |
actions |
What players can do: what they choose, when it’s allowed, and what it changes |
triggers |
Rules that run by themselves when something happens |
end |
How the game ends and how it’s scored |
heuristic |
Game knowledge for bots, which never changes the rules (DSL 1.1) |
The rules file reference lists every section and field.
A game has seats, one per player. game.seats.names names them, and the rules refer to them with seat(name):
game: id: tic-tac-toe name: Tic-tac-toe players: { min: 2, max: 2 } seats: topology: ring names: [x, o]topology: ring means play passes around the table and wraps from the last seat to the first. Inside an action,
self is the seat taking it.
state declares the values the game keeps track of. state.global holds one of each for the whole game, and
state.seat one of each for every seat. Each is written as a type and a starting value:
state: global: tricks: "int = 0" # tricks played this hand leader: "seat? = null" heartsBroken: "bool = false" seat: score: "int = 0"Expressions read a global value as global.tricks, and a seat’s as self.score, or s.score for a seat named s.
The global. is required: a bare tricks means a local name, and the checker reports it as unknown. The ? in
seat? means the value may be null.
Components and zones
Section titled “Components and zones”A component type declares the properties each component has, such as a card’s suit and rank:
enums: suit: [clubs, diamonds, spades, hearts]
components: card: props: suit: suit rank: "int = 2" # 2 to 14, the ace highComponents are always somewhere: in a zone. A zone has a kind, the component type it holds, and a visibility that decides who sees what’s in it:
zones: deck: { kind: stack, of: card, visibility: hidden } hand: { kind: set, of: card, perSeat: true, visibility: owner } trick: { kind: list, of: card, visibility: public }perSeat: true gives every seat its own hand, reached as hand[self] or hand[s]. A stack has a top that deal
and draw take from, a list keeps its order, and a set holds its components without one. A cell zone on a board
gives every square or space its own place; Build tic-tac-toe uses one.
Hidden information and randomness covers the visibilities.
Actions
Section titled “Actions”An action is something a player can do. It has choice steps, which the player makes one after another, then effects, which change the state:
actions: play: choose: - card: { from: "hand[self]", where: "canPlay(card, self)", label: Choose a card to play } effect: - move: { what: card, to: trick }Each choice step names what’s chosen (card) and where the candidates come from (from), optionally filtered
(where). Later steps and the effects refer to it by its name. label is the prompt players see. A step can also
pick several (count: 3), be skipped (optional: true), or only apply sometimes (when). An action can also have a
require condition, which must hold for the action to be offered at all.
The engine lists every legal move from these declarations, so the web player, the bots and your own clients all offer exactly the moves the rules allow.
The flow
Section titled “The flow”flow is the order of play: a list of steps run in order, which can nest. Each step is one of these:
| Step | What it does |
|---|---|
phase with do |
Runs effects by itself, such as dealing a new hand |
phase with turns |
Seats take turns, choosing from the listed actions, until until holds |
phase with simultaneous |
Every listed seat makes one choice in secret; the choices are revealed and applied together |
repeat |
Runs its steps once for each of a list of values, setting a state field to each in turn |
loop |
Runs its steps again and again until until holds |
A turns phase says who goes first, which way play passes, how many actions make a turn, and which actions are allowed:
flow: - phase: play turns: order: { from: "seat(x)", direction: clockwise } budget: 1 actions: [mark]Effects
Section titled “Effects”Effects are the only way the state changes. Each is a one-key map, the key naming the effect. These are the ones the examples in this guide use:
| Effect | What it does |
|---|---|
set, add |
Set a state value or property, or add to it: add: { target: self.score, value: 1 } |
create |
Make a new component in a zone or on a square |
move, moveAll |
Move a component (or a list of them), or everything in a zone |
deal |
Move components one at a time from the top of a zone, such as cards to a hand |
shuffle |
Put a zone’s components in a random order |
removeFromGame |
Take a component, or a list of them, out of the game |
let |
Name a value for the effects after it |
if, forEach |
Run effects when a condition holds (then, else), or once per item |
endGame |
End the game with a winner or a draw, and a reason players see |
procedures let a game name its own verbs: a procedure is a list of effects with parameters, used like any other
effect.
Expressions
Section titled “Expressions”Conditions and values are expressions, written as YAML strings in a subset of C#:
- Values: whole numbers, text in single quotes (
'A'),true,false,null, lists[a, b]. - Operators:
+ - * / %, comparisons,&& || !, the conditionala ? b : c, and?.and??for values that may benull. - Names:
self(the acting seat),players(the number of players), your declared names, and names a step or effect binds, such as a choice step’s. - Lambdas:
c => c.suit == suit.hearts, which the list functions take:hand[s].where(c => c.rank > 10).count(). - Local names:
let best = …; …names a value inside a long expression.
Every function and method is in the standard library reference, and a file’s
functions add its own.
Some rules keep the engine deterministic, so a match replays exactly, on every server and every player’s device:
- Numbers are whole numbers. There are no fractions, and a number too big for 64 bits is an error.
/rounds toward zero, as in C#;floor(x, 2),ceil(x, 2)andround(x, 2)divide and round the way they say. - Randomness only in effects.
die(6),random(list)andlist.shuffled()may appear in an effect’s arguments, never in a function, a condition or a filter. - Expressions never change anything. Only effects do.
- Ties go to the first. Sorting keeps the order of equal items, and
min,maxandfirstreturn the first match.
One thing catches people out: ?? binds more loosely than comparisons, as in C#. x ?? 0 > 1 means
x ?? (0 > 1); write (x ?? 0) > 1.
Triggers
Section titled “Triggers”A trigger runs effects by itself when an event happens, such as after every action. Chess uses triggers for the draws the rules apply automatically, such as when the same position has occurred five times:
triggers: - event: afterAction when: "history.count(position) >= 5" do: [{ endGame: { draw: true, reason: fivefold repetition } }] - event: afterAction when: "insufficientMaterial()" do: [{ endGame: { draw: true, reason: insufficient material } }]Games can raise their own events with emit, for triggers to match.
What the checker reports
Section titled “What the checker reports”The checker reads the whole file every time you save, and as you type in the editor. Each problem says where it is and what’s wrong, and is one of these kinds:
| Kind | What it means |
|---|---|
yaml |
The file isn’t valid YAML, such as a missing colon or a wrong indent |
schema |
A section or field is missing, misspelled, or holds the wrong kind of value |
syntax |
An expression can’t be read |
reference |
A name isn’t declared: an action, effect, function, state field or local |
type |
An expression’s types don’t fit, such as adding a number to a Boolean |
determinism |
Randomness where it isn’t allowed, such as in a condition |
lint |
Something that works but should be split up, such as an expression over 6 lines or 400 characters |
limit |
The file is over a size limit |
Fix them from the top: one mistake, such as a misspelled state field, can cause several problems further down.