Class: LabyrinthSolver::Labyrinth

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/subparry_labyrinth_solver/labyrinth.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Labyrinth

Returns a new instance of Labyrinth.



20
21
22
23
24
25
26
27
# 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

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



9
10
11
# File 'lib/subparry_labyrinth_solver/labyrinth.rb', line 9

def height
  @height
end

#positionObject (readonly)

Returns the value of attribute position.



9
10
11
# File 'lib/subparry_labyrinth_solver/labyrinth.rb', line 9

def position
  @position
end

#widthObject (readonly)

Returns the value of attribute width.



9
10
11
# File 'lib/subparry_labyrinth_solver/labyrinth.rb', line 9

def width
  @width
end

Class Method Details

.validate_data(data) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
# 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

Instance Method Details

#go(direction) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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