Skip to Main Content
Mazes for Programmers
book

Mazes for Programmers

by Jamis Buck
July 2015
Intermediate to advanced content levelIntermediate to advanced
286 pages
6h 31m
English
Pragmatic Bookshelf
Content preview from Mazes for Programmers

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) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Exercises for Programmers

Exercises for Programmers

Brian P. Hogan

Publisher Resources

ISBN: 9781680501315Errata Page