Skip to content

Instantly share code, notes, and snippets.

@bb010g
Created April 29, 2017 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bb010g/3255a3243eb8f291e22006346787e60c to your computer and use it in GitHub Desktop.
Save bb010g/3255a3243eb8f291e22006346787e60c to your computer and use it in GitHub Desktop.
Perl 6 dice roller
use Crypt::Random;
class Roll {
has Int:D $.dice = 1;
has Int:D $.sides is required;
has Int:D $.constant = 0;
method parse(Str:D $str) {
if $str ~~ /[$<d>=(\d+) d]? $<s>=(\d+) [\+ $<c>=(\d+)]?/ {
self.new(dice => ($<d>:v || 1).Int, sides => ($<s>:v).Int, constant => ($<c>:v).Int);
}
}
}
sub USAGE() { say "Usage: {$*PROGRAM-NAME} [<dice>d]<sides>[+<constant>]" }
sub MAIN(Str $roll-str) {
my $roll = Roll.parse($roll-str) // return USAGE();
say $roll.constant + [+] (1 .. $roll.dice)».&{ crypt_random_uniform($roll.sides) + 1 };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment