Class: Pathfinder

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

Overview

Pathfinders provide the shortest route between two locations. The

destination needs to be accessible from the origin through portals. Note
that Pathfinders do not take into account portals that characters should
not be able to traverse, such as locked doors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination) ⇒ Pathfinder

Returns a new instance of Pathfinder.



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

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:



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

def destination
  @destination
end

#originRoom (readonly)

Returns:



7
8
9
# File 'lib/gamefic-standard/pathfinder.rb', line 7

def origin
  @origin
end

Instance Method Details

#pathArray<Room>

Returns:



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

def path
  # @path is nil if the path is invalid, but #path should return an empty
  # array instead.
  @path || []
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/gamefic-standard/pathfinder.rb', line 33

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