Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Rate my Die class

Name: Anonymous 2012-06-30 5:37

module DMT

    # 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: Anonymous 2012-07-03 4:29

Awesome code. Great size. Looks concise. Efficient. Elegant. Keep us all posted on your continued progress with any new code factoring or compiler optimization. Show us what you got man. Wanna see how freakin' expressive, efficient, concise and elegant you can get. Thanks for the motivation.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List