Class: Startupgem::Player
- Inherits:
-
Object
- Object
- Startupgem::Player
- Defined in:
- lib/startupgem/player.rb
Instance Attribute Summary collapse
-
#found_treasures ⇒ Object
readonly
Returns the value of attribute found_treasures.
-
#health ⇒ Object
Returns the value of attribute health.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other_object) ⇒ Object
- #blam ⇒ Object
- #found_treasure(treasure) ⇒ Object
-
#initialize(name, health = 100) ⇒ Player
constructor
A new instance of Player.
- #points ⇒ Object
- #print_each_treasure ⇒ Object
- #score ⇒ Object
- #strong? ⇒ Boolean
- #to_s ⇒ Object
- #w00t ⇒ Object
Constructor Details
#initialize(name, health = 100) ⇒ Player
Returns a new instance of Player.
9 10 11 12 13 |
# File 'lib/startupgem/player.rb', line 9 def initialize(name,health=100) @name = name.capitalize @health = health @found_treasures = Hash.new(0) end |
Instance Attribute Details
#found_treasures ⇒ Object (readonly)
Returns the value of attribute found_treasures.
7 8 9 |
# File 'lib/startupgem/player.rb', line 7 def found_treasures @found_treasures end |
#health ⇒ Object
Returns the value of attribute health.
6 7 8 |
# File 'lib/startupgem/player.rb', line 6 def health @health end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/startupgem/player.rb', line 7 def name @name end |
Class Method Details
Instance Method Details
#<=>(other_object) ⇒ Object
64 65 66 |
# File 'lib/startupgem/player.rb', line 64 def <=>(other_object) other_object.score <=> score end |
#blam ⇒ Object
50 51 52 53 |
# File 'lib/startupgem/player.rb', line 50 def blam @health -= 10 puts "\t\t#{@name} got blammed!" end |
#found_treasure(treasure) ⇒ Object
32 33 34 35 36 |
# File 'lib/startupgem/player.rb', line 32 def found_treasure(treasure) @found_treasures[treasure.name] += treasure.points puts "\t#{@name} found a #{treasure.name} worth #{treasure.points} points." puts "\t#{@name}'s treasures: #{@found_treasures}" end |
#points ⇒ Object
27 28 29 30 |
# File 'lib/startupgem/player.rb', line 27 def points @found_treasures.values.reduce(0) {|sum,t| sum + t } # @found_treasures.values.reduce(0,:+) end |
#print_each_treasure ⇒ Object
20 21 22 23 24 25 |
# File 'lib/startupgem/player.rb', line 20 def print_each_treasure @found_treasures.each do |key,value| treasure = Treasure.new(key,value) yield(treasure) end end |
#score ⇒ Object
38 39 40 |
# File 'lib/startupgem/player.rb', line 38 def score @health + points.to_i end |
#strong? ⇒ Boolean
42 43 44 |
# File 'lib/startupgem/player.rb', line 42 def strong? health >= 100 end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/startupgem/player.rb', line 46 def to_s "\tI'm #{@name} with a health = #{@health}, points = #{points}, score = #{score}." end |
#w00t ⇒ Object
55 56 57 58 |
# File 'lib/startupgem/player.rb', line 55 def w00t self.health += 15 puts "\t\t#{@name} got w00ted!" end |