class Theseus::Path

The Path class is used to represent paths (and, generally, regions) within a maze. Arbitrary metadata can be associated with these paths, as well.

Although a Path can be instantiated directly, it is generally more convenient (and less error-prone) to instantiate them via Maze#new_path.

Attributes

cells[R]

Represents the cells within the Path. This is a Hash of bitfields, with bit 1 meaning the primary plane for the cell is set for this Path, and bit 2 meaning the under plane for the cell is set.

paths[R]

Represents the exit paths from each cell in the Path. This is a Hash of bitfields, and should be treated as read-only.

Public Class Methods

new(maze, meta={}) click to toggle source

Instantiates a new plane for the given maze instance, and with the given meta data. Initially, the path is empty.

# File lib/theseus/path.rb, line 19
def initialize(maze, meta={})
  @maze = maze
  @paths = Hash.new(0)
  @cells = Hash.new(0)
  @meta = meta
end

Public Instance Methods

[](key) click to toggle source

Returns the metadata for the given key.

# File lib/theseus/path.rb, line 27
def [](key)
  @meta[key]
end
add_path(path) click to toggle source

Adds all path and cell information from the parameter (which must be a Path instance) to the current Path object. The metadata from the parameter is not copied.

# File lib/theseus/path.rb, line 69
def add_path(path)
  path.paths.each do |pt, value|
    @paths[pt] |= value
  end

  path.cells.each do |pt, value|
    @cells[pt] |= value
  end
end
path?(point, direction) click to toggle source

Returns true if there is a path from the given point, in the given direction.

# File lib/theseus/path.rb, line 87
def path?(point, direction)
  @paths[point] & direction != 0
end
set(point, how=:over) click to toggle source

Marks the given point as occupied in this path. If how is :over, the point is set in the primary plane. Otherwise, it is set in the under plane.

The how parameter is usually used in conjunction with the return value of the link method:

how = path.link(from, to)
path.set(to, how)
# File lib/theseus/path.rb, line 39
def set(point, how=:over)
  @cells[point] |= (how == :over ? 1 : 2)
end
set?(point, how=:over) click to toggle source

Returns true if the given point is occuped in the path, for the given plane. If how is :over, the primary plane is queried. Otherwise, the under plane is queried.

# File lib/theseus/path.rb, line 82
def set?(point, how=:over)
  @cells[point] & (how == :over ? 1 : 2) != 0
end