Class: StudioGame::Player

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

Constructor Details

#initialize(name, health = 100) ⇒ Player

Returns a new instance of Player.



11
12
13
14
15
# File 'lib/studio_game/player.rb', line 11

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

Instance Attribute Details

#healthObject

Returns the value of attribute health.



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

def health
  @health
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#scoreObject (readonly)

Returns the value of attribute score.



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

def score
  @score
end

Class Method Details

.from_csv(line) ⇒ Object



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

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

Instance Method Details

#<=>(other_player) ⇒ Object



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

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

#each_found_treasureObject



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

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

#found_treasure(treasure) ⇒ Object



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

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

#pointsObject



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

def points
  @found_treasures.values.sum()
end

#to_pretty_sObject



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

def to_pretty_s
  formatted_name = @name.ljust(20, '.')
  "#{formatted_name} #{points}"
end

#to_sObject



17
18
19
# File 'lib/studio_game/player.rb', line 17

def to_s
  "I'm #{@name.capitalize} with health = #{@health}, points = #{points}, and score of #{score}"
end