Class: Pathfinder

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic-standard/pathfinder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination) ⇒ Pathfinder

Returns a new instance of Pathfinder.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gamefic-standard/pathfinder.rb', line 14

def initialize origin, destination
  @origin = origin
  @destination = destination
  @path = nil
  @paths = [[@origin]]
  @visited = []
  if @origin == @destination
    @path = []
  else
    embark while @path.nil? && @paths.length > 0
  end
end

Instance Attribute Details

#destinationRoom (readonly)

Returns:



12
13
14
# File 'lib/gamefic-standard/pathfinder.rb', line 12

def destination
  @destination
end

#originRoom (readonly)

Returns:



9
10
11
# File 'lib/gamefic-standard/pathfinder.rb', line 9

def origin
  @origin
end

Instance Method Details

#pathArray<Room>

Returns:



28
29
30
31
32
# File 'lib/gamefic-standard/pathfinder.rb', line 28

def path
  # @path is nil if the path is invalid, but #path should return an empty

  # array instead.

  @path || []
end

#valid?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/gamefic-standard/pathfinder.rb', line 35

def valid?
  path.length > 0 || origin == destination
end