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 a Mask

Let’s create a Mask class that will encapsulate the on-off state of each cell in our grid. That is to say, for each cell in the grid, our mask should be able to tell us whether or not it should be included in the maze. The following implementation does this by keeping a separate two-dimensional array of Boolean values, where false means the corresponding cell is “off the grid.”

Create a new file named mask.rb, and start it off with the following properties and constructor.

mask.rb
 
class​ Mask
 
attr_reader :rows, :columns
 
 
def​ initialize(rows, columns)
 
@rows, @columns = rows, columns
 
@bits = Array.new(@rows) { Array.new(@columns, true) }
 
end

The initialize constructor is pretty straightforward, just recording ...

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