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?, #w00ted

Constructor Details

#initialize(name, health = 50, score = 0) ⇒ Player

Returns a new instance of Player.



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

def initialize name ,health=50,score =0
  # def initialize name ,health = rand(180...300) ,score = 0
  @name = name.capitalize
  @health = health
  @score = score
  @found_treasure = Hash.new(0)
end

Instance Attribute Details

#healthObject

Returns the value of attribute health.



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

def health
  @health
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#scoreObject

Returns the value of attribute score.



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

def score
  @score
end

Class Method Details

.from_csv(string) ⇒ Object



66
67
68
69
# File 'lib/studio_game/player.rb', line 66

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

Instance Method Details

#<=>(the_other) ⇒ Object



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

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

#each_found_treasureObject

def each_found_treasure

@found_treasures.each do |name, points|
  yield Treasure.new(name, points)

end



59
60
61
62
63
64
# File 'lib/studio_game/player.rb', line 59

def each_found_treasure
  @found_treasure.each do |name, points|
    treasure = Swag.new(name, points)
    yield treasure
  end
end

#found_treasure(treasure) ⇒ Object



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

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

#pointsObject



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

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

#say_helloObject



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

def say_hello
  "I'm #{@name.capitalize} with a health of #{@health}"
end

#to_sObject

I had to create instance variables( @name , @health) to pass variables to the status method inside this player class



25
26
27
# File 'lib/studio_game/player.rb', line 25

def to_s # player status
  "#{@name}'s current score = #{score}"
end