class StudioGame::Player

Attributes

found_treasures[R]
health[RW]
name[RW]

Public Class Methods

from_csv(string) click to toggle source
# File lib/studio_game/player.rb, line 39
def self.from_csv(string)
  name, health = string.split(',')
  Player.new(name, Integer(health))
end
new(name = "Player", health = 100) click to toggle source
# File lib/studio_game/player.rb, line 13
def initialize(name = "Player", health = 100)
  @name = name.capitalize
  @health = health
  @found_treasures = Hash.new(0)
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/studio_game/player.rb, line 27
def <=>(other)
  other.score <=> score
end
find_treasure(treasure = TreasureTrove.random_treasure) click to toggle source
# File lib/studio_game/player.rb, line 44
def find_treasure(treasure = TreasureTrove.random_treasure)
  @found_treasures[treasure.name] += treasure.points

  "#{@name} found a #{treasure.name} worth #{treasure.points} points"
end
name=(new_name) click to toggle source
# File lib/studio_game/player.rb, line 23
def name=(new_name)
  @name = new_name.capitalize
end
points() click to toggle source
# File lib/studio_game/player.rb, line 35
def points
  @found_treasures.values.sum
end
score() click to toggle source
# File lib/studio_game/player.rb, line 31
def score
  @health + points
end
to_s() click to toggle source
# File lib/studio_game/player.rb, line 19
def to_s
  "#{@name}'s health = #{@health} and score = #{score}"
end