Class: Amaze::Mask
- Inherits:
-
Object
- Object
- Amaze::Mask
- Defined in:
- lib/amaze/mask.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
The rows and columns of the mask.
-
#rows ⇒ Object
readonly
The rows and columns of the mask.
Class Method Summary collapse
Instance Method Summary collapse
- #[](row, column) ⇒ Object
- #[]=(row, column, is_on) ⇒ Object
- #count ⇒ Object
-
#initialize(rows, columns) ⇒ Mask
constructor
A new instance of Mask.
- #random_location ⇒ Object
Constructor Details
#initialize(rows, columns) ⇒ Mask
Returns a new instance of Mask.
9 10 11 12 |
# File 'lib/amaze/mask.rb', line 9 def initialize rows, columns @rows, @columns = rows, columns @bits = Array.new(@rows) { Array.new(@columns, true) } end |
Instance Attribute Details
#columns ⇒ Object (readonly)
The rows and columns of the mask
7 8 9 |
# File 'lib/amaze/mask.rb', line 7 def columns @columns end |
#rows ⇒ Object (readonly)
The rows and columns of the mask
7 8 9 |
# File 'lib/amaze/mask.rb', line 7 def rows @rows end |
Class Method Details
.from_png(file) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/amaze/mask.rb', line 62 def self.from_png file image = ChunkyPNG::Image.from_file file mask = new image.height, image.width mask.rows.times do |row| mask.columns.times do |column| mask[row,column] = image[column, row] != ChunkyPNG::Color::BLACK end end mask end |
.from_txt(file) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/amaze/mask.rb', line 45 def self.from_txt file lines = File.readlines(file).map(&:strip) lines.pop while lines.last.length < 1 rows = lines.length columns = lines.first.length mask = new rows, columns mask.rows.times do |row| mask.columns.times do |column| mask[row,column] = lines[row][column] != 'X' end end mask end |
Instance Method Details
#[](row, column) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/amaze/mask.rb', line 14 def [](row, column) if row.between?(0, rows - 1) && column.between?(0, columns - 1) @bits[row][column] else false end end |
#[]=(row, column, is_on) ⇒ Object
22 23 24 |
# File 'lib/amaze/mask.rb', line 22 def []=(row, column, is_on) @bits[row][column] = is_on end |
#count ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/amaze/mask.rb', line 26 def count count = 0 rows.times do |row| columns.times do |column| count += 1 if @bits[row][column] end end count end |
#random_location ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/amaze/mask.rb', line 37 def random_location loop do row = rand(rows) column = rand(columns) return [row, column] if @bits[row][column] end end |