r/ProgrammingPrompts • • Mar 09 '14
Virtual Dungeons and Dragons Dice
I challenge y'all to make a set of virtual DnD dice of which you can decided how many sides the dice have. This one should be really simple though there's plenty of room for branching off and being as thorough as you'd like (ASCII art title, keep track of each player's rolls and keep track of them by name etc.).
14
Upvotes
2
u/ryalla Mar 09 '14
When doing this, make sure each number is equally likely. Specifically in C,
rand() % nfor ann-sided die does not satisfy this for anynthat isn't a power of two1
u/[deleted] Mar 10 '14
Here is a simple version in C++.
1
u/[deleted] Mar 10 '14
Here's a complicated version in C++:
http://pastebin.com/W33BGfiS
It is called from the command line, and will recursively parse the input. You can ask for as many dice rolls as you like, separated by semi-colons.
You can also chain silly things together. For example: d2d3d4 will:
The order it parses things in is:
If you want to have a closer look at exactly what's happening, pass -debug as an argument.
Also, let me know about any bugs you find (it should be fully portable between all C++11 compliant platforms, but I only tested in VS2012) and also about the silliest dice rolls you tried.
For a demo, try the command line: "d6 ; 3d6 ; 2d4 + 4 ; (3 * (2+4d6k3)) ; 1d2d3d4s2r3 ; (2d3)d8k(1d4) ; ((((((((2)))))))) ; -debug" to see what it does.