Sha256: bea93c09e37979195e1c85514158162965035cb16f56a8f7e0cd6813f29f56f5
Contents?: true
Size: 1.16 KB
Versions: 1
Compression:
Stored size: 1.16 KB
Contents
require_relative 'treasure_trove.rb' require_relative 'Playable' module StudioGame class Player include Playable attr_accessor :name, :health def initialize(name, health=100) @name = name.capitalize @health = health @found_treasures = Hash.new(0) end def self.from_csv (string) name, health = string.split(',') Player.new(name, Integer(health)) end def found_treasure(treasure) @found_treasures[treasure.name] += treasure.points puts "#{@name} found a #{treasure.name} worth #{treasure.points} points." puts "#{@name}'s treasures: #{@found_treasures}" end def each_found_treasure @found_treasures.each do |name, points| yield Treasure.new(name, points) end end def points @found_treasures.values.reduce(0, :+) end def to_s "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}." end def name=(new_name) @name = new_name.capitalize end def score @health + points end def <=>(other) other.score <=> score end end end player = StudioGame::Player.new("moe") if __FILE__ == $0 puts player.name puts player.health player.w00t puts player.health player.blam puts player.health end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
studio_game_kb-1.0 | lib/studio_game/player.rb |