Module: Crucigrama::Crossword::Grid
- Included in:
- Crucigrama::Crossword
- Defined in:
- lib/crucigrama/crossword/grid.rb
Overview
This module implements the behaviour associated to the crossword grid
Instance Method Summary collapse
-
#add(word, coordinates, direction) ⇒ Boolean
It adds a word to the crossword setting it on the given coordinates and direction, if it can be set.
-
#char_at(coordinates) ⇒ String
The char in the crossword at the given coordinates.
-
#dimensions ⇒ Hash<Symbol,Integer>
The horizontal and vertical dimensions of the crossword.
-
#grid ⇒ String
A raw representation for the crossword grid.
-
#grid=(grid) ⇒ Object
Builds the crossword grid specified.
-
#initialize(options = {}) ⇒ Object
Initializes an empty crossword (without words) of the given dimensions.
-
#line(coordinate, direction) ⇒ String
The line in the crossword in the given direction (row for :horizontal, column for :vertical) with the specified number.
-
#lines(direction) ⇒ Array<String>
of calling direction) on every line in the given dimension.
Instance Method Details
#add(word, coordinates, direction) ⇒ Boolean
It adds a word to the crossword setting it on the given coordinates and direction, if it can be set.
57 58 59 60 61 62 63 64 65 |
# File 'lib/crucigrama/crossword/grid.rb', line 57 def add(word, coordinates, direction) constant_coordinate = direction_other_than(direction) word.chars.with_index do |char, word_position| @grid[direction][coordinates[constant_coordinate]][coordinates[direction]+word_position] = char @grid[constant_coordinate][coordinates[direction]+word_position][coordinates[constant_coordinate]] = char end grid_modified! true end |
#char_at(coordinates) ⇒ String
Returns the char in the crossword at the given coordinates.
78 79 80 |
# File 'lib/crucigrama/crossword/grid.rb', line 78 def char_at(coordinates) @grid[:horizontal][coordinates[:vertical]][coordinates[:horizontal]] end |
#dimensions ⇒ Hash<Symbol,Integer>
Returns the horizontal and vertical dimensions of the crossword.
68 69 70 |
# File 'lib/crucigrama/crossword/grid.rb', line 68 def dimensions @dimensions ||={:horizontal =>@grid[:vertical].count, :vertical => @grid[:horizontal].count} end |
#grid ⇒ String
Returns a raw representation for the crossword grid.
41 42 43 44 45 |
# File 'lib/crucigrama/crossword/grid.rb', line 41 def grid @grid[:horizontal].collect do |row| "#{row.join}\n" end.join end |
#grid=(grid) ⇒ Object
Builds the crossword grid specified
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/crucigrama/crossword/grid.rb', line 28 def grid=(grid) @grid[:horizontal] = grid.split("\n").collect do |row| row.chars.to_a end @grid[:vertical] = @grid[:horizontal].collect.with_index do |row, i| row.collect.with_index do |char, j| @grid[:horizontal][j][i] end end grid_modified! end |
#initialize(options = {}) ⇒ Object
:dimensions option must be able to accept [x,y] representation according to Crucigrama::Positionable#position
if accept options as array of two integers [x,y]
Initializes an empty crossword (without words) of the given dimensions
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/crucigrama/crossword/grid.rb', line 13 def initialize(={}) super() . @grid = {} if [:grid] self.grid = [:grid] else dimensions = {:horizontal => 10, :vertical => 10}.merge([:dimensions]||) @grid[:horizontal] = Array.new(dimensions[:vertical].to_i){ Array.new(dimensions[:horizontal].to_i){self.class::BLACK}} @grid[:vertical] = Array.new(dimensions[:horizontal].to_i){ Array.new(dimensions[:vertical].to_i){self.class::BLACK}} end end |
#line(coordinate, direction) ⇒ String
Returns the line in the crossword in the given direction (row for :horizontal, column for :vertical) with the specified number.
87 88 89 |
# File 'lib/crucigrama/crossword/grid.rb', line 87 def line(coordinate, direction) @grid[direction][coordinate].join end |
#lines(direction) ⇒ Array<String>
of calling direction) on every line in the given dimension
94 95 96 |
# File 'lib/crucigrama/crossword/grid.rb', line 94 def lines(direction) @grid[direction].collect(&:join) end |