class Theseus::Solvers::Base

The abstract superclass for solver implementations. It simply provides some helper methods that implementations would otherwise have to duplicate.

Attributes

a[R]

The point (2-tuple array) at which the solution path should begin.

b[R]

The point (2-tuple array) at which the solution path should end.

maze[R]

The maze object that this solver will provide a solution for.

Public Class Methods

new(maze, a=maze.start, b=maze.finish) click to toggle source

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

current_solution() click to toggle source

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
each() { |s| ... } click to toggle source

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
solution() click to toggle source

Returns the solution path as an array of 2-tuples, beginning with a and ending with b. If the solution has not yet been generated, this will generate the solution first, and then return it.

# File lib/theseus/solvers/base.rb, line 35
def solution
  solve unless solved?
  @solution
end
solve() click to toggle source

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
solved?() click to toggle source

Returns true if the solution has been generated.

# File lib/theseus/solvers/base.rb, line 28
def solved?
  @solution != nil
end
step() click to toggle source

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
to_path(options={}) click to toggle source

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