Class: StudioGame::Player
- Inherits:
-
Object
- Object
- StudioGame::Player
- Includes:
- Playable
- Defined in:
- lib/studio_game/player.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#health ⇒ Object
Returns the value of attribute health.
-
#name ⇒ Object
Returns the value of attribute name.
-
#score ⇒ Object
Returns the value of attribute score.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(the_other) ⇒ Object
-
#each_found_treasure ⇒ Object
def each_found_treasure @found_treasures.each do |name, points| yield Treasure.new(name, points) end.
- #found_treasure(treasure) ⇒ Object
-
#initialize(name, health = 50, score = 0) ⇒ Player
constructor
A new instance of Player.
- #points ⇒ Object
- #say_hello ⇒ Object
-
#to_s ⇒ Object
I had to create instance variables( @name , @health) to pass variables to the status method inside this player class.
Methods included from Playable
Constructor Details
#initialize(name, health = 50, score = 0) ⇒ Player
Returns a new instance of Player.
14 15 16 17 18 19 20 |
# File 'lib/studio_game/player.rb', line 14 def initialize name ,health=50,score =0 # def initialize name ,health = rand(180...300) ,score = 0 @name = name.capitalize @health = health @score = score @found_treasure = Hash.new(0) end |
Instance Attribute Details
#health ⇒ Object
Returns the value of attribute health.
11 12 13 |
# File 'lib/studio_game/player.rb', line 11 def health @health end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/studio_game/player.rb', line 11 def name @name end |
#score ⇒ Object
Returns the value of attribute score.
11 12 13 |
# File 'lib/studio_game/player.rb', line 11 def score @score end |
Class Method Details
.from_csv(string) ⇒ Object
66 67 68 69 |
# File 'lib/studio_game/player.rb', line 66 def self.from_csv(string) name, health = string.split(',') new(name, Integer(health)) end |
Instance Method Details
#<=>(the_other) ⇒ Object
33 34 35 |
# File 'lib/studio_game/player.rb', line 33 def <=>(the_other) the_other.score <=> score end |
#each_found_treasure ⇒ Object
def each_found_treasure
@found_treasures.each do |name, points|
yield Treasure.new(name, points)
end
59 60 61 62 63 64 |
# File 'lib/studio_game/player.rb', line 59 def each_found_treasure @found_treasure.each do |name, points| treasure = Swag.new(name, points) yield treasure end end |
#found_treasure(treasure) ⇒ Object
45 46 47 48 |
# File 'lib/studio_game/player.rb', line 45 def found_treasure(treasure) @found_treasure[treasure.name] += treasure.points puts "#{@name} got #{treasure.name} worth #{treasure.points}." end |
#points ⇒ Object
37 38 39 |
# File 'lib/studio_game/player.rb', line 37 def points @found_treasure.values.reduce(0,:+) end |
#say_hello ⇒ Object
29 30 31 |
# File 'lib/studio_game/player.rb', line 29 def say_hello "I'm #{@name.capitalize} with a health of #{@health}" end |
#to_s ⇒ Object
I had to create instance variables( @name , @health) to pass variables to the status method inside this player class
25 26 27 |
# File 'lib/studio_game/player.rb', line 25 def to_s # player status "#{@name}'s current score = #{score}" end |