Class: RubyGame::Players

Inherits:
Object
  • Object
show all
Includes:
Playable
Defined in:
lib/ruby_game/players_class.rb

Direct Known Subclasses

BerserkPlayer, ClumsyPlayer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Playable

#blammed, #strong?, #wooted

Constructor Details

#initialize(name, health) ⇒ Players

Returns a new instance of Players.



8
9
10
11
12
# File 'lib/ruby_game/players_class.rb', line 8

def initialize(name, health)
    @name=name.capitalize
    @health=health
    @found_treasures = Hash.new(0);
end

Instance Attribute Details

#healthObject (readonly)

Returns the value of attribute health.



6
7
8
# File 'lib/ruby_game/players_class.rb', line 6

def health
  @health
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/ruby_game/players_class.rb', line 7

def name
  @name
end

Instance Method Details

#each_treasureObject



18
19
20
21
22
# File 'lib/ruby_game/players_class.rb', line 18

def each_treasure
    @found_treasures.each do |name, points|
        yield Treasure.new(name, points)
    end
end

#found_treasure(object) ⇒ Object



13
14
15
16
17
# File 'lib/ruby_game/players_class.rb', line 13

def found_treasure(object)
    @found_treasures[object.name] += object.points 
    puts "#{@name} found a #{object.name} worth #{object.points} points."
    puts "#{@name}'s Treasures: #{@found_treasures}"
end

#pointsObject



23
24
25
# File 'lib/ruby_game/players_class.rb', line 23

def points
    @found_treasures.values.reduce(0,:+)
end

#scoreObject



26
27
28
# File 'lib/ruby_game/players_class.rb', line 26

def score
    return @health + points
end

#to_sObject



30
31
32
# File 'lib/ruby_game/players_class.rb', line 30

def to_s
    return "I'm #{@name} with health = #{@health}, points = #{points} and score = #{score}"
end