class Theseus::Mask

A “mask” is, conceptually, a grid of true/false values that corresponds, one-to-one, with the cells of a maze object. For every mask cell that is true, the corresponding cell in a maze may contain passages. For every mask cell that is false, the corresponding maze cell must be blank.

Any object may be used as a mask as long as it responds to height, width, and [].

Attributes

height[R]

The number of rows in the mask.

width[R]

the length of the longest row in the mask.

Public Class Methods

from_png(file_name) click to toggle source

Given a PNG file with the given file_name, read the file and create a new mask where transparent pixels will be considered true, and all others false. Note that a pixel with any transparency at all will be considered true.

The resulting mask will have the same dimensions as the image file.

# File lib/theseus/mask.rb, line 40
def self.from_png(file_name)
  image = ChunkyPNG::Image.from_file(file_name)
  grid = Array.new(image.height) { |y| Array.new(image.width) { |x| (image[x, y] & 0xff) == 0 } }
  new(grid)
end
from_text(text) click to toggle source

Given a string, treat each line as rows and each character as a cell. Every period character (“.”) will be mapped to true, and everything else to false. This lets you define simple masks as ASCII art:

mask_string = <<MASK
..........
.X....XXX.
..X....XX.
...X....X.
....X.....
.....X....
.X....X...
.XX....X..
.XXX....X.
..........
MASK

mask = Theseus::Mask.from_text(mask_string)
# File lib/theseus/mask.rb, line 31
def self.from_text(text)
  new(text.strip.split(/\n/).map { |line| line.split(//).map { |c| c == '.' } })
end
new(grid) click to toggle source

Instantiate a new mask from the given grid, which must be an Array of rows, and each row must be an Array of true/false values for each column in the row.

# File lib/theseus/mask.rb, line 54
def initialize(grid)
  @grid = grid
  @height = @grid.length
  @width = @grid.map { |row| row.length }.max
end

Public Instance Methods

[](x,y) click to toggle source

Returns the true/false value for the corresponding cell in the grid.

# File lib/theseus/mask.rb, line 61
def [](x,y)
  @grid[y][x]
end