This post is more than 2 years old.
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.