Class: WordsMatrix::Matrix

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, min_length) ⇒ Matrix

Returns a new instance of Matrix.



4
5
6
7
8
9
# File 'lib/words_matrix/matrix.rb', line 4

def initialize(n, min_length)
  @n = n
  @min_length = min_length
  @grid = generate_grid
  parse_to_tokens!
end

Instance Attribute Details

#gridObject (readonly)

Returns the value of attribute grid.



2
3
4
# File 'lib/words_matrix/matrix.rb', line 2

def grid
  @grid
end

#tokensObject (readonly)

Returns the value of attribute tokens.



2
3
4
# File 'lib/words_matrix/matrix.rb', line 2

def tokens
  @tokens
end

Instance Method Details

#to_sObject



11
12
13
14
15
# File 'lib/words_matrix/matrix.rb', line 11

def to_s
  grid.map do |line|
    line.join(" ")
  end.join("\n")
end

#words_from(x, y) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/words_matrix/matrix.rb', line 17

def words_from(x, y)
  raise ArgumentError, "Incorrect range" unless (0..@n-1).include?(x) || (0..@n-1).include?(y)
  [
    horizontal_line((y...@n).to_a, x),
    horizontal_line((0..y).to_a.reverse, x),
    vertical_line((x...@n).to_a, y),
    vertical_line((0..x).to_a.reverse, y),
    diagonal((x...@n).to_a, y),
    diagonal((0..x).to_a.reverse, y),
    diagonal((x...@n).to_a, y, -1),
    diagonal((0..x).to_a.reverse, y, -1)
  ].flatten.compact
end