Friday Puzzler: Rolling the die...

It has been a while since my last Friday puzzle, but I ran across something yesterday that I thought would be an excellent little coding challenge. Even better, this one I guarantee shouldn't take you more than 5 minutes. Honest!

Your challenge this week is to implement a common RPG mechanism involving die rolls. For those of you who aren't gaming nerds, a typical part of these games is to roll a die, or multiple dice, to see if you succeed at some particular task. Because this is used so often, a shorthand way of representing what you should roll is used. So for example, to roll a typical six sided die twice, you would see: 2d6. To roll one twelve sided die once, it would be 1d12. Many times a bonus, or penalty, is applied to roll. So for example, you have a sword that does 2d6 damage, but because it has the Blessing of Awesome-itude, you get to add two to the roll. That would be designated as 2d6+2. On the flip side, you have a wand that does 3d12 damage (pretty impressive), but the creator was drunk when she created it so you get a penalty of minus four: 3d12-4.

Your task is simple. Write a program that accepts the input, parses it, and returns the result. It should gracefully handle errors in the input. For brownie points, return the total as well as what each individual die roll was. You can solve this in any language, but please remember to post a Pastebin or Gist link for server-side code. For client-side code use JSBin (or something similar).

And hey - I'll give you a prize too. How about a 30 gig USB card from Adobe? It is the size of a business card so I can slip it in the mail - but only for folks in America. Because - um - I'm cheap.

Archived Comments

Comment 1 by Sean Coyne posted on 2/21/2014 at 9:10 PM

Responses are logged to console so open it to see results...

http://jsfiddle.net/seancoy...

Comment 2 by Raymond Camden posted on 2/21/2014 at 9:13 PM

Nice. To be picky, I don't like the outputs in the rollDice func. It should be returning results to the caller instead.

Comment 3 by Sean Coyne posted on 2/21/2014 at 9:13 PM

Yeah, not exactly "production ready" code.

Comment 4 by Raymond Camden posted on 2/21/2014 at 9:14 PM

Btw - no offense made. In general, I like to nit pick the submissions. Totally arbitrary of course. ;)

Comment 5 by Sean Coyne posted on 2/21/2014 at 9:15 PM

None taken at all. Was fun to write. Hadn't generated a random number, as simple as it is, in JS before. Just never had a need I guess.

Comment 6 by Danny Scott posted on 2/21/2014 at 9:28 PM

Super quick and dirty
https://gist.github.com/dan...

Comment 7 by Raymond Camden posted on 2/21/2014 at 9:34 PM

Nice - any reason you didn't use JSBin? (I ask because I keep going back and forth on whether I like them. I find them awkward at times.

Comment 8 by Raymond Camden posted on 2/21/2014 at 9:35 PM

Oh - I see you called it Node Roller - so the assumption was back end perhaps?

Comment 9 by Danny Scott posted on 2/21/2014 at 9:38 PM

Yeah, quick and dirty so I just went with the back-end.

I'm in the same boat as you when it comes to JSBin...cool mostly but akward

Comment 10 by Michael Schmidt posted on 2/21/2014 at 10:59 PM

Here is my attempt it is very ugly and doesn't handle a case of the minimum number of sides a dice can have i assumed that number of sides and number of dice had to be more then zero

Comment 11 by Michael Schmidt posted on 2/21/2014 at 10:59 PM
Comment 12 by Sean Walsh posted on 2/22/2014 at 12:42 AM

Quick and dirty in Groovy: https://gist.github.com/s99...

I wasn't sure if a roll result could be negative (e.g. 1d6-20), so I just let it happen.

Comment 13 by Raymond Camden posted on 2/22/2014 at 12:58 AM

Fascinating. I learned Groovy years ago, but haven't used it - I forget how nice it is.

Comment 14 by Sean Walsh posted on 2/22/2014 at 1:17 AM

I'm still very much in the early stages of learning Groovy (read: that code could probably be way better), but it's been a blast so far! Really fun language to code in.

Comment 15 by Jared posted on 2/22/2014 at 3:13 AM

Some regular expression fun with JavaScript. I bet there are some edge cases it misses, but it works pretty well.

http://jsbin.com/bogolewa/5...

Comment 16 by AXL posted on 2/22/2014 at 6:52 PM

http://pastebin.com/Kmwb3dMZ

Can't wait for the warm weather ^^;