Class: RichEngine::Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/rich_engine/matrix.rb

Overview

A simple 2D grid utility backed by nested arrays, with convenience methods for indexing, iterating, mapping, zipping, and filling regions.

Examples:

grid = RichEngine::Matrix.new(width: 10, height: 5, fill_with: 0)
grid[2, 3] = 1
grid.fill(x: 0..2, y: 0..1, with: 9)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width: 1, height: 1, fill_with: nil) ⇒ Matrix

Builds a matrix of the given dimensions, filling every cell with +fill_with+.

Parameters:

  • width (Integer) (defaults to: 1)

    the number of columns

  • height (Integer) (defaults to: 1)

    the number of rows

  • fill_with (Object) (defaults to: nil)

    the initial value for every cell



23
24
25
# File 'lib/rich_engine/matrix.rb', line 23

def initialize(width: 1, height: 1, fill_with: nil)
  @vec = Array.new(width) { Array.new(height) { fill_with } }
end

Instance Attribute Details

#vecArray<Array>

Returns the backing nested array of rows.

Returns:

  • (Array<Array>)

    the backing nested array of rows



15
16
17
# File 'lib/rich_engine/matrix.rb', line 15

def vec
  @vec
end

Instance Method Details

#[](x, y) ⇒ Object

Reads the value at the given coordinates.

Parameters:

  • x (Integer)

    the column index

  • y (Integer)

    the row index

Returns:

  • (Object)

    the value at +(x, y)+



32
33
34
# File 'lib/rich_engine/matrix.rb', line 32

def [](x, y)
  @vec[x][y]
end

#[]=(x, y, value) ⇒ Object

Writes a value at the given coordinates.

Parameters:

  • x (Integer)

    the column index

  • y (Integer)

    the row index

  • value (Object)

    the value to store

Returns:

  • (Object)

    the stored value



42
43
44
# File 'lib/rich_engine/matrix.rb', line 42

def []=(x, y, value)
  @vec[x][y] = value
end

#any? {|cell| ... } ⇒ Boolean

Whether any cell matches the given block.

Yields:

  • (cell)

    each cell in the matrix

Returns:

  • (Boolean)

    true if the block returns truthy for any cell



50
51
52
# File 'lib/rich_engine/matrix.rb', line 50

def any?(&block)
  @vec.any? { |row| row.any?(&block) }
end

#each {|tile| ... } ⇒ void

This method returns an undefined value.

Iterates over every cell in row-major order.

Yields:

  • (tile)

    each cell value



58
59
60
61
62
63
64
# File 'lib/rich_engine/matrix.rb', line 58

def each
  @vec.each do |row|
    row.each do |tile|
      yield(tile)
    end
  end
end

#each_with_indexes {|tile, i, j| ... } ⇒ void

This method returns an undefined value.

Iterates over every cell along with its column and row indexes.

Yields:

  • (tile, i, j)

    each cell value with its column index +i+ and row index +j+



95
96
97
98
99
100
101
# File 'lib/rich_engine/matrix.rb', line 95

def each_with_indexes
  @vec.each_with_index do |row, i|
    row.each_with_index do |tile, j|
      yield(tile, i, j)
    end
  end
end

#fill(x:, y:, with:) ⇒ void

This method returns an undefined value.

Fills a cell or region with a value. +x+ and +y+ may each be a single index or any object responding to +each+ (e.g. a Range), so regions can be filled in one call.

Examples:

Fill a region

grid.fill(x: 0..2, y: 0..1, with: 9)

Parameters:

  • x (Integer, #each)

    the column index or range of columns

  • y (Integer, #each)

    the row index or range of rows

  • with (Object)

    the value to write



113
114
115
116
117
118
119
120
121
122
# File 'lib/rich_engine/matrix.rb', line 113

def fill(x:, y:, with:)
  xs = Iterable(x)
  ys = Iterable(y)

  xs.each do |x|
    ys.each do |y|
      @vec[x][y] = with
    end
  end
end

#map {|value| ... } ⇒ Array<Array>

Maps every cell through the block, returning a nested array of results.

Yields:

  • (value)

    each cell value

Returns:

  • (Array<Array>)

    a nested array of mapped values



70
71
72
73
74
# File 'lib/rich_engine/matrix.rb', line 70

def map(&block)
  @vec.map do |row|
    row.map { |value| block.call(value) }
  end
end

#zip(other) ⇒ Matrix

Pairs each cell with the cell at the same coordinates in +other+.

Parameters:

  • other (Matrix)

    another matrix of the same dimensions

Returns:

  • (Matrix)

    a new matrix whose cells are +[self_value, other_value]+ pairs



81
82
83
84
85
86
87
88
# File 'lib/rich_engine/matrix.rb', line 81

def zip(other)
  new_matrix = Matrix.new
  new_matrix.vec = @vec.map.with_index do |row, i|
    row.map.with_index { |value, j| [value, other[i, j]] }
  end

  new_matrix
end