class RpgTools::Dice

Attributes

base[RW]
bonus[RW]
malus[RW]
rolls[RW]
sides[RW]
type[RW]
value[RW]

Public Class Methods

new(base) click to toggle source
# File lib/rpg_tools/dice.rb, line 5
def initialize(base)
  @base  = base.upcase
  @type  = fudge_dice? ? 'Fudge' : 'Standard'
  @value = nil
  @sides = fudge_dice? ? 3 : base.gsub(/^./, '').to_i
  @rolls = 0

  dice_validity_check
  set_modifiers_and_clean_base unless fudge_dice?
end

Public Instance Methods

roll() click to toggle source
# File lib/rpg_tools/dice.rb, line 16
def roll
  @rolls += 1

  fudge_dice? ? fudge_roll : standard_roll
end
Also aliased as: roll!
roll!()
Alias for: roll

Private Instance Methods

base_check() click to toggle source
# File lib/rpg_tools/dice.rb, line 36
def base_check
  unless standard_dice? || fudge_dice?
    raise ArgumentError.new("You can only create strandard and fudge dices.")
  end
end
dice_validity_check() click to toggle source
# File lib/rpg_tools/dice.rb, line 25
def dice_validity_check
  sides_check
  base_check
end
fudge_dice?() click to toggle source
# File lib/rpg_tools/dice.rb, line 64
def fudge_dice?
  @base == 'DF'
end
fudge_roll() click to toggle source
# File lib/rpg_tools/dice.rb, line 56
def fudge_roll
  @value = (rand(1..3) - 2)
end
modifier_check() click to toggle source
# File lib/rpg_tools/dice.rb, line 30
def modifier_check
  if (@base =~ /^[D]\d+[+-]?\d*/).nil?
    raise ArgumentError.new("You can only use + or - as modifiers.")
  end
end
set_modifiers_and_clean_base() click to toggle source
# File lib/rpg_tools/dice.rb, line 48
def set_modifiers_and_clean_base
  modifier_check

  @bonus = @base.gsub(/[D]\d+\+/, '').to_i
  @malus = @base.gsub(/[D]\d+\-/, '').to_i
  @base.gsub!(/[+-]\d+/ , '')
end
sides_check() click to toggle source
# File lib/rpg_tools/dice.rb, line 42
def sides_check
  if [0, 1].include?(@sides)
    raise ArgumentError.new("You can't create dices with less than 2 sides.")
  end
end
standard_dice?() click to toggle source
# File lib/rpg_tools/dice.rb, line 68
def standard_dice?
  @base.gsub(/^D\d*[+-]?[\d]+$/, '').empty?
end
standard_roll() click to toggle source
# File lib/rpg_tools/dice.rb, line 60
def standard_roll
  @value = rand(1..@sides) + @bonus - @malus
end