Twitter: raymondcamden

Address: Lafayette, LA, USA

Friday Puzzler: Rolling the die...

02-21-2014 2,485 views Development 16 Comments

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.


These comments will soon be imported into Disqus. To add a comment, use Disqus above.
  • Commented on 02-21-2014 at 10:10 AM
    Responses are logged to console so open it to see results...
  • Commented on 02-21-2014 at 10:13 AM
    Nice. To be picky, I don't like the outputs in the rollDice func. It should be returning results to the caller instead.
  • Commented on 02-21-2014 at 10:13 AM
    Yeah, not exactly "production ready" code.
  • Commented on 02-21-2014 at 10:14 AM
    Btw - no offense made. In general, I like to nit pick the submissions. Totally arbitrary of course. ;)
  • Commented on 02-21-2014 at 10:15 AM
    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.
  • Commented on 02-21-2014 at 10:28 AM
    Super quick and dirty
  • Commented on 02-21-2014 at 10:34 AM
    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.
  • Commented on 02-21-2014 at 10:35 AM
    Oh - I see you called it Node Roller - so the assumption was back end perhaps?
  • Commented on 02-21-2014 at 10:38 AM
    Yeah, quick and dirty so I just went with the back-end.

    I'm in the same boat as you when it comes to mostly but akward
  • Michael Schmidt #
    Commented on 02-21-2014 at 11:59 AM
    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
  • Michael Schmidt #
    Commented on 02-21-2014 at 11:59 AM
    I forgot the link
  • Commented on 02-21-2014 at 1:42 PM
    Quick and dirty in Groovy:

    I wasn't sure if a roll result could be negative (e.g. 1d6-20), so I just let it happen.
  • Commented on 02-21-2014 at 1:58 PM
    Fascinating. I learned Groovy years ago, but haven't used it - I forget how nice it is.
  • Commented on 02-21-2014 at 2:17 PM
    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.
  • Jared #
    Commented on 02-21-2014 at 4:13 PM
    Some regular expression fun with JavaScript. I bet there are some edge cases it misses, but it works pretty well.
  • AXL #
    Commented on 02-22-2014 at 7:52 AM

    Can't wait for the warm weather ^^;