Class: StudioGame::Player
- Inherits:
-
Object
- Object
- StudioGame::Player
- Includes:
- Playable
- Defined in:
- lib/player_class.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#health ⇒ Object
Returns the value of attribute health.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
this overrides default for whenever the compare <=> operator is used on player class object.
- #each_found_treasure ⇒ Object
- #found_treasure(treasure) ⇒ Object
-
#initialize(name, health = 100) ⇒ Player
constructor
overwrites the default method when Player.new is created! Default health is now 100.
-
#points ⇒ Object
POINTS *********************.
-
#score ⇒ Object
SCORE *********************.
-
#to_s ⇒ Object
to_s overwrites/defines the default method for PUTS.
Methods included from Playable
Constructor Details
#initialize(name, health = 100) ⇒ Player
overwrites the default method when Player.new is created! Default health is now 100.
13 14 15 16 17 |
# File 'lib/player_class.rb', line 13 def initialize(name, health=100) #overwrites the default method when Player.new is created! Default health is now 100. @name = name.capitalize @health = health @found_treasures = Hash.new(0) end |
Instance Attribute Details
#health ⇒ Object
Returns the value of attribute health.
6 7 8 |
# File 'lib/player_class.rb', line 6 def health @health end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/player_class.rb', line 7 def name @name end |
Instance Method Details
#<=>(other) ⇒ Object
this overrides default for whenever the compare <=> operator is used on player class object.
41 42 43 |
# File 'lib/player_class.rb', line 41 def <=>(other) #this overrides default for whenever the compare <=> operator is used on player class object. other.score <=> score end |
#each_found_treasure ⇒ Object
45 46 47 48 49 |
# File 'lib/player_class.rb', line 45 def each_found_treasure @found_treasures.each do |name, points| yield Treasure.new(name, points) end end |
#found_treasure(treasure) ⇒ Object
19 20 21 22 23 |
# File 'lib/player_class.rb', line 19 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 |
#points ⇒ Object
POINTS *********************
25 26 27 |
# File 'lib/player_class.rb', line 25 def points @found_treasures.values.reduce(0, :+) end |
#score ⇒ Object
SCORE *********************
29 30 31 |
# File 'lib/player_class.rb', line 29 def score points + @health end |
#to_s ⇒ Object
to_s overwrites/defines the default method for PUTS.
37 38 39 |
# File 'lib/player_class.rb', line 37 def to_s #to_s overwrites/defines the default method for PUTS. "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}." end |