Maze Generation: Eller's Algorithm
weblog.jamisbuck.org
weblog.jamisbuck.org
(user@pasta) /home/user/prj/ruby $ ruby backtrack-maze.rb 20 20
_______________________________________
| |_____ _ _ | _____| _| ___ |
|_ | |_ |_ |___| _____| ___ | | |
| |___|_ |___| | | _| |_| |___| |
| _______| _ | |___| _|_____|___ | |
|_ | ___ | | | | |_______ | |_ |
| _|___ | | _| |_|_ | |___|_ | |
|_____ |_| |___|_______| |_| | _____| |
|_ _|_ | _ _| | _| | | |_ |
| _| ___|___| ___|___|_ | |___|_ | |
| ___|_ _ |___ _______| | | | |_|
|_ | _|_ |_____| _ | _| |_ |_ |
| | | |_| |___ | _| _|_____|_ _| |
| | | | | |___ | | |_ | ___ | | _|
| | |_|___| | |___| _|___| | | |_ |
| |_____ | _| |___ ___| |___|___| |
| |_ | | | |___ | |___ |_______ | |
| |_ _| |_ | | |___ | _____| _| |
|_ |_| _|_____| |___ | |_________|_ |
| _| _| ___ |_| | | | | ___ | |
|_________|_________|___|___|___|_______|
backtrack-maze.rb 20 20 3751362624
(user@pasta) /home/user/prj/ruby $ ruby eller-maze.rb 20 20
_______________________________________
|_ | _|_ | | |_ | ___ ___ |
| | | _ | | | |_____| _|_|_|_| _| |_|
| | | | _ _| | _| | | |_ | |
| | | |_| | | | | | | _| |___ |
| | | |_|_|_|_| |_| |_| _| ___| |_|
| | | |_| ___ ___|_________|___| | _|
|_ | _|___| | |_____ _|_ | _| | |
|_______ | |_ _| | _| |_ | |_ _|
|_ | | |___ | _ | | | |___| | | |___|
|_ | _|___| |___ |___ | | | | | _|
| _| |_______| | _|_| _| | | |___ |
| |_____ | _| | | _| | | _| |_ | |
| | _| ___| |_ |_ | |_ | |_| |
|_ |_| | | |_ |_| |_|_| | | | | | |
|_ | | | | _| |_| | _ | | | | | | | |
|_ |_| _| | _| _| | |_|_ |_____ |
|_____ _|_ | | | | _| | _|_ |_|
| | | _ _ | ___ | _| |_ |_ |
|_ |_|___ | | |___ |_|_|___| | | | |
|___________|_|_|_____|_________________|
eller-maze.rb 20 20 873996890In fact, I just remember implementing a pygame maze app a few years ago that I should really update... http://www.pygame.org/project-PyMaze-733-.html (picture on the page).
Edit: Ha, my first paragraph is very similar to jamis'...
How do you construct a random spanning tree? One way is to use Kruskal's algorithm with random weights on the edges. See http://www.mccaughan.org.uk/software/make-maze-1.02.tar.gz for an implementation and http://www.mccaughan.org.uk/software/mazes/90x100.ps for a typical maze it generates.
(I first saw this algorithm on a web page of Olin Shivers, who had a nice Scheme implementation. Nice, but slow and memory-hungry. Mine does roughly the same as his, but in reasonably tightly-written C and with some neat tricks to make the output compact.)
Tomorrow I’ll make it into a little interactive game.