Module: Doku::PuzzleOnGrid::ClassMethods
Instance Attribute Summary collapse
-
#glyph_chars ⇒ Array
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.
-
#template ⇒ String
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 #to_grid_string.
Instance Method Summary collapse
-
#column(x, top_y = 0, size = glyphs.size) ⇒ Array
Selects some squares with a specific x coordinate.
-
#coordinates_in_grid_string(square) ⇒ Array
Array with the line number and character number of the given square in the #template string.
-
#glyph_char(glyph) ⇒ String
The character that represents the given glyph.
-
#glyph_parse(char) ⇒ Object
The glyph represented by the given character.
-
#row(y, leftmost_x = 0, size = glyphs.size) ⇒ Array
Selects some squares with a specific y coordinate.
-
#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.
-
#squares_matching(conditions) ⇒ Array
Selects all the squares in this puzzle which match the specified condition.
Instance Attribute Details
#glyph_chars ⇒ Array (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.
72 73 74 |
# File 'lib/doku/grid.rb', line 72 def glyph_chars @glyph_chars end |
#template ⇒ String (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.
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.
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.
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.
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.
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.
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.
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.
78 79 80 |
# File 'lib/doku/grid.rb', line 78 def squares_matching(conditions) squares.select { |sq| sq.matches? conditions } end |