
My little home computer could create something that had deep structure-every square of the maze could be reached from any other-and yet it seemed to be chaotic-it carved at random and every maze was different. Eventually, every square of the grid was connected and the screen was filled with a complete, perfect maze. It filled the screen with a grid of green squares, then incrementally cut holes in the walls.

One of my earliest memories of computing is a maze generator running on my family’s Apple IIe. Pretty neat, huh? If you want to skip the prose, the code is here. Go ahead and click the little box below to see what we end up with: Sorry, you need canvas support for this demo. Alas, you are at the mercy of my wandering attention span! Instead of game loops, today we’re going to talk about possibly the most fun and challenging part of making a roguelike: generating dungeons! Well, I finally got some time to think about my roguelike again and today, I’m here to… keep you hanging. Then I got completely sidetracked by self-publishing my book, Game Programming Patterns, and forgot all about it. Several months ago I promised a follow-up to my previous blog post about turn-based game loops in my roguelike. This allows you, from a small number of parameters, to create very different looking levels, from sparse massive halls with small corridors linking them to a hive looking maze of closely interconnected chambers.īased on this algorithm, you could add wall decoration that depends on the zone, make corridors smaller or larger, make special rooms, and so on.Rooms and Mazes: A Procedural Dungeon Generator If the difference in weight is high, then the algorithm will prefer going through existing rooms and corridors, making the paths more tortuous, and with fewer choices to reach a certain destination. If the difference in weight is small, then digging new corridors will be reasonably cheap, and the algorithm will make many corridors in between rooms, with many possibilities to go from a place to another. For this, run a pathfinder to dig corridors (let's say, A*) in between each pair of rooms, giving a random chosen weight to existing spaces (rooms or other corridors) compared to space not yet carved (the walls). Then, you need to make sure that all rooms are connected to each other.

An adaptive algorithm could even define some zones that are made of large hallways, and others of very small rooms.

Random parameters will define the size of rooms and their numbers. I managed to get reasonably good looking levels by using the following algorithm, based on a square grid:įirst, create a set of rooms.
