Spielerando

TLA+ for Game Makers: Let a Checker Walk Every Path

TLA+ is a way to write down the rules of your game so that a program called TLC can walk every path through them. A playtest tries the paths someone thought of. TLC tries all of them and, when one breaks, prints the exact steps that lead there.

What is TLA+, in plain words?

Leslie Lamport, who made it, calls TLA+ a high-level language for modeling programs and systems, and says it helps find mistakes in a design before they reach the code.

For a game that means four short lists:

  • What the game remembers, such as the room you are in and whether you hold the key.
  • How it starts.
  • Every move, with when it is allowed and what it changes.
  • The rules that must always hold, such as "the player is never shut in".

TLC starts at the start, tries every allowed move from every situation, and keeps going until it has seen every situation the rules allow. It does not play your game and does not read your code. It checks a small copy of the rules, and that is exactly why it can try everything.

When does it pay off?

Whenever the order of things matters:

  • Characters that switch between modes, such as standing, running, jumping, dashing and getting hit.
  • Story branches and quests, where a choice in one chapter can leave a quest with nobody to finish it. Quests that do not get stuck shows that case.
  • Saving and loading, where a save made at the wrong moment brings back something that should be impossible.
  • Turns in multiplayer, where two players act at nearly the same time.
  • Puzzles with one-way doors, levers and keys.

It does not pay off for how a jump feels, for art, or for whether the game is fun. It is also the wrong tool for "is there any number that breaks this?": that is a question for Z3.

A puzzle that can softlock

An invented puzzle: the key lies in the cellar, and the way out is a bridge. A lever in the hall lowers the bridge, and the same lever shuts the cellar gate. The designer's rule is that the lever only moves with the key in it. Here is the whole puzzle as TLA+, in a file called Cellar.tla:

---- MODULE Cellar ----
\* An invented puzzle. The key lies in the cellar. The way out is a bridge.
\* The lever in the hall lowers the bridge and shuts the cellar gate.
VARIABLES room, hasKey, gateOpen, bridgeDown, escaped
vars == <<room, hasKey, gateOpen, bridgeDown, escaped>>

Init == /\ room = "hall"   /\ hasKey = FALSE
        /\ gateOpen = TRUE /\ bridgeDown = FALSE /\ escaped = FALSE

GoDown    == /\ room = "hall" /\ gateOpen
             /\ room' = "cellar"
             /\ UNCHANGED <<hasKey, gateOpen, bridgeDown, escaped>>
GoUp      == /\ room = "cellar" /\ gateOpen
             /\ room' = "hall"
             /\ UNCHANGED <<hasKey, gateOpen, bridgeDown, escaped>>
TakeKey   == /\ room = "cellar" /\ ~hasKey
             /\ hasKey' = TRUE
             /\ UNCHANGED <<room, gateOpen, bridgeDown, escaped>>
PullLever == /\ room = "hall" /\ ~bridgeDown
             /\ hasKey   \* rule: the lever only moves with the key in it
             /\ bridgeDown' = TRUE /\ gateOpen' = FALSE
             /\ UNCHANGED <<room, hasKey, escaped>>
Escape    == /\ room = "hall" /\ bridgeDown /\ hasKey /\ ~escaped
             /\ escaped' = TRUE
             /\ UNCHANGED <<room, hasKey, gateOpen, bridgeDown>>
Done      == escaped /\ UNCHANGED vars   \* the end screen is not a softlock

Next == GoDown \/ GoUp \/ TakeKey \/ PullLever \/ Escape \/ Done
Spec == Init /\ [][Next]_vars

\* Something that must never happen: shut in the cellar.
NotShutIn == ~(room = "cellar" /\ ~gateOpen)
====

You do not need to write this yourself. Read it like a list: each move first says when it is allowed, then what changes, and a name with a ' is the value after the move. UNCHANGED lists what stays as it was.

Next to it sits Cellar.cfg, which tells TLC what to check:

SPECIFICATION Spec
INVARIANT NotShutIn

Run it, and TLC answers (shortened):

$ java -cp tla2tools.jar tlc2.TLC Cellar
TLC2 Version 2.19 of 08 August 2024 (rev: 5a47802)
…
Model checking completed. No error has been found.
…
9 states generated, 6 distinct states found, 0 states left on queue.

Six situations exist in this puzzle, and TLC visited all of them. In none was the player shut in, and in none was the player stuck.

Break one rule on purpose

A check that cannot fail tells you nothing, so break it once. Delete the line /\ hasKey, the way a quick change might when someone wants the lever to "just work". TLC now says:

