Module: Doku::PuzzleOnGrid::ClassMethods

Included in:
Hexadoku, Hexamurai, Sudoku
Defined in:
lib/doku/grid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#glyph_charsArray (readonly)

This is an array of characters (strings of length 1) which are used in multi-line grid strings to represent glyphs in a square. The order of this array must correspond to the order of the 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.

Returns:

  • (Array)

    Array of characters.



72
73
74
# File 'lib/doku/grid.rb', line 72

def glyph_chars
  @glyph_chars
end

#templateString (readonly)

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 Doku::PuzzleOnGrid#to_grid_string. A period character (‘.’) represents a square. This is defined in the class by using the #has_template method.

Returns:

  • (String)

    The template string from the class definition.



63
64
65
# File 'lib/doku/grid.rb', line 63

def template
  @template
end

Instance Method Details

#column(x, top_y = 0, size = glyphs.size) ⇒ Array

Selects some squares with a specific x coordinate.

Parameters:

  • top_y (Integer) (defaults to: 0)

    The y coordinate of the top-most square.

  • size (Integer) (defaults to: glyphs.size)

    The height of the column.

Returns:

  • (Array)

    Array of squares.



104
105
106
# 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) ⇒ Array

Returns Array with the line number and character number of the given square in the #template string.

Returns:

  • (Array)

    Array with the line number and character number of the given square in the #template string.



54
55
56
# 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) ⇒ String

Returns The character that represents the given glyph.

Returns:

  • (String)

    The character that represents the given glyph.



109
110
111
# 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) ⇒ Object

Returns The glyph represented by the given character.

Returns:

  • (Object)

    The glyph represented by the given character.



114
115
116
# 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) ⇒ Array

Selects some squares with a specific y coordinate.

Parameters:

  • leftmost_x (Integer) (defaults to: 0)

    The x coordinate of the left-most square.

  • size (Integer) (defaults to: glyphs.size)

    The width of the row.

Returns:

  • (Array)

    Array of squares.



96
97
98
# 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)) ⇒ Array

Selects all the squares in this puzzle that are within a certain square area on the grid.

Parameters:

  • leftmost_x (Integer)

    The x coordinate of the left-most column in the square area.

  • top_y (Integer)

    The y coordinate of the top-most row in the square area.

  • size (Integer) (defaults to: Math.sqrt(glyphs.size))

    The width and height of the square.

Returns:

  • (Array)

    Array of squares.



88
89
90
# 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) ⇒ Array

Selects all the squares in this puzzle which match the specified condition.

Parameters:

Returns:

  • (Array)

    Array of squares.



78
79
80
# File 'lib/doku/grid.rb', line 78

def squares_matching(conditions)
  squares.select { |sq| sq.matches? conditions }
end