Games::Dice::Advanced - simulate dice rolls, including weird and loaded dice
print Games::Dice::Advanced->roll(); # roll a six-sided die print Games::Dice::Advanced->roll('d4'); # roll a four-sided die # roll a four-sided die and a 6-sided die and return the total $die1 = Games::Dice::Advanced->new('d4'); $die2 = Games::Dice::Advanced->new('d6'); print Games::Dice::Advanced->roll($die1, $die2); print $die1->roll(); # roll the d4 we created above # roll 2 four-sided dice and a 6-sided die and return the total print Games::Dice::Advanced->roll('2d4', 'd6'); # create a four-sided die with the squares of 1, 2, 3 and 4 Games::Dice::Advanced->new(sub { int(1+rand(4)) ** 2 });
Roll one or more dice. If called as a class method, it first calls appropriate constructors and creates objects before calling those objects' roll() methods. When called on an object it simply rolls the die.
When called as a class method, it takes a list of arguments defining a 'recipe' to roll. These are added together to produce a result. Each item in the recipe must be a valid argument to the constructor (see the description of the 'new' method below). If no argument is given to a class method, we assume a six-sided die - 'd6'. You will note that the multiplier constructor is not available when roll is called in this way.
When called as an object method, no arguments are permitted.
This method defines a die. You may call it yourself to create a die for later rolling, or it may be called by the roll() method. It takes zero, one or two arguments. If no argument is given, we silently assume that the user wants to create a six-sided die, a 'd6'. Valid arguments are:
Creates a die that always returns that constant
Creates a die that returns a random integer from 1 to N with results spread evenly across the range.
Creates a die dM as above which is rolled N times to generate a result. Note that the N is *not* just a multiplier.
Note that the two arguments may be in any order. Creates a die as specified, and multiplies the results by N when it is rolled. Compare with NdM above.
Leading and trailing whitespace is stripped, no other whitespace is allowed in any of the above.
A reference to a subroutine, which is to be called whenever we need to generate a result. It should take no parameters.
Use this to easily specify truly weird dice. NOT YET IMPLEMENTED, so use a SUBREF for the moment.
For random, read 'pseudo-random'. Patches to work with sources of true randomness are welcome.
Doesn't support dice with fractional or complex numbers of sides :-)
I welcome feedback about my code, including constructive criticism. And, while this is free software (both free-as-in-beer and free-as-in-speech) I also welcome payment. In particular, your bug reports will get moved to the front of the queue if you buy me something from my wishlist, which can be found at http://www.cantrell.org.uk/david/shopping-list/wishlist.
David Cantrell <david@cantrell.org.uk>
Copyright 2003 David Cantrell
This module is free-as-in-speech software, and may be used, distributed, and modified under the same terms as Perl itself.
To install Games::Dice::Advanced, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Games::Dice::Advanced
CPAN shell
perl -MCPAN -e shell install Games::Dice::Advanced
For more information on module installation, please visit the detailed CPAN module installation guide.