class Player

Attributes

found_treasures[RW]
hash[RW]
health[W]

Public Class Methods

from_csv(line) click to toggle source
# File lib/player.rb, line 12
def self.from_csv(line)
  name,health = line.split(",")
  if health.nil?
    health = "100"
  end
  Player.new(name,health.to_i)
end
new(name,health=100) click to toggle source
# File lib/player.rb, line 7
def initialize(name,health=100)
  @name = name.capitalize
  @health = health
  @found_treasures = Hash.new(0)
end

Public Instance Methods

<=>(other_player) click to toggle source
# File lib/player.rb, line 34
def <=>(other_player)
  other_player.score <=> score
end
each_found_treasure() { |obj| ... } click to toggle source
# File lib/player.rb, line 48
def each_found_treasure
  @found_treasures.each do |treasure|
    obj = Treasure.new(treasure.first,treasure.last)
    yield obj
  end
end
found_treasure(treasure) click to toggle source
# File lib/player.rb, line 37
def found_treasure(treasure)
  @found_treasures[treasure.name] += Integer(treasure.point)
  puts "#{name} found a treasure #{treasure.name} of worth #{treasure.point}"
end
health() click to toggle source
# File lib/player.rb, line 22
def health
  @health
end
name() click to toggle source
# File lib/player.rb, line 25
def name
  @name
end
name=(name) click to toggle source
# File lib/player.rb, line 28
def name=(name)
  @name = name.capitalize
end
points() click to toggle source
# File lib/player.rb, line 41
def points
  all_points = 0
  @found_treasures.each do |treasure|
    all_points += treasure.last
  end
  all_points
end
score() click to toggle source
# File lib/player.rb, line 31
def score
  @health + self.points
end
to_csv() click to toggle source
# File lib/player.rb, line 54
def to_csv
  "#{name.ljust(10,".")} #{score}"
end
to_s() click to toggle source
# File lib/player.rb, line 19
def to_s
  "I'm #{@name} with a health of #{@health} and a score of #{score}"
end