...

/

Implementing the OverCell and UnderCell Class

Implementing the OverCell and UnderCell Class

Learn about the implementation of tunneling by subclassing the Cell class with two different cell types: over-cells and under-cells.

Walking through OverCell

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

Press + to interact
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

Press + to interact
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 ...