Class: MasmaGame::Player

Inherits:
Object
  • Object
show all
Includes:
Playable
Defined in:
lib/masma_game/player.rb

Direct Known Subclasses

BluePlayer, RedPlayer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Playable

#blam, #strong?, #w00t

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

#healthObject

Returns the value of attribute health.



7
8
9
# File 'lib/masma_game/player.rb', line 7

def health
  @health
end

#nameObject

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_hoardObject



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

#pointsObject



21
22
23
# File 'lib/masma_game/player.rb', line 21

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

#scoreObject



32
33
34
# File 'lib/masma_game/player.rb', line 32

def score
  @health + points
end

#to_sObject



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