Saturday, June 19, 2010

Let's Roll Some Dice

I reimplemented my very simple dice rolling module. It is now a pair of functions instead of a struct, and it supports strings as input.

Couple examples:
auto roll = rollDice("1d4"); // results in a value between 1 and 4
auto roll2 = rollDice("2d6+2", 4); // results in a value between 8 and 18
auto roll3 = rollDice(1, 8, -2); // results in a value between -1 and 6
The parser doesn't support strings like "1d4 + 2d6". I have no plans to add that kind of support any way as it can be combined from smaller pieces.

Source code: here