Class: TextGame::Player

Inherits:
Object
  • Object
show all
Includes:
Playable
Defined in:
lib/text_game/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?, #woot

Constructor Details

#initialize(name, health = 100) ⇒ Player

Returns a new instance of Player.



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

def initialize name,health=100
	@name = name.capitalize
	@health = health
	@treasure_points = Hash.new 0
end

Instance Attribute Details

#healthObject

Returns the value of attribute health.



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

def health
  @health
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#scoreObject (readonly)

Returns the value of attribute score.



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

def score
  @score
end

Class Method Details

.from_csv(line) ⇒ Object



14
15
16
17
# File 'lib/text_game/player.rb', line 14

def self.from_csv line
	name, health = line.split(',')
	Player.new name,Integer(health)
end

Instance Method Details

#each_treasureObject



39
40
41
42
43
44
# File 'lib/text_game/player.rb', line 39

def each_treasure
	@treasure_points.each do |name, points|
		treasure = Treasure.new name, points
		yield treasure
	end
end

#found_treasure(treasure) ⇒ Object



46
47
48
49
# File 'lib/text_game/player.rb', line 46

def found_treasure treasure
	@treasure_points[treasure.name] += treasure.points
	puts "#{@name} found #{treasure.name} worth #{treasure.points}"
end

#points_accumulatedObject



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

def points_accumulated
	@treasure_points.values.reduce(0,:+)
end

#to_csvObject



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

def to_csv
	"#{@name},#{score}"
end

#to_sObject



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

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