module Doku::PuzzleOnGrid::ClassMethods

Attributes

glyph_chars[R]

This is an array of characters (strings of length 1) which are used in multi-line grid strings to represent {Puzzle.glyphs glyphs} in a square. The order of this array must correspond to the order of the {Puzzle.glyphs glyphs}. For example, for {Sudoku}, this is [‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]. This is defined in the class definition using the {#has_glyph_chars} method. @return (Array) Array of characters.

template[R]

This is a multi-line string that defines all the squares in the puzzle and the {Separators} to use to make the puzzle more readable in {#to_grid_string}. A period character (‘.’) represents a square. This is defined in the class by using the {#has_template} method. @return (String) The template string from the class definition.

Public Instance Methods

column(x, top_y=0, size=glyphs.size) click to toggle source

Selects some squares with a specific x coordinate. @param top_y (Integer) The y coordinate of the top-most square. @param size (Integer) The height of the column. @return (Array) Array of squares.

# File lib/doku/grid.rb, line 104
def column(x, top_y=0, size=glyphs.size)
  squares_matching :x => x, :y => top_y...(top_y+size)
end
coordinates_in_grid_string(square) click to toggle source

@return (Array) Array with the line number and character number

of the given square in the {#template} string.
# File lib/doku/grid.rb, line 54
def coordinates_in_grid_string(square)
  [@line_number[square.y], @char_number[square.x]]
end
glyph_char(glyph) click to toggle source

@return (String) The character that represents the given {Puzzle.glyphs glyph}.

# File lib/doku/grid.rb, line 109
def glyph_char(glyph)
  glyph_chars.at(glyphs.index(glyph) || (raise ArgumentError, "Invalid glyph #{glyph}."))
end
glyph_parse(char) click to toggle source

@return (Object) The {Puzzle.glyphs glyph} represented by the given character.

# File lib/doku/grid.rb, line 114
def glyph_parse(char)
  glyphs.at(glyph_chars.index(char.upcase) || (raise ArgumentError, "Invalid character '#{char}'."))
end
row(y, leftmost_x=0, size=glyphs.size) click to toggle source

Selects some squares with a specific y coordinate. @param leftmost_x (Integer) The x coordinate of the left-most square. @param size (Integer) The width of the row. @return (Array) Array of squares.

# File lib/doku/grid.rb, line 96
def row(y, leftmost_x=0, size=glyphs.size)
  squares_matching :x => leftmost_x...(leftmost_x+size), :y => y
end
square_group(leftmost_x, top_y, size=Math.sqrt(glyphs.size)) click to toggle source

Selects all the squares in this puzzle that are within a certain square area on the grid. @param leftmost_x (Integer) The x coordinate of the left-most column in the square area. @param top_y (Integer) The y coordinate of the top-most row in the square area. @param size (Integer) The width and height of the square. @return (Array) Array of squares.

# File lib/doku/grid.rb, line 88
def square_group(leftmost_x, top_y, size=Math.sqrt(glyphs.size))
  squares_matching :x => leftmost_x...(leftmost_x+size), :y => top_y...(top_y+size)
end
squares_matching(conditions) click to toggle source

Selects all the squares in this puzzle which match the specified condition. @param conditions (Hash) See {SquareOnGrid#matches?}. @return (Array) Array of squares.

# File lib/doku/grid.rb, line 78
def squares_matching(conditions)
  squares.select { |sq| sq.matches? conditions }
end

Private Instance Methods

define_square_on_grid(x, y, line_num, char_num) click to toggle source
# File lib/doku/grid.rb, line 134
def define_square_on_grid(x, y, line_num, char_num)
  define_square SquareOnGrid.new x, y

  @line_number ||= []
  @char_number ||= []
  @line_number[y] = line_num
  @char_number[x] = char_num
end
define_squares_from_template() click to toggle source

Using the {#template} provided for the puzzle, this method defines objects to represent each of the different squares.

# File lib/doku/grid.rb, line 145
def define_squares_from_template
  PuzzleOnGrid.parse_grid_string(template) do |char, char_number, line_number, x, y|
    if char == '.'
      define_square_on_grid x, y, line_number, char_number
    end
  end
end
has_glyph_chars(chars) click to toggle source

This is called in the class definition to define the {#glyph_chars}. This function converts the characters to upper case to make it easier to parse strings provided by the user in a case insensitive way.

# File lib/doku/grid.rb, line 130
def has_glyph_chars(chars)
  @glyph_chars = chars.collect &:upcase
end
has_template(string) click to toggle source

This is called in the class definition to define the {#template} string, thus defining which squares are in the puzzle.

# File lib/doku/grid.rb, line 122
def has_template(string)
  @template = string.freeze
  define_squares_from_template
end