Sha256: a9623a94f3f254c83ec373663ac9252d472b57e2ca4d0297e6d853d1abc3c062

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require_relative "playable"

module StudioGame
  class Player
    include Playable
    attr_reader :health, :found_treasures
    attr_accessor :name

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

    def points
      @found_treasures.values.sum
    end

    def name=(new_name)
      @name = new_name.capitalize
    end

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

    def score
      @health + points
    end

    def found_treasure(name, points)
      @found_treasures[name] += points
    end

    def self.from_csv(line)
      name, health = line.split(',')
      Player.new(name, Integer(health))
    rescue ArgumentError
      puts "Ignored invalid health: #{health}"
      Player.new(name)
    end
  end
end

if __FILE__ == $0
  player = Player.new("jase")
  puts player.name
  puts player.health
  player.boost
  puts player.health
  player.drain
  puts player.health
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jadens_first_gem-1.0.0 lib/studio_game/player.rb