Created
April 29, 2017 06:19
-
-
Save bb010g/3255a3243eb8f291e22006346787e60c to your computer and use it in GitHub Desktop.
Perl 6 dice roller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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