O'Reilly logo

Mazes for Programmers by Jamis Buck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing Recursive Division

The algorithm really is as simple as described. First, we’ll “blank out” the grid by linking every cell to its neighbors (effectively removing all interior walls), and then recursively split the grid in half by adding walls back in.

Unlike the other algorithms we’ve implemented, we’re going to break this one into a few different methods to help with the recursion. It’ll all start with our typical on(grid) method, though. Put the following in recursive_division.rb.

recursive_division.rb
Line 1 
class​ RecursiveDivision
def​ self.on(grid)
@grid = grid
@grid.each_cell ​do​ |cell|
cell.neighbors.each { |n| cell.link(n, false) }
end
10 
divide(0, 0, @grid.rows, grid.columns)
end

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required