Class: Theseus::Mask
- Inherits:
-
Object
- Object
- Theseus::Mask
- Defined in:
- lib/theseus/mask.rb
Overview
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 #[].
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
The number of rows in the mask.
-
#width ⇒ Object
readonly
the length of the longest row in the mask.
Class Method Summary collapse
-
.from_png(file_name) ⇒ Object
Given a PNG file with the given
file_name, read the file and create a new mask where transparent pixels will be consideredtrue, and all othersfalse. -
.from_text(text) ⇒ Object
Given a string, treat each line as rows and each character as a cell.
Instance Method Summary collapse
-
#[](x, y) ⇒ Object
Returns the
true/falsevalue for the corresponding cell in the grid. -
#initialize(grid) ⇒ Mask
constructor
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.
Constructor Details
#initialize(grid) ⇒ Mask
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.
54 55 56 57 58 |
# File 'lib/theseus/mask.rb', line 54 def initialize(grid) @grid = grid @height = @grid.length @width = @grid.map { |row| row.length }.max end |
Instance Attribute Details
#height ⇒ Object (readonly)
The number of rows in the mask.
47 48 49 |
# File 'lib/theseus/mask.rb', line 47 def height @height end |
#width ⇒ Object (readonly)
the length of the longest row in the mask.
50 51 52 |
# File 'lib/theseus/mask.rb', line 50 def width @width end |
Class Method Details
.from_png(file_name) ⇒ Object
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.
40 41 42 43 44 |
# 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) ⇒ Object
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 = "..........\n.X....XXX.\n..X....XX.\n...X....X.\n....X.....\n.....X....\n.X....X...\n.XX....X..\n.XXX....X.\n..........\n"
mask = Theseus::Mask.from_text(mask_string)
31 32 33 |
# 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 |
Instance Method Details
#[](x, y) ⇒ Object
Returns the true/false value for the corresponding cell in the grid.
61 62 63 |
# File 'lib/theseus/mask.rb', line 61 def [](x,y) @grid[y][x] end |