class StudioGame::Player

Attributes

health[RW]
name[RW]

Public Class Methods

from_csv(string) click to toggle source
# File lib/studio_game/player.rb, line 63
def self.from_csv(string)
    name, health = string.split(',')
    Player.new(name, Integer(health))
end
new(name, health=100) click to toggle source

attr_writer

# File lib/studio_game/player.rb, line 15
def initialize(name, health=100)  
  #set instance variables to parameter variables(local) so that they are available to other instance methods
  @name = name.capitalize
  @health = health    
  @found_treasures = Hash.new(0)

  #Check that everything is set up:
  #puts self.inspect
end

Public Instance Methods

<=>(other) click to toggle source

Override the <=> method (the general comparison (aka spaceship) operator) in the Player class so that any time you call sort on an array of players it always returns them sorted by descending score.

# File lib/studio_game/player.rb, line 43
def <=>(other)
  other.score <=> score
end
each_found_treasure() { |treasure| ... } click to toggle source
# File lib/studio_game/player.rb, line 53
def each_found_treasure
  @found_treasures.each do |name, points|
    yield Treasure.new(name, points)
  end
end
found_treasure(treasure) click to toggle source
# File lib/studio_game/player.rb, line 47
def found_treasure(treasure)
  @found_treasures[treasure.name] += treasure.points
  puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
  puts "#{@name}'s treasures: #{@found_treasures}"
end
name=(new_name) click to toggle source
# File lib/studio_game/player.rb, line 27
def name=(new_name)
  @name = new_name.capitalize
end
points() click to toggle source
# File lib/studio_game/player.rb, line 59
def points
  @found_treasures.values.reduce(0, :+)
end
score() click to toggle source
# File lib/studio_game/player.rb, line 31
def score
  @health + points
  #@health + @name.length
end
to_s() click to toggle source
# File lib/studio_game/player.rb, line 36
def to_s
  "I'm #{@name.capitalize} with health = #{@health}, points = #{points}, and score = #{score}."
end