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-06-30 5:39

Side note: DMT is a larger module called "Dungeon Master Toolkit", this die is supposed to make things like generating characters a hell of a lot easier. Things like generating stats is as simple as:

6.times { puts DMT::Die["4d6 drop 1"].roll }

Name: Anonymous 2012-06-30 6:17

What language is this shit ?

Name: Anonymous 2012-06-30 6:46

>>3

Ruby

Name: Anonymous 2012-06-30 6:56

Looks like Ruby. Ruby is garbage, it's an extreme heteroiconic language, the precise opposite of Lisp in terms of complexity. It's worse than C++.

>>1
You're abusing Object-Oriented Design principles. OOP is fucking shit, and you're just rotting your brain and wasting your life.

Watch these and save yourself:

http://vimeo.com/43380467
http://www.infoq.com/presentations/Simple-Made-Easy

Name: Anonymous 2012-06-30 7:09

self.[]

What the fuck? Use new.

Your rating: 5/10. This kinda sucks.

Name: Anonymous 2012-06-30 7:15

>>5

If Ruby is the opposite of lisp in terms of complexity, then lisp must be more complex than assembly. Ruby's fucking simple.

Name: Anonymous 2012-06-30 7:26

>>6

self.[] formats the string and translates it to new. Just calling new looks like crap and may end up in having to throw in unused arguments.

Like for example, 4d6 drop 1 would be new 4, 6, 0, 1. It says nothing to the end user what each of the values are. There's a reason smalltalk and obj-c used named arguments like

dieWithNumnber: 4, value: 6, bonus: 0, drop: 1

Name: Anonymous 2012-06-30 7:34

>>7
You are confused. It is not simple. The language is complex. Ruby might be easy for novice programmers working on small programs, but that doesn't mean it is simple. As the size of your Ruby programs grow, the complexity will overwhelm you and will not be easy to continue growing it.

Name: Anonymous 2012-06-30 7:42

>>9
Actually >>7 is half-right, assembly is simpler than Lisp. But Ruby is extremely complex.

Name: Anonymous 2012-06-30 7:51

Ruby the hetero,
on Snails he goes.

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2012-06-30 7:52

>>4
I thought it was Python. Another one of those scripting languages that seem so simple to start with, but then >>9 happens.

I've never used either in any volume and wouldn't want to. Significant whitespace and lack of visible statement terminators is a huge turn-off.

Name: Anonymous 2012-06-30 7:59

>>10
I never said assembly language isn't simple, I just focused on what was wrong with his argument.

Name: Anonymous 2012-06-30 8:27

>>12
Cudder, are you Jewish?

Name: Anonymous 2012-06-30 8:29

>>5
It's worse than C++.
and slower than than BASH scripts.

Name: Anonymous 2012-06-30 9:25

>>5
Please install Flash player.

Name: Anonymous 2012-06-30 9:29

>>16
Use Gnash!!

Name: Anonymous 2012-06-30 9:30

>>17
I don't like executing foreign code on my computer, especially not flash bytecode.

Name: Anonymous 2012-06-30 10:07

DMT is released in massive amounts into your brain when you are about to Die.  It's a scientific fact, look it up.

Name: Anonymous 2012-06-30 11:03

(defun d (x &optional (n 1))
  (loop repeat n collecting
    (1+ (random x))))

