Module: Doku::PuzzleOnGrid

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

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

Instance Method Details

#get(x, y) ⇒ Object

Gets the glyph assignment for a given square.

Parameters:

  • x (Integer)

    The x coordinate of the square.

  • x (Integer)

    The y coordinate of the square.

Returns:

  • (Object)

    The glyph assigned to that square, or nil if none is assigned.



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.

Parameters:

  • grid_string (String) (defaults to: nil)

    A multi-line string defining which glyphs are currently written in which squares. You can use Separators to make this string more readable. This parameter is provided to make it easy to manually type in puzzles. If you are not typing the puzzle in, you should probably use #set instead. A good way to type this string is to copy the class’s template and replace some of the periods with glyph_chars (e.g. replace a ‘.’ with ‘3’).



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.

Parameters:

  • x (Integer)

    The x coordinate of the square.

  • y (Integer)

    The y coordinate of the square.

  • glyph

    The glyph to assign to that square, or nil.



185
186
187
# File 'lib/doku/grid.rb', line 185

def set(x, y, glyph)
  self[SquareOnGrid.new(x, y)] = glyph
end

#to_grid_stringString

Returns A multi-line string representation of the puzzle suitable for displaying, based on the template.

Returns:

  • (String)

    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_sString

Returns The same as #to_grid_string.

Returns:



199
200
201
# File 'lib/doku/grid.rb', line 199

def to_s
  to_grid_string
end