Class: 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



19
20
21
22
23
24
25
26
# File 'lib/subparry_labyrinth_solver/labyrinth.rb', line 19

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.



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

def height
  @height
end

#positionObject (readonly)

Returns the value of attribute position.



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

def position
  @position
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Class Method Details

.validate_data(data) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
# File 'lib/subparry_labyrinth_solver/labyrinth.rb', line 14

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:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/subparry_labyrinth_solver/labyrinth.rb', line 28

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