Sha256: 361444d5e4dec711175560fb56bbdac3ac4afdf8f28e7ed2c3570161e5d59416

Contents?: true

Size: 911 Bytes

Versions: 4

Compression:

Stored size: 911 Bytes

Contents

module Tools
  class Die
    attr_accessor :sides
    # Creates a new +Die+ with a +Fixnum+ as the number of +sides+
    def initialize sides=20
      @sides = sides
    end

    # Rolls the Die and returns the result as +Fixnum+ if rolls==1 or as +Array+ of +Fixnum+
    # if rolls>1.
    def roll rolls=1
      return simple_roll if rolls==1
      results = Array.new
      rolls.times do
        results << simple_roll
      end
      return results
    end

    # Returns the Die as a string with the format Dx, where x is the number of sides. Example "D6", "D20", "D100", etc.
    def to_s
      return "D#{@sides}"
    end

    # Return true if the dice are equal, i.e. they have the same number of sides.
    def eql? die
      return false if die==nil or die.sides!=@sides
      return true
    end

    private

    #Returns a simple die roll
    def simple_roll
      1 + rand(sides)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rpg-tools-0.2.4 app/models/tools/die.rb
rpg-tools-0.2.3 app/models/tools/die.rb
rpg-tools-0.2.2 app/models/tools/die.rb
rpg-tools-0.2.1 app/models/tools/die.rb