class StudioGame::Player

Attributes

health[RW]
name[RW]

Public Class Methods

from_csv(line) click to toggle source
# File lib/studio_game/player.rb, line 20
def self.from_csv(line)
  name, health = line.split(',')
  health = health.nil? ? 100 : health
  Player.new(name, Integer(health))
end
new(name, health = 100) click to toggle source
# File lib/studio_game/player.rb, line 10
def initialize(name, health = 100)
  @name = name.capitalize
  @health = health
  @found_treasures = Hash.new(0)
end

Public Instance Methods

each_found_treasure() { |treasure| ... } click to toggle source
# File lib/studio_game/player.rb, line 37
def each_found_treasure
  @found_treasures.each do |name, worth|
    yield Treasure.new(name, worth)
  end
end
found_treasure(tresure) click to toggle source
# File lib/studio_game/player.rb, line 30
def found_treasure(tresure)
  puts "#{@name} found a #{tresure.name} worth #{tresure.points} points."
  @found_treasures[tresure.name] += tresure.points
  puts "#{@name}'s treasures: #{@found_treasures}."
  tresure
end
points() click to toggle source
# File lib/studio_game/player.rb, line 43
def points
  @found_treasures.values.reduce(0, :+)
end
to_csv() click to toggle source
# File lib/studio_game/player.rb, line 16
def to_csv
  "#{name},#{points}"
end
to_s() click to toggle source
# File lib/studio_game/player.rb, line 26
def to_s
  "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
end