Class: Player

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

Direct Known Subclasses

BerserkPlayer, ClumsyPlayer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Playable

#blam, #strong?, #w00t

Constructor Details

#initialize(name, health = 100) ⇒ Player

Returns a new instance of Player.



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

def initialize(name,health=100)
  @name = name.capitalize
  @health = health
  @found_treasures = Hash.new(0)
end

Instance Attribute Details

#found_treasuresObject

Returns the value of attribute found_treasures.



5
6
7
# File 'lib/player.rb', line 5

def found_treasures
  @found_treasures
end

#hashObject

Returns the value of attribute hash.



5
6
7
# File 'lib/player.rb', line 5

def hash
  @hash
end

#healthObject



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

def health
  @health
end

Class Method Details

.from_csv(line) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/player.rb', line 12

def self.from_csv(line)
  name,health = line.split(",")
  if health.nil?
    health = "100"
  end
  Player.new(name,health.to_i)
end

Instance Method Details

#<=>(other_player) ⇒ Object



34
35
36
# File 'lib/player.rb', line 34

def <=>(other_player)
  other_player.score <=> score
end

#each_found_treasureObject



48
49
50
51
52
53
# File 'lib/player.rb', line 48

def each_found_treasure
  @found_treasures.each do |treasure|
    obj = Treasure.new(treasure.first,treasure.last)
    yield obj
  end
end

#found_treasure(treasure) ⇒ Object



37
38
39
40
# File 'lib/player.rb', line 37

def found_treasure(treasure)
  @found_treasures[treasure.name] += Integer(treasure.point)
  puts "#{name} found a treasure #{treasure.name} of worth #{treasure.point}"
end

#nameObject



25
26
27
# File 'lib/player.rb', line 25

def name
  @name
end

#name=(name) ⇒ Object



28
29
30
# File 'lib/player.rb', line 28

def name=(name)
  @name = name.capitalize
end

#pointsObject



41
42
43
44
45
46
47
# File 'lib/player.rb', line 41

def points
  all_points = 0
  @found_treasures.each do |treasure|
    all_points += treasure.last
  end
  all_points
end

#scoreObject



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

def score
  @health + self.points
end

#to_csvObject



54
55
56
# File 'lib/player.rb', line 54

def to_csv
  "#{name.ljust(10,".")} #{score}"
end

#to_sObject



19
20
21
# File 'lib/player.rb', line 19

def to_s
  "I'm #{@name} with a health of #{@health} and a score of #{score}"
end