(defun drop (n rolls)
  (last (sort rolls #'<)
    (- (length rolls) n)))

(defun bonus (b rolls)
  (cons b rolls))

(defun roll (rolls)
  (apply #'+ rolls))

; example:
(roll (bonus 3 (drop 2 (d 6 2))))

Name: Anonymous 2012-06-30 15:07

>>20
period

Name: Anonymous 2012-06-30 15:18

dubs

Name: Anonymous 2012-06-30 20:06

print(5 ? 4D6-2 DROP 1);
MODE DIE = STRUCT(INT count, sides, dropped, multiplier, additive);
PRIO D = 6, DROP = 6, KEEP = 6, ? = 5;
OP D = (INT count, sides)DIE: (count, sides, 0, 1, 0);
OP D = (INT sides)DIE: (1, sides, 0, 1, 0);
OP DROP = (DIE d, INT n)DIE: (count OF d, sides OF d, n, multiplier OF d, additive OF d);
OP KEEP = (DIE d, INT n)DIE: (count OF d, sides OF d, count OF d - n, multiplier OF d, additive OF d);
OP + = (DIE d, INT n)DIE: (count OF d, sides OF d, dropped OF d, multiplier OF d, additive OF d + n);
OP - = (DIE d, INT n)DIE: (count OF d, sides OF d, dropped OF d, multiplier OF d, additive OF d - n);
OP * = (DIE d, INT n)DIE: (count OF d, sides OF d, dropped OF d, multiplier OF d * n, additive OF d);
OP ? = (INT lim)INT: ENTIER(random * lim) + 1;
OP ? = (DIE d)INT: roll(d);
OP ? = (INT n, DIE d)[]INT: ([n]INT r; FOR i TO n DO r[i] := roll(d) OD; r);
PROC roll = (DIE d)INT:
    IF count OF d <= 0 OR dropped OF d >= count OF d THEN
        0
    ELIF dropped OF d > 0 THEN
        [sides OF d]INT r, INT min := 1, n := 0;
        FOR i TO UPB r DO r[i] := 0 OD;
        TO count OF d DO r[?sides OF d] +:= 1 OD;
        TO dropped OF d DO
            WHILE r[min] = 0 DO min +:= 1 OD;
            r[min] -:= 1
        OD;
        FOR i TO UPB r DO n +:= i * r[i] OD;
        n
    ELSE
        INT n := 0;
        TO count OF d DO n +:= ?sides OF d OD;
        n
    FI * multiplier OF d + additive OF d;
~

Name: Anonymous 2012-06-30 20:13

OH GOD!!!

COMMUNICATIONS TERMINATED

Name: Anonymous 2012-06-30 23:44

>>23

What the fuck am I reading?

Name: FrozenVoid !!mJCwdV5J0Xy2A21 2012-07-01 0:11

>>25
Algol source

Name: Anonymous 2012-07-01 1:11

>>26

This is the 2000s, why are we writing in Algol again?

Name: Anonymous 2012-07-01 1:49

>>27
Using C
2012
ISHYGDDT

Name: Anonymous 2012-07-01 3:07

>>28

I use C++, not C.

Name: Anonymous 2012-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

Name: Anonymous 2012-07-01 9:20

>>30`
>implying get the fuck back to /g/

Name: Anonymous 2012-07-01 12:03

>>30
>lack of OOP
>a [...] limit
Seriously. OOP is shit.

Name: Anonymous 2012-07-01 14:33


#include <vector>
#include <algorithm>
#include <cstdlib>

namespace dmt
{
    int die(int n, int v, int bonus = 0, int drop = 0)
    {
        int damage = bonus;
        std::vector<int> d(n);
        std::for_each(d.begin(), d.end(), [v](int& p) { p = rand() % v + 1; });
        std::sort(d.begin(), d.end(), std::greater<int>());
        std::for_each(d.begin(), d.end() - drop, [&damage](int p) { damage += p; });
        return damage;
    }
}

Name: Anonymous 2012-07-01 14:41

>>5
OOP is only shit when retards like the OP apply it to every-fucking-thing possible.

Name: Anonymous 2012-07-01 18:10

>>33

That's nice, but there's a fucking reason I made my die a class and not a function. It's designed to be used multiple times as a value

Longsword = DMT::Weapon.new "Longsword", DMT::Die["1d8"] # Other arguments follow
Longsword.damage # Calls @wpower.roll every time


You can't pass a function with the arguments already defined, as an argument to a constructor. You can however, pass an object. The reasoning for self.[] is to make the arguments more readable. Passing the arguments as a string allows for use of the constructor without having to look back at the original source.

Name: Anonymous 2012-07-01 18:25

>>35
How about:

Longsword = DMT::Weapon.new "Longsword", "1d8"

And defining DMT::Weapon#damage to call a  die function with that argument?

Name: Anonymous 2012-07-01 18:26

Stringly typed Ruby sucks.

Name: Anonymous 2012-07-01 18:34

>>37
It's perfectly acceptable here. Think of it as a mini-DSL for dice rolls.

Name: Anonymous 2012-07-01 18:55

>>35
You can't pass a function with the arguments already defined, as an argument to a constructor
This seems like an odd thing to leave out of a language. Partial application of function arguments is not something one can live without in a reasonably high level language.
Hell, with a good CPU architecture, you might argue that ASM can support partially applied functions

Name: Anonymous 2012-07-01 20:14

>>35
You can't pass a function with the arguments already defined
Sure you can



#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cstdlib>
#include <ctime>

namespace dmt
{
    int die(int n, int v, int bonus = 0, int drop = 0)
    {
        int damage = bonus;
        std::vector<int> d(n);
        std::for_each(d.begin(), d.end(), [v](int& p) { p = rand() % v + 1; });
        std::sort(d.begin(), d.end(), std::greater<int>());
        std::for_each(d.begin(), d.end() - drop, [&damage](int p) { damage += p; });
        return damage;
    }
   
    class weapon
    {
    public:
        weapon(const std::string& name_, std::function<int()> damage_lambda_)
            : name(name_), damage_lambda(damage_lambda_)
        {
        }

        int damage()
        {
            return damage_lambda();
        }

    private:
        std::string name;
        std::function<int()> damage_lambda;
    };
}



int main()
{
    srand(time(NULL));

    dmt::weapon longsword("longsword", []() -> int { return dmt::die(1, 8); });
    printf("%d", longsword.damage());

    return 0;
}

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