class Theseus::Solvers::Base
The abstract superclass for solver implementations. It simply provides some helper methods that implementations would otherwise have to duplicate.
Attributes
The point (2-tuple array) at which the solution path should begin.
The point (2-tuple array) at which the solution path should end.
The maze object that this solver will provide a solution for.
Public Class Methods
Create a new solver instance for the given maze, using the given start (a
) and finish (b
) points. The solution will not be immediately generated; to do so, use the step
or solve
methods.
# File lib/theseus/solvers/base.rb, line 20 def initialize(maze, a=maze.start, b=maze.finish) @maze = maze @a = a @b = b @solution = nil end
Public Instance Methods
Returns the current (potentially partial) solution to the maze. This is for use while the algorithm is running, so that the current best-solution may be inspected (or displayed).
# File lib/theseus/solvers/base.rb, line 83 def current_solution raise NotImplementedError, "solver subclasses must implement #current_solution" end
If the maze is solved, this yields each point in the solution, in order.
If the maze has not yet been solved, this yields the result of calling step
, until the maze has been solved.
# File lib/theseus/solvers/base.rb, line 54 def each if solved? solution.each { |s| yield s } else yield s while s = step end end
Generates the solution to the maze, and returns self
. If the solution has already been generated, this does nothing.
# File lib/theseus/solvers/base.rb, line 42 def solve while !solved? step end self end
Returns true
if the solution has been generated.
# File lib/theseus/solvers/base.rb, line 28 def solved? @solution != nil end
Runs a single iteration of the solution algorithm. Returns false
if the algorithm has completed, and non-nil otherwise. The return value is algorithm-dependent.
# File lib/theseus/solvers/base.rb, line 90 def step raise NotImplementedError, "solver subclasses must implement #step" end
Returns the solution (or, if the solution is not yet fully generated, the current_solution
) as a Theseus::Path
object.
# File lib/theseus/solvers/base.rb, line 64 def to_path(options={}) path = @maze.new_path(options) prev = @maze.entrance (@solution || current_solution).each do |pt| how = path.link(prev, pt) path.set(pt, how) prev = pt end how = path.link(prev, @maze.exit) path.set(@maze.exit, how) path end