Implementing Better Weaving

To make this work, we’re going to add a method to our Kruskals::State class so we can install those crossings in it. That method will be supported by a simple subclass of our WeaveGrid from the previous chapter. With those changes in hand, generating the actual maze will be really straightforward.

So, first things first, open up kruskals.rb and add the following method just after the merge method in Kruskals::State.

Line 1 
def​ add_crossing(cell)
return​ false ​if​ cell.links.any? ||
!can_merge?(cell.east, cell.west) ||
!can_merge?(cell.north, cell.south)
@neighbors.delete_if { |left,right| left == cell || right == cell }
if​ rand(2) == 0
merge(cell.west, cell)
10 
merge(cell, cell.east) ...

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.