class LabyrinthSolver::Labyrinth

Labyrinth class in charge of keeping track of all nodes and performing movements

Attributes

height[R]
position[R]
width[R]

Public Class Methods

new(data) click to toggle source
# File lib/subparry_labyrinth_solver/labyrinth.rb, line 20
def initialize(data)
  Labyrinth.validate_data data

  @height = data.size
  @width = data.first.size
  @position = Point.new(0, 0)
  @nodes = data.collect { |row| row.collect { |cell| Node.new(cell) } }
end
validate_data(data) click to toggle source
# File lib/subparry_labyrinth_solver/labyrinth.rb, line 15
def self.validate_data data 
  raise ArgumentError unless data.respond_to?(:each) && data.first.respond_to?(:each)
  raise MissingCheeseError unless data.any? { |rows| rows.any? {|paths| paths[:cheese]} }
end

Public Instance Methods

go(direction) click to toggle source
# File lib/subparry_labyrinth_solver/labyrinth.rb, line 29
def go direction
  raise InvalidMoveError, "Attempted to move #{direction}" unless open? direction

  case direction
  when :up
    @position.y -= 1
  when :down
    @position.y += 1
  when :left
    @position.x -= 1
  when :right
    @position.x += 1
  end
end

Private Instance Methods

current_node() click to toggle source
# File lib/subparry_labyrinth_solver/labyrinth.rb, line 46
def current_node
  @nodes[@position.y][@position.x]
end