Skip to main content
You have an authored game and you want a player to be able to play against the computer. The bot layer is built for exactly this. Five lines of bot code, three lines of wiring.

Step 1 — Add the legalActions hook (one time, per game)

The bot runtime needs to know which moves a seat may legally play. Add an enumerator to defineGame:
The engine never reads this field. Only the bot runtime does. Authors who don’t ship bots can omit it. If you cannot or do not want to modify the game definition, a bot can ship its own enumerator via the enumerate field — see Reference: bot.

Step 2 — Define the bot

rng is forked from the snapshot’s RNG and salted by bot name + seat + turn, so two bots on the same snapshot get different (but reproducible) streams.

Step 3 — Attach to a session

For a single bot opponent:
Use the returned session, not raw, in your game loop. The facade notifies the runner on every dispatch.

Step 4 — Drive the loop

whenIdle(playerID) resolves once the runner has finished thinking and dispatched (or errored). The bot watches the snapshot autonomously — you do not call decide yourself.

Recipes

Heuristic bot — score and sort

Search-based bot — alpha-beta minimax

For deterministic optimal play, recurse with simulate:
The enumerate(snapshot, toMove) helper re-runs the same legal-action logic against a simulated snapshot — define it inline or factor it out. See the tic-tac-toe bot tutorial for the complete file.

Verifying

A few moments worth running before you ship:
  • Unit: call your decide with a hand-built DecideContext and assert the chosen action is in legalActions.
  • Integration: play many random-vs-random matches and assert every match terminates with a legal winner | draw. The runner notification bus is easy to wire wrong; this catches it.
  • Deterministic regression: fix seed on createLocalSession and assert the full move log is identical across runs. Useful to detect accidental non-determinism (e.g. a bot that calls Math.random instead of rng).
The repo’s examples/games/tic-tac-toe/bots package has both unit and integration test suites you can crib from. For a richer reference, examples/games/splendor/bots ships three bot tiers (random, greedy, strategic) for a full-scale 2–4 player game. The strategic bot plans around nobles, engine balance, reserves, and opponent threats — useful as a template for your own heuristic bots that need to weigh multiple objectives in a non-trivial state space.

Cloud play

In cloud /play, run bots as separate processes that connect to the room over WebSocket using the same protocol as a human:
simulate is unavailable on hosted hosts. Search-based bots run as in-process bots (CLI, server-side sidecar) where the full snapshot is reachable. The recommended topology and supervisor pattern is in Concepts: bots.