Class: TreasureHunt

Inherits:
Object
  • Object
show all
Defined in:
lib/lister.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loot) ⇒ TreasureHunt

Returns a new instance of TreasureHunt.



16
17
18
19
# File 'lib/lister.rb', line 16

def initialize(loot)
  @next_waypoint = Waypoint.new(loot)
  @num_waypoints = 1
end

Instance Attribute Details

#next_waypointObject

Returns the value of attribute next_waypoint.



14
15
16
# File 'lib/lister.rb', line 14

def next_waypoint
  @next_waypoint
end

#num_waypointsObject

Returns the value of attribute num_waypoints.



14
15
16
# File 'lib/lister.rb', line 14

def num_waypoints
  @num_waypoints
end

Instance Method Details

#add_waypoint(hint) ⇒ Object



21
22
23
24
25
26
# File 'lib/lister.rb', line 21

def add_waypoint(hint)
  current = Waypoint.new(hint)
  current.leads_to = @next_waypoint
  @num_waypoints += 1
  @next_waypoint = current
end

#remove(waypoint) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lister.rb', line 40

def remove(waypoint)
  current_loc = @next_waypoint
  result = nil
  prev_loc = nil
  while current_loc != nil
    if current_loc.item == waypoint.item
      result = current_loc
      prev_loc.leads_to = current_loc.leads_to
    end
    prev_loc = current_loc
    current_loc = current_loc.leads_to
  end
  return result
end

#search(value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lister.rb', line 28

def search(value)
  current_loc = @next_waypoint
  result = nil
  while current_loc != nil
    if current_loc.item == value
      result = current_loc
    end
    current_loc = current_loc.leads_to
  end
  return result
end

#to_sObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lister.rb', line 55

def to_s
  current_loc = @next_waypoint
  result = ""
  while current_loc !=nil
    item = current_loc.item
    if item.is_a?(Symbol)
      result += ":" + item.to_s + ", "
    elsif item.is_a?(String)
      result += "'" + item.to_s + "', "
    else
      result += item.to_s + ", "
    end
    current_loc = current_loc.leads_to
  end
  return result.chomp(", ")
end