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 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) ...

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