Write a world¶
A world is a class that subclasses Environment. You describe the rules; the framework
handles turns, illegal moves, limits and logging. This page builds a complete world, a
number-guessing game, in under 50 lines.
The whole file¶
--8 < --"examples/guess/guess_world.py"
What each method does¶
| Method | Job |
|---|---|
reset(seed) |
Start a new episode. All randomness must come from seed. |
agents() |
The agent IDs, fixed for the episode. |
done() |
True once the episode is over. |
to_act() |
Who may act now: one agent for turn-based worlds, several for simultaneous ones. |
observe(agent) |
What that agent can see. Return only what it is allowed to see. |
actions(agent) |
The legal actions: a list, or a JSON Schema as here. |
check(agent, action) |
None if legal, else an error that names the problem and the legal choices. |
apply(agent, action) |
Make the move. Only called for legal actions. |
outcome() |
Winners and scores, once done. |
You never write step(): the base class calls check and apply, rejects out-of-turn
agents, and guarantees an illegal action never changes your state. Everything you return
must be plain JSON data.
Run it¶
Put a scenario next to the file and refer to the world by its file name:
# Run with: agentplaykit play examples/guess/scenario.yaml
# Both the world and the agent are plain .py files next to this scenario:
# no registration and no framework changes needed.
agentplay: 1
name: guess
seed: 1
world: {use: guess_world.py, config: {high: 100}}
agents:
- {id: player, use: bisect_agent.py}
agentplaykit play examples/guess/scenario.yaml
Test it against the spec¶
agentplaykit test examples/guess/guess_world.py
This runs the SPEC-001 checks (determinism under seeds, illegal moves, plain data, log
validity) and prints one ok or FAIL line each, with the requirement ID.
More¶
- Simultaneous moves: set
simultaneous = Trueand return every agent still to choose fromto_act(); seesrc/agentplaykit/envs/rps.py. - Turn chat off with
chat = False. - The built-in blank world is a commented template.
- Share a world as a package: register it under the
agentplaykit.envsentry point, or name it in anagentplaykit.yamlfile in your project (see Scenarios).