Class: Startupgem::Player

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, health = 100) ⇒ Player

Returns a new instance of Player.



9
10
11
12
13
# File 'lib/startupgem/player.rb', line 9

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

Instance Attribute Details

#found_treasuresObject (readonly)

Returns the value of attribute found_treasures.



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

def found_treasures
  @found_treasures
end

#healthObject

Returns the value of attribute health.



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

def health
  @health
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.convert(line) ⇒ Object



15
16
17
18
# File 'lib/startupgem/player.rb', line 15

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

Instance Method Details

#<=>(other_object) ⇒ Object



64
65
66
# File 'lib/startupgem/player.rb', line 64

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

#blamObject



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

def blam
	@health -= 10
	puts "\t\t#{@name} got blammed!"
end

#found_treasure(treasure) ⇒ Object



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

def found_treasure(treasure)
	@found_treasures[treasure.name] += treasure.points
	puts "\t#{@name} found a #{treasure.name} worth #{treasure.points} points."
	puts "\t#{@name}'s treasures: #{@found_treasures}"
end

#pointsObject



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

def points
	@found_treasures.values.reduce(0) {|sum,t| sum + t }
	# @found_treasures.values.reduce(0,:+)
end


20
21
22
23
24
25
# File 'lib/startupgem/player.rb', line 20

def print_each_treasure
	@found_treasures.each do |key,value|
		treasure = Treasure.new(key,value)
		yield(treasure)
	end
end

#scoreObject



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

def score
	@health + points.to_i
end

#strong?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/startupgem/player.rb', line 42

def strong?
	health >= 100
end

#to_sObject



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

def to_s
	"\tI'm #{@name} with a health = #{@health}, points = #{points}, score = #{score}."
end

#w00tObject



55
56
57
58
# File 'lib/startupgem/player.rb', line 55

def w00t
	self.health += 15
	puts "\t\t#{@name} got w00ted!"
end