5
0
Background
When playing an RPG such as Dungeons & Dragons, Pathfinder, or Paranoia, players each need a set of dice. You are hosting one such game this weekend, and some of the friends you invited have never played an RPG before. They don't want to commit to buying their own set of dice until they know they'll use them (if they want to keep playing). This is fine and dandy with you, but you're not going to let them use your lucky set of dice that you pre-rolled the ones out of...
Goal
...so you are going to write a program or function that simulates the dice-rolling for them.
Dice are often described in the format: <How many dice>d<Number of sides>
A common set of dice for RPG use includes: 1d4
, 1d6
, 1d8
, 2d10
(also used as 1d%
or 1d100
), 1d12
, and 1d20
.
Your code must accept a string of any number of the above dice specifiers separated by spaces or commas as input. It must then output the result of each individual die-roll separated by spaces, with parentheses around groups; then the sum of each grouping on the next line, separated by spaces; and finally, the total sum on a third line. Single-die groups don't have parentheses. Dice must always be in the same order as provided in the input.
The percent die, whether written as 1d%
or 1d100
, should be calculated as the concatenation of 2d10
(just 2 rolls, each from 0-9, but a final result of zero is instead counted as 100), but each occurrence will be displayed as if a single roll. 2d10
in the input will still be treated and displayed as 2 individual rolls. It can be easy to get confused by percent dice, here's a good explanation.
Edit: To clarify, the number of rolls may exceed 9.
Examples
Input:
1d4 3d6 2d8
Possible Output:
3 (1 5 2) (8 4)
3 8 12
23
Input:
1d20 1d% 2d10 1d100 5d12
Possible Output:
16 100 (9 4) 42 (5 1 7 7 11)
16 100 13 42 31
204
Rules
- Standard loopholes are disallowed.
- Printable ASCII in code, only.
- code-golf - shortest code wins.
- -10 bytes if the rolls are actually random, rather than pseudo-random.
2So, wait. Am I right that the
d10
s are 0-9 but the others are 1-n? Or do they also go 0 to (n-1)? Clearly thed100
s are supposed to have values 1-100. – DLosc – 2015-09-03T22:04:15.2671Very closely related. – Martin Ender – 2015-09-03T22:30:33.930
@DLosc I was only specifying how to calculate a d%.
1d10
is still in the range 1-10. – mbomb007 – 2015-09-03T22:59:31.910@mbomb007 I didn't say duplicate. ;) – Martin Ender – 2015-09-03T23:01:05.527
For the percentage die, couldn't a program just generate a single (pseudo-)random number between 1 and 100? If so, what's the reason for calculating it as the concatenation of
2d10
and special-casing 00? – DLosc – 2015-09-03T23:05:50.6031I imagine because that's how it's handed in DnD. – Celeo – 2015-09-03T23:11:46.013
Yes, but this is code golf. If generating a number in the right range is equivalent and takes fewer bytes, can my program just do that? – DLosc – 2015-09-03T23:44:04.130
@DLosc I guess so. Makes the most sense. – mbomb007 – 2015-09-04T01:13:16.810
Will the number of rolls always be single-digit? – Trebuchette – 2015-09-04T03:41:28.557
@Trebuchette No. I didn't show it in my examples, but the number of rolls can be more than a single digit. – mbomb007 – 2015-09-04T13:35:43.777