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

Constructor Details

#initialize(player_name, player_health = 100) ⇒ Player

Returns a new instance of Player.



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

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

Instance Attribute Details

#healthObject

Returns the value of attribute health.



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

def health
  @health
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.from_csv(player) ⇒ Object



57
58
59
60
# File 'lib/studio_game/player.rb', line 57

def self.from_csv(player)
   playername, health= player.split(",")
   Player.new(playername, Integer(health))
end

Instance Method Details

#<=>(other_player) ⇒ Object



39
40
41
# File 'lib/studio_game/player.rb', line 39

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

#each_found_treasureObject



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

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

end

#found_treasure(treasure) ⇒ Object



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

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

#pointsObject



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

def points
    @found_treasures.values.reduce(0,:+) 
end

#scoreObject



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

def score
    @health + @found_treasures.values.reduce(0,:+)
end

#to_sObject



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

def to_s
  "I am #{@name} health = #{@health}, points = #{points}, score = #{score}"
end

#total_treasuresObject



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

def total_treasures
   @found_treasures.size
end