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

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.

Parameters:

  • word (String)

    the word to add to the crossword

  • coordinates (Hash<Symbol,Integer>)

    the coordinates of the crossword cell where the word must start

  • direction (:horizontal, :vertical)

    the direction for the word to be set on the crossword

Options Hash (coordinates):

  • :horizontal (Integer)

    a number between 0 and the horizontal dimension of the crossword minus one specifying the row of the cell where the word must start

  • :vertical (Integer)

    a number between 0 and the vertical dimension of the crossword minus one specifying the column of the cell where the word must start

Returns:

  • (Boolean)

    if the word 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.

Parameters:

  • coordinates (Hash<Symbol,Integer>)

    the coordinates for the crossword cell being queried

Options Hash (coordinates):

  • :horizontal (Integer)

    a number between 0 and the horizontal dimension of the crossword minus one specifying the row of the cell being queried

  • :vertical (Integer)

    a number between 0 and the vertical dimension of the crossword minus one specifying the column of the cell being queried

Returns:

  • (String)

    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

#dimensionsHash<Symbol,Integer>

Returns the horizontal and vertical dimensions of the crossword.

Returns:

  • (Hash<Symbol,Integer>)

    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

#gridString

Returns a raw representation for the crossword grid.

Returns:

  • (String)

    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

Parameters:

  • grid (String)

    a raw representation for the crossword grid to build



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

TODO:

:dimensions option must be able to accept [x,y] representation according to Crucigrama::Positionable#position

TODO:

if accept options as array of two integers [x,y]

Initializes an empty crossword (without words) of the given dimensions

Parameters:

  • options (Hash<Symbol,Integer>, optional) (defaults to: {})

    the dimensions of the crossword

Options Hash (options):

  • :horizontal (Integer)

    the horizontal size of the crossword, that is, how many columns it has

  • :vertical (Integer)

    the vertical size of the crossword, that is, how many rows it has

  • :dimensions (Hash)

    a Hash with the :horizontal and :vertical options described above

  • :grid (String)

    a raw representation of the crossword grid as described in #grid

  • :definitions (Hash)

    the definitions for the word in the crossword as returned by method #definitions



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/crucigrama/crossword/grid.rb', line 13

def initialize(options={})
  super(options)
  options.to_options!
  @grid = {}
  if options[:grid]
    self.grid = options[:grid]
  else
    dimensions = {:horizontal => 10, :vertical => 10}.merge(options[:dimensions]||options)
    @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.

Parameters:

  • coordinate (Integer)

    a number between 0 and the horizontal dimension of the crossword minus one specifying the queried line

  • direction (:horizontal, :vertical)

    the direction of the line being queried

Returns:

  • (String)

    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

Parameters:

  • direction (:horizontal, :vertical)

    the direction of the lines being queried

Returns:

  • (Array<String>)

    an array of lines in the given direction, that is, an array with the results



94
95
96
# File 'lib/crucigrama/crossword/grid.rb', line 94

def lines(direction)
  @grid[direction].collect(&:join)
end