Module: Doku::PuzzleOnGrid
Overview
This module is meant to be included in subclasses of Puzzle where the squares are arranged in a grid.
The Puzzle class contains only very abstract code, dealing with the abstract concepts of squares, glyphs, and groups. The Puzzle class can represent a wide variety of puzzles, including three-dimensional puzzles or puzzles that don’t have any particular spatial arrangement. However, most of the puzzles we are interested in studying are arranged in a grid, and this module contains code that makes it easy to define and work with those puzzles.
Every square in a PuzzleOnGrid puzzle is a SquareOnGrid with x and y coordinates to represent its position on the grid. The x coordinate is 0 for the first row, 1 for the second row, etc. The y coordinate is 0 for the first column, 1 for the second column, etc.
See the ClassMethods module for the class methods that are added to each class that includes PuzzleOnGrid.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- Separators =
These are the separators that can appear in the template string or the string argument to the #initialize to make it more readable.
['-', '+', '|']
Instance Method Summary collapse
-
#get(x, y) ⇒ Object
Gets the glyph assignment for a given square.
-
#initialize(grid_string = nil) ⇒ Object
Creates a new instance of the puzzle.
-
#set(x, y, glyph) ⇒ Object
Assigns a glyph to a square.
-
#to_grid_string ⇒ String
A multi-line string representation of the puzzle suitable for displaying, based on the template.
-
#to_s ⇒ String
The same as #to_grid_string.
Instance Method Details
#get(x, y) ⇒ Object
Gets the glyph assignment for a given square.
194 195 196 |
# File 'lib/doku/grid.rb', line 194 def get(x, y) self[SquareOnGrid.new(x, y)] end |
#initialize(grid_string = nil) ⇒ Object
Creates a new instance of the puzzle.
163 164 165 166 |
# File 'lib/doku/grid.rb', line 163 def initialize(grid_string=nil) super() parse_initial_grid_string grid_string if grid_string end |
#set(x, y, glyph) ⇒ Object
Assigns a glyph to a square. This will modify the state of the puzzle, overwriting the previous glyph assignment.
185 186 187 |
# File 'lib/doku/grid.rb', line 185 def set(x, y, glyph) self[SquareOnGrid.new(x, y)] = glyph end |
#to_grid_string ⇒ String
Returns A multi-line string representation of the puzzle suitable for displaying, based on the template.
170 171 172 173 174 175 176 177 |
# File 'lib/doku/grid.rb', line 170 def to_grid_string lines = self.class.template.split("\n") each do |square, glyph| line_number, char_number = self.class.coordinates_in_grid_string square lines[line_number][char_number] = self.class.glyph_char glyph end lines.join "\n" end |
#to_s ⇒ String
Returns The same as #to_grid_string.
199 200 201 |
# File 'lib/doku/grid.rb', line 199 def to_s to_grid_string end |