Error: Deadlock reached.
Error: The behavior up to this point is:
State 1: <Initial predicate>
/\ room = "hall"
/\ bridgeDown = FALSE
/\ hasKey = FALSE
/\ gateOpen = TRUE
/\ escaped = FALSE

State 2: <PullLever line 19, col 14 to line 21, col 51 of module Cellar>
/\ room = "hall"
/\ bridgeDown = TRUE
/\ hasKey = FALSE
/\ gateOpen = FALSE
/\ escaped = FALSE

TLC calls a situation where no move is possible a deadlock. In a puzzle, that is a softlock. The path is two steps long: the player starts in the hall and pulls the lever before fetching the key. The gate is shut, the key lies behind it, and the way out needs the key. That is also why the Done line exists: without it, TLC would report the end screen as stuck too.

The fix

There are two fixes. Put the rule back, or let the lever go back up:

PushLever == /\ room = "hall" /\ bridgeDown /\ ~escaped   \* the fix
             /\ bridgeDown' = FALSE /\ gateOpen' = TRUE
             /\ UNCHANGED <<room, hasKey, escaped>>

Add \/ PushLever to Next and run again:

Model checking completed. No error has been found.
…
12 states generated, 7 distinct states found, 0 states left on queue.

One tip: leave out moves that change nothing, like walking in circles. Otherwise a softlock hides behind them, because the player can always still "do" something.

How do I install and run it?

Three ways, from easiest to most hands-on:

  • The TLA+ extension for VS Code. The tools' own GitHub page recommends it if you want buttons and windows. It runs TLC for you and shows the path.
  • The TLA+ Toolbox, a separate program. It is still available, but the same page calls it currently unmaintained.
  • One command. Install Java 11 or newer, download tla2tools.jar from the releases on github.com/tlaplus/tlaplus, and run java -cp tla2tools.jar tlc2.TLC Cellar in the folder with both files. TLC finds Cellar.cfg by its name. Do not add -deadlock: that switches off the stuck check.

The easiest way of all is to let your AI install it and run it, as the text below asks. If you want to learn the language yourself, Lamport writes that most beginners will find his video course the best way in.

Why does this matter more as the game grows?

An AI can keep a few rules in mind at once. It cannot keep every path in mind. Ten switches that are either on or off already make 1,024 combinations, and every new lever, quest or save slot multiplies them. When an AI reads your code and says "looks good", it has followed the few paths it thought of. TLC does not get tired at path five hundred.

The file also becomes your list of what must keep working. After each change, your AI runs the check again, and if something new broke an old rule, you see the steps instead of finding them in a playtest weeks later. My game keeps breaking explains why that happens so often.

Paste this to your AI

Check the rules of my game with TLA+ and the TLC model checker. Do not just read the code.

1. Pick one part of the game where the order of things matters, for example a puzzle, a quest, saving and loading, or whose turn it is. If you do not know which, ask me.
2. Write down in plain words what this part remembers, how it starts, every move the player or the game can make, when each move is allowed and what it changes. Show me this list and ask me what is missing before you go on.
3. Write a TLA+ spec of exactly that, and a .cfg file. Keep numbers small, for example at most 3 coins, so TLC can try everything. Leave out moves that change nothing.
4. Add the rules that must always hold as invariants. Leave the deadlock check on: a state with no move left is a softlock, unless it is an ending, and endings must be marked.
5. If TLC is missing, install it (Java 11 or newer, tla2tools.jar from github.com/tlaplus/tlaplus/releases) and run it.
6. Show me what TLC printed, word for word. If it found an error, tell me the path step by step as moves a player would make in the game.
7. Break one rule on purpose and run again, to show me the check can fail. Then put the rule back.
8. For each move in the spec, name the place in my game's code that does it, with file and line. If the code does something the spec does not, say so.

Never say "looks good" or "should work". Say what TLC checked, how many states it found, and what it did not check. If you could not run TLC, say so.

Which tool for which question?

Question Tool
Can things happen in an order that breaks the game? Softlocks, quests, turns, saving TLA+ and TLC, this page
Is there any number or setting that breaks this? Balance, prices, timing Z3
Must this law hold forever, for every input and any number of moves? Lean 4

None of the three tells you whether your game is fun. If you do not know yet where your game breaks, start with My game keeps breaking.

Was this page helpful?

One click, no account. It shows us which pages help.

Next step

Check in the browser

Check your game's rules in your browser

No Java needed for a first try: the check page loads this guide's cellar puzzle, the version with the lever rule taken out and the fix, and walks every state in your browser, saying plainly that it is not TLC.

Sources

Last updated: 2026-11-25