Class: MasmaGame::Player
- Inherits:
-
Object
- Object
- MasmaGame::Player
- Includes:
- Playable
- Defined in:
- lib/masma_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.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #each_found_hoard ⇒ Object
- #found_hoard(hoard) ⇒ Object
-
#initialize(name, health = 0) ⇒ Player
constructor
A new instance of Player.
- #points ⇒ Object
- #score ⇒ Object
- #to_s ⇒ Object
Methods included from Playable
Constructor Details
#initialize(name, health = 0) ⇒ Player
9 10 11 12 13 |
# File 'lib/masma_game/player.rb', line 9 def initialize(name, health=0) @name = name.capitalize @health = health @found_hoards = Hash.new(0) end |
Instance Attribute Details
#health ⇒ Object
Returns the value of attribute health.
7 8 9 |
# File 'lib/masma_game/player.rb', line 7 def health @health end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/masma_game/player.rb', line 7 def name @name end |
Class Method Details
.from_csv(string) ⇒ Object
45 46 47 48 |
# File 'lib/masma_game/player.rb', line 45 def self.from_csv(string) name, health = string.split(',') new(name, Integer(health)) end |
Instance Method Details
#<=>(other) ⇒ Object
36 37 38 |
# File 'lib/masma_game/player.rb', line 36 def <=>(other) other.score <=> score end |
#each_found_hoard ⇒ Object
25 26 27 28 29 30 |
# File 'lib/masma_game/player.rb', line 25 def each_found_hoard @found_hoards.each do |name, points| next_hoard = Hoard.new(name, points) yield next_hoard end end |
#found_hoard(hoard) ⇒ Object
15 16 17 18 19 |
# File 'lib/masma_game/player.rb', line 15 def found_hoard(hoard) @found_hoards[hoard.name] += hoard.points puts "#{@name} found a #{hoard.name} worth #{hoard.points} points." puts "#{@name}'s hoards: #{@found_hoards}" end |
#points ⇒ Object
21 22 23 |
# File 'lib/masma_game/player.rb', line 21 def points @found_hoards.values.reduce(0, :+) end |
#score ⇒ Object
32 33 34 |
# File 'lib/masma_game/player.rb', line 32 def score @health + points end |
#to_s ⇒ Object
40 41 42 43 |
# File 'lib/masma_game/player.rb', line 40 def to_s "I'm #{@name} with health = #{@health}, points = #{points} and score = #{score}" end |