20
7
I want to play Dungeons and Dragons, but I don't have any dice! Your challenge is to roll some D&D dice.
The input format specification in Backus-Naur form is:
<valid-input> ::= <opt-integer> "d" <integer> <opt-modifier>
<opt-integer> ::= | <integer>
<opt-modifier> ::= | "+" <integer>
<integer> ::= "0" | "1" | "2" | "3" | "4" | "5" |
"6" | "7" | "8" | "9" | <integer> <integer>
The optional integer before the d
is the number of dice to roll; it must be at least 1
, and defaults to 1
if not supplied.
The required integer immediately after the d
is the number of sides each die has; it must be at least 1
. The sides of each die are distinct consecutive positive integers starting at 1
.
The optional modifier may be +0
, and it defaults to +0
if not specified.
For example, for the input 2d10+5
, you generate two random numbers from 1 to 10 inclusive, add them together, and add 5. Then you will output the result.
If you receive invalid input, such as 2d
, d20+
, 0d4
, 2d5+1+2
, 2+2
, or anything else that doesn't fit this format, you must output "Invalid input
". Otherwise, you must output only a single random integer, weighted according to the input. For example, 3d6
should produce more 10
s than 4
s.
Test cases
Input Minimum possible output Maximum possible output
d1 1 1
d6 1 6
d1+3 4 4
d20+3 4 23
2d1 2 2
2d6+2 4 14
d01 1 1
d01+0 1 1
01d01+01 2 2
3d20+10 13 70
d Invalid input
d0 Invalid input
d+0 Invalid input
d0+0 Invalid input
0d1 Invalid input
0d1+1 Invalid input
d1+ Invalid input
1d Invalid input
1d1+ Invalid input
1d+1 Invalid input
2d+2d Invalid input
d2+d2 Invalid input
d2+2+2 Invalid input
d2-1 Invalid input
-d2 Invalid input
-2d2 Invalid input
4*3 Invalid input
4*d2 Invalid input
This is code-golf, so the shortest code in bytes will win!
Is
2d10-5
a valid input? -- Oops, I missed the part where you said p is 0 or more. Nevermind. – undergroundmonorail – 2014-04-07T02:37:21.3271Is
02d05+073
a valid input? – MT0 – 2014-04-07T07:23:27.437Can you throw a 13 sided die? Or a 17 sided die? – Averroes – 2014-04-07T08:29:35.613
2The hard part about this question is validating the input, but the paragraph which describes the validation rules is self-contradictory. It describes
n
andp
as optional, but input which choose not to include them (d20+
) as invalid. – Peter Taylor – 2014-04-07T09:13:50.6801@PeterTaylor: I think the
+
sign should only be added if the modifierp
is provided. – ProgramFOX – 2014-04-07T09:39:00.0371@MT0 Yes;
02
is a number >= 1. – Doorknob – 2014-04-07T12:11:31.467@Averroes Of course, why not? – Doorknob – 2014-04-07T12:11:59.873
@PeterTaylor Edited post to clarify ("...and preceded by a
+
sign.") – Doorknob – 2014-04-07T12:12:58.9474@Doorknob, Well, because d13 and d17 aren't dice used in D&D. D&D uses d4, d6, d8, d10, d12, and d20. Also, there are certainly cases where a roll would include different types of dice (eg,
1d4+1d6
for a Rogue sneak attacking with a dagger) or having a negativep
(eg,1d20-1
for a skill check with no ranks/training and a negative ability modifier). – Brian S – 2014-04-07T15:48:46.337@BrianS well you also sometimes see the odd
d3
which is done using a normal die and dividing by 2 (rounding up). I'm not entirely sure but I may have seend2
as well which is just flipping a coin (or using a similar method as above). But yesd13
andd17
are a bit unusual. – Martin Ender – 2014-04-07T16:23:15.120@m.buettner, I think 3.5/PF might use d2/d3 for whips, but the standard is to simulate those with d4/d6 (particularly when considering a "standard set" available at hobby shops). 4e doesn't have dice below d4, although it does have things like Weakened condition/half damage. – Brian S – 2014-04-07T16:28:09.103
2You're going to play dnd without the usecase of
2d8 + 1d6 + 4
? You're gonna have a bad time – corsiKa – 2014-04-07T20:19:02.7431
2d6+2 4 12
is wrong; max should be 14. – Cees Timmerman – 2014-04-08T09:56:24.423@CeesTimmerman, concur. Fixed. – Peter Taylor – 2014-04-08T10:53:54.880