Search⌘ K

Implementing the OverCell and UnderCell Class

Explore how to implement OverCell and UnderCell classes to enable braiding and weaving in maze generation. Understand how these classes interact with the grid to create tunnels and link cells, leveraging helper methods and class polymorphism for flexible maze design.

Walking through OverCell

The following is a start to our new OverCell class. We'll put it in weave_cells.rb later on.

Ruby 3.1.2
require 'cell'
class OverCell < Cell
def neighbors
list = super
list << north.north if can_tunnel_north?
list << south.south if can_tunnel_south?
list << east.east if can_tunnel_east?
list << west.west if can_tunnel_west?
list
end
end

Code explanation

Line 5: Calling super here gets us the default set of neighbors, as computed by our original Cell class. We then append to that list, adding potential targets to tunnel to.

Line 6: Here, we add the northern neighbor of this cell, as long as it is possible to tunnel north.

Next, we'll define the helper methods (can_tunnel_north? and friends). We'll add these after our new neighbours method.

The helper methods

The can_tunnel_north? and friends methods

Ruby
def can_tunnel_north?
north && north.north &&
north.horizontal_passage?
end
def can_tunnel_south?
south && south.south &&
south.horizontal_passage?
end
def can_tunnel_east?
east && east.east &&
east.vertical_passage?
end
def can_tunnel_west?
west && west.west &&
west.vertical_passage?
end

Here’s where we encode the logic that decides whether or not it’s even possible to tunnel in a given direction. Look at the ...