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 |
5 | |
- | @grid.each_cell do |cell| |
- | cell.neighbors.each { |n| cell.link(n, false) } |
- | end |
- | |
10 | divide(0, 0, @grid.rows, grid.columns) |
- | end |
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.