# Custom die class to take into account
# Multiple dice, bonuses added to dice and dropped dice
class Die
# Standard initializer
def self.[] str
raise TypeError, "Argument must be a String" unless str.is_a? String
raise ArgumentError, "Error: Malformed Argument\
\nUsage:\
\n\t<Number>d<Die>\
\n\t<Number>d<Die><+ or -><Bonus>\
\n\t<Number>d<Die><+ or -><Bonus> drop <NumDropped>\
\nExamples:\
\n\tDMT::Die[\"3d6\"]\
\n\tDMT::Die[\"3d6+4\"], DMT::Die[\"3d6-2\"]\
\n\tDMT::Die[\"4d6 drop 1\"], DMT::Die[\"4d6-2 drop 1\"]\
\n" unless str.match /\A(\d+d\d+\ ?([\+\-]\ ?\d+)?(\ drop\ \d+)?)\z/
args = (str.split /\D/).reject{|s| s.empty?}.map{|i| i.to_i}
if str.include? "drop" then
if str.include? "-" then
new args[0], args[1], -args[2], args[3]
else
(args.size.eql? 3) ? (new args[0], args[1], 0, args[2]) : (new *args)
end
else
if str.include? "-" then
new args[0], args[1], -args[2]
else
new *args
end
end
end
# Die roller, produces a random number
def roll
damage = @bonus
dies = []
@number.times { dies.push (rand @value) + 1 }
(dies.sort.last dies.size - @drop).each { |d| damage += d }
damage
end
def inspect
"<Die: #{@number}d#{@value}" +
(@bonus.zero?? "" : @bonus < 0 ? @bonus.to_s : "+#{@bonus}") +
(@drop.zero?? "" : " Drop #{@drop}") + ">"
end
protected
def initialize num, val, bonus = 0, drop = 0
@number = num
@value = val
@bonus = bonus
@drop = drop
end
end
end
Name:
Anonymous2012-07-01 6:02
>>28
>implying C isn't a great language that's still evolving
>implying the lack of OOP is anything but a trivial limit to its application