Game rules

From the café table to code: how we turn house rules into software

Rules before looks: an approved rules document, settings for disputed rules, a deterministic rules engine, and 100,000 matches before the first player.

Ask two players at backgammon tables in two different cafés about the same rule and you will often hear two answers. That is not a mistake; it is part of the charm. Every table has its customs. Software, though, has no customs. It needs one clear rule at every moment. That is why our first step is not design, and not code. Our first step is the rule.

A document before a single line

Every game begins with a rules document written in Arabic: the setup, the turn, the legal moves, the odd situations that happen once a month, the scoring, and when the match ends. We review it clause by clause with people who really play the game, and no code is written until it is approved. When something is unclear, we do not guess; we write the question into the document and wait for the answer.

A disputed rule becomes a setting

Some rules genuinely differ between countries or even between cafés. Instead of imposing one opinion and upsetting half of the players, such a rule becomes a setting with a sensible default that the host of the table can change. The trusted rules live in code; the disputed ones are left to the people.

A rules engine that knows nothing about the screen

Each game's rules live in their own module, which knows nothing about drawing, sound or networking. It takes the state of the table and a move, and returns the new state. Dice and cards come from a random number generator that repeats exactly when given the same start. Any match can therefore be replayed move by move, on the phone and on the server, with exactly the same result.

Tests first

For every rule we write a test before the code that implements it: "if this checker is trapped, this move is illegal". We also record complete matches as references; if a change alters the outcome of any of them unintentionally, a test fails immediately.

100,000 matches before the first player

Finally, we let the computer play: 100,000 matches per game, with random but legal moves, looking for anything the rules did not anticipate, such as an illegal move accepted, a turn that never ends, or a wrong score. Only when all of them pass without a single error is the game ready to be drawn.

This approach is slower than starting with the looks. But in the end, players remember less about the colours than about whether the game "plays right".

More from the journal

An idea, a partnership, or a question?

Press, collaborations, content, anything about games. We read every message.