class StudioGame::Player

Attributes

health[RW]
name[RW]
score[RW]

Public Class Methods

new(name, health=100, score=0) click to toggle source
# File lib/studio_game/player.rb, line 8
def initialize name, health=100, score=0
    @name = name.capitalize
    @health = health
    @found_treasures = Hash.new(0)
    @score = @name.length + @health
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/studio_game/player.rb, line 32
def <=> other
    other.score <=> @score
end
each_found_treasure() { |treasure| ... } click to toggle source
# File lib/studio_game/player.rb, line 50
def each_found_treasure
    @found_treasures.each do |name, points|
        yield Treasure.new(name, points)
    end
end
found_treasure(treasure) click to toggle source
# File lib/studio_game/player.rb, line 44
def found_treasure treasure
    puts "#{@name} found a #{treasure.name} worth #{treasure.points}.".center(40)
    @found_treasures[treasure.name] += treasure.points
    puts "#{@name}'s treasures #{@found_treasures}.".center(40)
end
points() click to toggle source
# File lib/studio_game/player.rb, line 36
def points
    @found_treasures.values.reduce(0, :+)
end
random_score() click to toggle source
# File lib/studio_game/player.rb, line 19
def random_score
    rand(1..100)
end
to_s() click to toggle source
# File lib/studio_game/player.rb, line 15
def to_s
    "I'm #{@name} with a health of #{@health} and a score of #{score}"
end
use_special_attack(opponent) click to toggle source
# File lib/studio_game/player.rb, line 22
def use_special_attack opponent
    if opponent.health
        value = random_score
        opponent.health -= value
        puts "Special attack used on #{opponent.name}"
    else
        puts "Cannot attack this player"
    end
end