-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroll.pl
More file actions
executable file
·34 lines (31 loc) · 840 Bytes
/
roll.pl
File metadata and controls
executable file
·34 lines (31 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/perl -w
use strict;
my $lastroll = -1;
for (split(',', join('',@ARGV))) {
if (/^([0-9]+)\s*d\s*([0-9]+)\s*([-+\/*])?\s*([0-9]+)?\s*$/) {
my $mod = defined $3 && defined $4 ? "$3$4" : undef;
printf "%9s: ", sprintf "%sd%s%s", $1, $2, defined $mod?$mod:'';
$lastroll = &roll($1, $2, $mod);
} else {
warn "I don't understand: $_\n";
}
}
exit $lastroll;
sub roll {
my ($die, $sides, $mod) = @_;
my $total=0;
my @rolls;
if (defined $die && defined $sides) {
while ($die--) {
push @rolls, int(rand($sides))+1;
$total += $rolls[-1];
}
print "(", join('+',@rolls), ")";
if (defined $mod) {
print "$mod";
$total = eval "$total$mod";
}
}
print " = $total\n";
return $total;
}