Procedural Dungeons
A procedural dungeon is made from a seed number, so the same seed always gives the same dungeon. Rooms are placed without touching, joined left to right so all of it can be reached, then checked. A failed dungeon is made again from the same random sequence, and after enough failures a plain, safe one is used.

Paste this to your AI
Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.
Add "Procedural Dungeons" to my game.
What it should do: A new dungeon every run keeps a game fresh without a level designer for every floor. A seed makes it repeatable, so a player can share a run and you can replay a bug, and a check makes sure no dungeon is ever impossible to finish.
Build it exactly as this state machine. First check that it fits my game, then build it like this:
```mermaid
stateDiagram-v2
[*] --> placing
placing: Placing rooms
linking: Joining rooms
checking: Checking
ready: Ready
placing --> linking: enough rooms
linking --> checking: all joined
checking --> placing: failed, try again
checking --> ready: good dungeon
ready --> [*]
```
Use these starting values:
- width: 40 cells
- height: 24 cells
- roomCount: 8 rooms
- maxRoomSize: 8 cells
- minExitDistance: 25 cells
- maxAttempts: 20 tries
Watch out for these common mistakes:
- Using the clock for randomness: Tell your AI: keep the random sequence inside the dungeon's own state and start it from the seed, never from the clock.
- Starting again with the same seed: Tell your AI: when a try fails, carry on with the next numbers of the same sequence, not the seed again.
- A dungeon that cannot be finished: Tell your AI: join the rooms in a line first, then check that every floor cell can be reached and that the exit is far enough.
- A job that never ends: Tell your AI: count the tries, and after the last one use a plain dungeon that always works.
- Rooms too big for the map: Tell your AI: cut the biggest room size down to what fits inside the walls before picking.
- Too many rooms wished for: Tell your AI: stop placing after a fixed number of tries per room, and count a dungeon as too small below about three fifths of the wish.
- Odd seeds: Tell your AI: turn the seed into a whole number first, and use zero for anything that is not a number.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP
What it does
A new dungeon every run keeps a game fresh without a level designer for every floor. A seed makes it repeatable, so a player can share a run and you can replay a bug, and a check makes sure no dungeon is ever impossible to finish.
How it runs, as a diagram
Everything it can be in, and what moves it on. Your AI builds from exactly this.
How it runs, as a diagram
- Placing rooms → Joining rooms: enough rooms
- Joining rooms → Checking: all joined
- Checking → Placing rooms: failed, try again
- Checking → Ready: good dungeon
Good starting values
| Value | Start with | What it changes |
|---|---|---|
| width | 40 cells | How wide the dungeon is. Forty cells leaves room for about eight rooms with space to walk between them. |
| height | 24 cells | How tall the dungeon is. Wider than tall fits a screen better. |
| roomCount | 8 rooms | How many rooms to try for. The dungeon may end up with fewer if they do not fit, but never fewer than about three fifths of this. |
| maxRoomSize | 8 cells | The longest side a room can have. Rooms are never smaller than 3 cells. Bigger rooms make fewer, roomier dungeons. |
| minExitDistance | 25 cells | How far the exit must be from the start along the paths. A dungeon where the exit is next door is dull. |
| maxAttempts | 20 tries | How many dungeons to try before the plain one is used. Twenty is plenty on a normal map and still finishes quickly. |
What usually goes wrong
Using the clock for randomness
If the dungeon is made from the clock or a global random generator, the same seed gives a different dungeon each time and bugs cannot be replayed.
Fix: Tell your AI: keep the random sequence inside the dungeon's own state and start it from the seed, never from the clock.
Starting again with the same seed
A failed dungeon that is retried from the same seed fails in exactly the same way, for ever.
Fix: Tell your AI: when a try fails, carry on with the next numbers of the same sequence, not the seed again.
A dungeon that cannot be finished
Rooms joined by chance can leave a part cut off, and an exit placed at random can be right next to the start.
Fix: Tell your AI: join the rooms in a line first, then check that every floor cell can be reached and that the exit is far enough.
A job that never ends
If the demands cannot be met on a small map, trying again and again never finishes and the game hangs on a loading screen.
Fix: Tell your AI: count the tries, and after the last one use a plain dungeon that always works.
Rooms too big for the map
A room that is bigger than the map allows has no place to go, and the random pick of a position breaks.
Fix: Tell your AI: cut the biggest room size down to what fits inside the walls before picking.
Too many rooms wished for
A small map with a large wish for rooms never reaches it, and a dungeon with two rooms is hardly a dungeon.
Fix: Tell your AI: stop placing after a fixed number of tries per room, and count a dungeon as too small below about three fifths of the wish.
Odd seeds
A seed of zero, a negative number or a broken value can turn the random sequence into the same number every time.
Fix: Tell your AI: turn the seed into a whole number first, and use zero for anything that is not a number.
How to check it works
- The default settings. You make a dungeon twice from seed 7: Both dungeons are exactly the same.
- The default settings. You make dungeons from seeds 1 to 20: At least 18 of them are different.
- Dungeons from seeds 1 to 30. You walk out from the start room over the floor: Every floor cell is reached.
- Dungeons from seeds 100 to 129. You check every room: Each is inside the walls at the edge, none touches another, and there are never more than eight.
- The default settings and a wanted distance of 25. You measure the path from the start to the exit: It is at least 25 cells.
- A wanted exit distance of 100 on a map of 40 by 24, and three tries. You make a dungeon: It ends after the third try with the plain dungeon.
- A try that failed the check. The next try starts: It carries on with the random sequence instead of going back to the seed, and makes different rooms.
- Two different seeds that both fall back to the plain dungeon. You compare them: They are the same, and every floor cell can be reached.
- A wish for 3, 5 or 12 rooms. You make ten dungeons for each: None has more rooms than the wish.
- A dungeon that is part way through being made. You save it, load it again and finish it: It ends exactly as if it had never been saved.
Works well with
- Loot Table: Use a separate random sequence for loot, started from the same seed, so changing the loot never changes the rooms. Make the dungeon first, then roll what lies in each room from the same seed.
- Minimap and Fog of War: Start the map blank and let the player uncover the finished dungeon, so the map never shows a half-made one. Finish the dungeon before the first cell is shown.
- Enemy Patrol and Chase: Keep guards away from the start room and give them patrol routes along the corridors that join the rooms. Place the guards after the check, using the rooms and the distances it found.
Common questions
how do i make random dungeons that are always possible to finish
Place rooms at random, join them in a line from left to right, and check the result: every floor cell reachable and the exit far from the start. If the check fails, try again from the next random numbers. After a set number of tries, use a plain dungeon that always works.
what is a seed and why does a dungeon need one
A seed is the starting number of the random sequence. The same seed always gives the same dungeon, so players can share a run and you can replay a bug. Never use the clock inside the dungeon maker.
why does my random dungeon sometimes trap the player
Rooms joined by chance can leave a part cut off. Join every room to the next one in a line first, add loops afterwards, and test that every floor cell can be reached from the start.
what do i tell my ai to add procedural dungeons
Paste this: Add procedural dungeons made from a seed. The random sequence lives in the state, never the clock. Place up to 8 rooms of 3 to 8 cells that do not touch, join them from left to right plus one loop, then check that every floor cell can be reached and the exit is at least 25 cells from the start. If it fails, carry on with the same sequence. After 20 tries use a plain row of rooms.
Where it comes from
We could not find a reliable origin for this one yet.
What was checked
- 20 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
Your AI gets the code
Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it