Roguelike: Drunkard's Walk
Generate organic cave maps with a random walker algorithm — set a starting position, pick random directions, and carve floor tiles until you hit a coverage target.
No login required.
The Walker
A drunkard's walk starts at the center of the map and stumbles outward, carving floor tiles as it goes. You'll place the walker, set up the loop, pick random directions, and carve tiles.
Random walkers are the simplest procedural generation algorithm -- they produce organic, cave-like shapes with zero configuration.
Loading game...
Click the game window to interact with it
What You'll Build
You will implement a drunkard's walk, the simplest map generator that produces interesting results. A single walker starts at the grid center and stumbles in a random cardinal direction each step, carving floor tiles wherever it lands. Because the walker backtracks over its own path unpredictably, you get winding passages, dead ends, and irregular chambers that feel hand-carved rather than algorithmic. The loop runs until roughly 40% of the grid is carved, then you walk the result. No flood fill needed here — the walker never lifts off the grid, so every carved tile is reachable from every other.
How it works
- 1
Fill the grid with walls
Every cell in the TileMapLayer starts as a wall tile. The algorithm works by subtracting from this solid block — carving floor tiles out of stone.
- 2
Place the walker at the center
The walker's starting position is Vector2i(WIDTH / 2, HEIGHT / 2) — the midpoint of the grid. This is the seed point that everything grows outward from.
- 3
Pick a random direction and move
Each step, the walker picks one of four cardinal directions (up, down, left, right) at random using rng.randi() % 4 from a constant DIRECTIONS array. It moves one tile in that direction. The randomness is what gives the cave its organic, winding shape.
- 4
Carve a floor tile
If the walker's current cell is a wall, carve it into a floor tile with set_cell() and increment the floor counter. If it's already a floor (the walker backtracked), skip the counter — this prevents double-counting and keeps the coverage calculation accurate.
- 5
Repeat until coverage target is reached
The while loop runs until the carved floor count reaches a target percentage of total cells (typically 40%). Lower targets produce tighter caves with more walls. Higher targets open the map up but the walker has to wander longer to find uncarved tiles.
- 6
Clamp to grid boundaries
After each move, clamp the position to stay one tile inside the edges. This leaves a solid wall border around the map and prevents out-of-bounds errors.
Learning Goals
- ✓Random walker algorithm with a constant direction array and modulo selection
- ✓While loop with a coverage target exit condition (carved / total < threshold)
- ✓Grid boundary clamping with Vector2i.clamp() to keep the walker in bounds
- ✓Conditional carving — only increment the counter when a wall becomes floor, preventing double-counting
GDScript Concepts in This Part
Tips
- ✨Define directions as a constant array [Vector2i.UP, DOWN, LEFT, RIGHT] and pick with rng.randi() % 4 for clean random walks.
- ✨Start with a 40-50% coverage target — too low creates sparse caves, too high fills the entire grid and you lose the cave feel.
- ✨The walker revisits already-carved tiles often, especially at high coverage. That's expected — it's what creates the organic branching pattern instead of a single long corridor.