Class: Pathfinder
- Inherits:
-
Object
- Object
- Pathfinder
- 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
- #destination ⇒ Room readonly
- #origin ⇒ Room readonly
Instance Method Summary collapse
-
#initialize(origin, destination) ⇒ Pathfinder
constructor
A new instance of Pathfinder.
- #path ⇒ Array<Room>
- #valid? ⇒ Boolean
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 while @path.nil? && @paths.length > 0 end end |
Instance Attribute Details
#destination ⇒ Room (readonly)
10 11 12 |
# File 'lib/gamefic-standard/pathfinder.rb', line 10 def destination @destination end |
#origin ⇒ Room (readonly)
7 8 9 |
# File 'lib/gamefic-standard/pathfinder.rb', line 7 def origin @origin end |
Instance Method Details
#path ⇒ Array<Room>
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
33 34 35 |
# File 'lib/gamefic-standard/pathfinder.rb', line 33 def valid? path.length > 0 || origin == destination end |