Sha256: 830726a7fee091bfc3e2cee607e7270b1b276ffb16bd701ad1cfec3b30f973a8

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require_relative 'die'
require_relative 'treasure_trove'
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 name=(new_name)
      # override the default writer method to capitalize the name passed
      @name = new_name.capitalize
    end

    def roll_the_dice
      die = Die.new
      die.roll
    end

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

    def to_s
      "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
    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|
        treasure = Treasure.new(name, points)
        yield(treasure)
      end
    end

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

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

# only run the below code if this very file is being run
# and not another file that required this one
# __FILE__ -> variable that holds the filename of this file
# $0 -> the currently running programm

if __FILE__ == $0
  player = Player.new("moe")
  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
pragma_studio_game-1.0.0 lib/studio_game/player.rb