Class: StudioGame::Player

Inherits:
Object
  • Object
show all
Includes:
Playable, Printable
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 Printable

#print_health_status, #print_player_information, #print_score, #print_treasure_trove, #print_treasures

Methods included from GameStats

#print_high_scores, #print_stats, #print_strength_stats, #print_treasure_stats, #total_points

Methods included from Formatting

#format_dot_spacing, #format_section_title

Methods included from Playable

#blam, #strong?, #w00t

Constructor Details

#initialize(name = "Player", health = 100) ⇒ Player

Returns a new instance of Player.



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

def initialize(name = "Player", 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.



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

def found_treasures
  @found_treasures
end

#healthObject

Returns the value of attribute health.



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

def health
  @health
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.from_csv(string) ⇒ Object



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

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

Instance Method Details

#<=>(other) ⇒ Object



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

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

#find_treasure(treasure = TreasureTrove.random_treasure) ⇒ Object



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

def find_treasure(treasure = TreasureTrove.random_treasure)
  @found_treasures[treasure.name] += treasure.points

  "#{@name} found a #{treasure.name} worth #{treasure.points} points"
end

#pointsObject



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

def points
  @found_treasures.values.sum
end

#scoreObject



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

def score
  @health + points
end

#to_sObject



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

def to_s
  "#{@name}'s health = #{@health} and score = #{score}"
end