Class: Player
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_treasures ⇒ Object
Returns the value of attribute found_treasures.
5
6
7
|
# File 'lib/player.rb', line 5
def found_treasures
@found_treasures
end
|
#hash ⇒ Object
Returns the value of attribute hash.
5
6
7
|
# File 'lib/player.rb', line 5
def hash
@hash
end
|
#health ⇒ Object
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_treasure ⇒ Object
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
|
#name ⇒ Object
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
|
#points ⇒ Object
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
|
#score ⇒ Object
31
32
33
|
# File 'lib/player.rb', line 31
def score
@health + self.points
end
|
#to_csv ⇒ Object
54
55
56
|
# File 'lib/player.rb', line 54
def to_csv
"#{name.ljust(10,".")} #{score}"
end
|
#to_s ⇒ Object
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
|