Class: TileMatrix
- Inherits:
-
Object
- Object
- TileMatrix
- Defined in:
- lib/tile_matrix.rb
Instance Method Summary collapse
- #[](col, row) ⇒ Object
- #[]=(col, row, value) ⇒ Object
- #add_column(new_column) ⇒ Object
- #add_row(new_row) ⇒ Object
- #each ⇒ Object
- #height ⇒ Object
-
#initialize ⇒ TileMatrix
constructor
A new instance of TileMatrix.
- #reduce(width, height) ⇒ Object
- #shift_down(new_row) ⇒ Object
- #shift_left(new_column) ⇒ Object
- #shift_right(new_column) ⇒ Object
- #shift_up(new_row) ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize ⇒ TileMatrix
Returns a new instance of TileMatrix.
3 4 5 |
# File 'lib/tile_matrix.rb', line 3 def initialize @columns = [] end |
Instance Method Details
#[](col, row) ⇒ Object
7 8 9 |
# File 'lib/tile_matrix.rb', line 7 def [](col, row) @columns[col][row] end |
#[]=(col, row, value) ⇒ Object
11 12 13 14 |
# File 'lib/tile_matrix.rb', line 11 def[]=(col, row, value) @columns[col] ||= [] @columns[col][row] = value end |
#add_column(new_column) ⇒ Object
63 64 65 |
# File 'lib/tile_matrix.rb', line 63 def add_column(new_column) @columns << new_column end |
#add_row(new_row) ⇒ Object
67 68 69 70 71 |
# File 'lib/tile_matrix.rb', line 67 def add_row(new_row) @columns.each_with_index do |c, i| c << new_row[i] end end |
#each ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/tile_matrix.rb', line 24 def each @columns.each_with_index do |row, col_idx| row.each_with_index do |value, row_idx| yield col_idx, row_idx, value end end end |
#height ⇒ Object
20 21 22 |
# File 'lib/tile_matrix.rb', line 20 def height @columns[0].size end |
#reduce(width, height) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/tile_matrix.rb', line 56 def reduce(width, height) @columns.slice!(width..@columns.length - 1) @columns.each do |c| c.slice!(height..c.length - 1) end end |
#shift_down(new_row) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/tile_matrix.rb', line 49 def shift_down(new_row) @columns.each_with_index do |c, i| c.delete_at(c.length - 1) c.unshift(new_row[i]) end end |
#shift_left(new_column) ⇒ Object
32 33 34 35 |
# File 'lib/tile_matrix.rb', line 32 def shift_left(new_column) @columns.shift @columns.push(new_column) end |
#shift_right(new_column) ⇒ Object
37 38 39 40 |
# File 'lib/tile_matrix.rb', line 37 def shift_right(new_column) @columns.delete_at(@columns.length - 1) @columns.unshift(new_column) end |
#shift_up(new_row) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/tile_matrix.rb', line 42 def shift_up(new_row) @columns.each_with_index do |c, i| c.shift c.push(new_row[i]) end end |
#width ⇒ Object
16 17 18 |
# File 'lib/tile_matrix.rb', line 16 def width @columns.size end |