Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

Dave


In reply to Re: Using user input to return variable typed. by davido
in thread Using user input to return variable typed. by myelinviolin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (7)
    As of 2014-12-25 07:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (159 votes), past polls