Making Hexagon (Sigma) Mazes

A maze made on a hex grid is, for some perverse reason, properly called a sigma maze. Fortunately, it works the same regardless of what you call it: just choose a maze algorithm and let it run its course. Let’s use the Recursive Backtracker algorithm to put our new to_png implementation through its paces. Put the following in a file named hex_maze.rb.

hex_maze.rb
 
require ​'recursive_backtracker'
 
require ​'hex_grid'
 
 
grid = HexGrid.new(10, 10)
 
RecursiveBacktracker.on(grid)
 
 
grid.to_png.save(​'hex.png'​)

Running it, you ought to get something like this figure. Very nice! It’s definitely working as we wanted. Experiment a bit with some other maze algorithms and see what you get, but be careful with Binary Tree ...

Get Mazes for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.