A dice roll script is generally useful. Minimally (without getting golfy), it could look like this:
use strict;
use warnings;
while(1) {
print "Enter a dice to roll ('q' to quit): ";
my $input = <STDIN>;
last if $input =~ m/^q/;
my( $rolls, $die ) = $input =~ m/(\d+)d(\d+)/i;
my $result = 0;
$result += 1 + int rand $die for 1 .. $rolls;
print $result, "\n";
}
If I plan to use it as more than a throwaway script, here's how I would do it:
use 5.010;
use strict;
use warnings;
use Readonly;
use Games::Dice::Advanced;
use IO::Prompt::Hooked;
Readonly my $RE_GOOD_ROLL => qr{
^\s* # Start of line, ignore
+whitespace
(?&POS_INT)? # How many rolls (option
+al).
\s* d \s* # 'd' (forgiving of whit
+espace)
(?&POS_INT) # Die to roll.
\s*$ # End of input, ignore w
+hitespace.
(?(DEFINE) (?<POS_INT> [1-9][0-9]{0,2}? ) ) # Definition of positive
+ int.
}x;
Readonly my $DEFAULT_ROLL => '1d6';
my $roll;
while (defined(my $input = ask($roll // $DEFAULT_ROLL))) {
$roll = interpret_input($input);
my $result = Games::Dice::Advanced->roll($roll);
print "Result of ( $roll ) is [ $result ].\n\n";
}
sub interpret_input {
my $i = shift;
$i =~ s/\s+//g;
$i =~ s/^d/1d/;
return $i;
}
sub ask {
my $default = shift;
return prompt(
default => $default,
message => 'Enter a dice roll ("q" to quit): ',
validate => $RE_GOOD_ROLL,
error => "Input must be in format of d8, or 2d6.\n\n",
escape => qr/^ \s* q (?: uit )? /ix, # q or quit, case insens
+itively.
);
}
The second snippet adds input validation, default input, and allows input like "d6" as synonymous with "1d6".
This example sets a default roll of '1d6'. It then prompts the user to enter a roll, 'q' or 'quit' to quit (case insensitively), or 'enter' to accept the default roll. After the first roll, all future rolls will have their default set to the previous roll, so that a user can enter '2d20', and then just hit enter again to repeat the roll.
User input is validated, and constrained to three digit roll counts, and three digit dice. Upon receiving invalid input, a message is provided to the user explaining valid input.
While it's not difficult to build a dice roll generator, I used Games::Dice::Advanced because it is already more fully developed, has its own reasonable error handling, and is a good fit for our use. IO::Prompt::Hooked handles the prompting, and facilitates validation, defaults, bad input message, and input termination. We also define a regex snippet that matches basic non-zero positive integers of up to three digits, for our roll count and die type.
Here's a sample run:
$ ./diceroll.pl
Enter a dice roll ("q" to quit): [1d6]
Result of ( 1d6 ) is [ 3 ].
Enter a dice roll ("q" to quit): [1d6] 2d4
Result of ( 2d4 ) is [ 3 ].
Enter a dice roll ("q" to quit): [2d4] 3d100
Result of ( 3d100 ) is [ 122 ].
Enter a dice roll ("q" to quit): [3d100]
Result of ( 3d100 ) is [ 153 ].
Enter a dice roll ("q" to quit): [3d100] 34c
Input must be in format of d8, or 2d6.
Enter a dice roll ("q" to quit): [3d100] d6
Result of ( 1d6 ) is [ 2 ].
Enter a dice roll ("q" to quit): [1d6] q
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.