How to Write a Game Mechanic Down So a Test Can Check It

The demo is playing itself. Press a key or a button to take over.
The bottle holds 12 squirts, at most 4 a second however fast you press. A refill takes 1.6 seconds and the water moves from the can only at its end, so walking off halfway loses nothing. An empty bottle and an empty can only puff.
Try it: Start a refill and walk off before the bar is full: the bottle and the can keep exactly what they had. Squirt until both are empty and press again: only a puff. Each cloud you stand under puts 20 into the can; once it holds 120, what does not fit shows in red.
←→ walk, stops a refill Space squirt, hold to keep on X refill the bottle
A rule you can test says what happens in every situation and for every button, in words a test can check. "Refilling takes a while and should feel fair" is a wish. "A refill takes 1.6 seconds, and the water moves at its end" is a rule. This lesson writes down the rule of the spray bottle above as states, moves and what-if questions, and then you write yours.
Also in this lesson 5 things to read up on 3 questions to check yourself
The idea
The last lesson ended with one sentence you would like to press. Before your AI builds it, write its rule down in a form that leaves nothing open. It has three parts:
- States: the few situations the mechanic can be in. At any moment exactly one of them is true.
- Moves: what each button does in each state, and which state comes next.
- What ifs: the questions a first draft forgets, such as a refill with a full bottle or a squirt in the middle of a refill. Each one gets an answer.
Why write it down, when the AI could just build it? Because every line written this way can become a test, and a test is how you find out whether the code does what you decided. In our own experiment, asking an AI to draw the states first did not make its code pass our tests more often. Running the tests was what showed which code was wrong. So the written rule is not the proof. It is what the proof is made from.
How it works
Play the game above for a minute. Wasps fly at the hive, and the beekeeper squirts water at them from a spray bottle. In the blueprint's words the bottle is the weapon and her watering can the reserve. Squirt until the bottle is empty, refill it, and watch the blue dots at the top and the number next to the can.
Step 1: the states. Four are enough:
- Ready: there is water in the bottle.
- Refilling: the bar over her hat runs.
- Empty: the bottle is empty, the can is not.
- Dry: both are empty.
On the blueprint's own page, the same game lights up the state it is in while you play.
Step 2: the moves. For every state, write what each button does. Think of it as a grid with one row per state and one column per button: every cell needs an answer. The bottle holds 12 squirts.
- Ready. Squirt: one squirt, at most one every 0.25 seconds. After the last one comes Empty, or Dry if the can is empty too. Refill: starts a refill, unless the bottle is full.
- Refilling. It takes 1.6 seconds. Squirt: nothing. Refill: nothing, it does not start again. Walk: stops the refill. At its end the water moves from the can into the bottle: as much as is missing, or as much as the can has, whichever is less.
- Empty. Squirt or refill: starts a refill.
- Dry. Squirt: only a puff. Refill: nothing. Rain fills the can: now it is Empty, no longer Dry.
Walking changes nothing in the bottle in any state but Refilling.
Step 3: the what ifs. Go through every cell and ask what a first draft would forget. Three you can try now:
- Squirt a few times, start a refill and walk off before the bar is full. The dots and the number next to the can stay exactly as they were. Walking stops a refill, and then nothing has moved: the bottle and the can keep exactly what they had.
- At the start, the can holds less than a full bottle. Squirt the bottle empty and refill: only as many dots light up as the can had, and its number turns red.
- Press refill with a full bottle. Nothing happens, and nothing on screen says so.
Look at the last one again. The rule is right, and the list has shown you a decision you had not made: should a refused refill get an answer, the way the first lesson of this course asked for every refused press?
Step 4: every line a test. Each line becomes one sentence of the form given …, when …, then …. "Given a bottle with 12 squirts, when refill is pressed, then no refill starts." "Given a refill that has run for half of its 1.6 seconds, when she walks, then the bottle and the can hold what they held before." The blueprint's page lists such lines for this mechanic, and its tested code passes every one of them. Now your AI has something to check its code against, and you have its output to read instead of its word.
What goes wrong
Asked to write the rule down, an AI tends to write something like this: "The player has limited water. Refilling takes a moment and should feel fair. Sometimes she runs dry, which adds tension." Each sentence sounds right. No test can check "sometimes", "a moment" or "feels fair".
Then the AI builds from its own reading of those words, and the holes sit where the what ifs would have been. Two of them are in the blueprint's list of what goes wrong. The water moves into the bottle when the refill starts, so walking off halfway gives a full bottle for free. Or a second press on refill starts the bar again, so a nervous player never finishes. Neither shows when you play normally. You have to walk off, or press twice, at the wrong moment.
You notice the shortcut by trying to answer one what if from the rule alone. What happens if she walks off halfway through a refill? If the written rule does not say, the AI decided, and you do not know what.
The fix is to turn each soft word into a number or a state. "A moment" becomes 1.6 seconds. "Sometimes she runs dry" becomes the state Dry, with what each button does in it. "Feels fair" is not part of the rule at all: it is a question for playing, and the next lessons ask it.
Tell your AI
Before you build [name of the mechanic], write its rule down so that a test can check every line. Do not write code yet.
1. States: list the few situations it can be in, so that at any moment exactly one is true. Give each a short name.
2. Moves: for every state, write what each input does: [list the player's buttons or gestures]. For each, give the next state, what changes and what the player sees. If a case cannot happen, write "cannot happen" and why.
3. What ifs: list the questions a first draft forgets for this mechanic, such as a press in the middle of something, a press when something is full or empty, and stopping halfway. Answer each one.
4. Mark every word that no test can check, such as "sometimes", "a while", "a bit", "fast" or "feels". Replace each with a number or a state, or tell me it is a question for playtesting.
Show me the whole rule and wait for my changes. Then write one test for every line, in the form "given ..., when ..., then ...". Build the mechanic, run the tests and show me their output. If a test fails, fix the code, not the test.
Try it
- In the game above, squirt a few times, start a refill and walk off before the bar is full. Read the dots and the number next to the can before and after: they are the same. You have just checked one line of the rule with your hands.
- Press refill with a full bottle. Later, in the middle of a refill, squirt while the bar runs. Nothing happens either time. Write both down as lines, such as "Ready with a full bottle, refill: nothing", and decide whether your player should see an answer.
- Take the sentence you kept from the last lesson. Write its states, and under each state what every button does. Every empty cell is a question nobody has decided yet. Circle every "sometimes" and "feels like" and replace it with a number or a state.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
The spray bottle of this lesson as a whole blueprint: every line of its rule as a check that passes, the mistakes a first try makes, and tested code.
Code that keeps a character or a door in exactly one state and refuses any move not on your list, with a plain reason your AI can show you.
A check your AI runs on a written-down set of states and moves: it finds where the mechanic can get stuck and which states can never happen.
Does Modelling First Make AI Game Code Correct? We Tested It
Our experiment in full: asked to draw the states first, an AI's code passed our tests no more often than without, and what that means for a written rule.
Game Programming Patterns: State
A free book chapter on a jumping heroine whose yes-or-no switches end up in mixes nobody planned, and how one state at a time with listed moves fixes it.
Check yourself: three questions
Each answer is checked and explained at once. The questions are part of the quiz, so they count for your Game Coder IQ like any other.
Which of these lines from a written-down rule can a test check?
Next lesson The Toy Test: Prototype One Mechanic Before You Build a Game
How useful was this page?
Nobody has rated this page yet.