dart_dice_parser 1.1.1 dart_dice_parser: ^1.1.1 copied to clipboard
A dart library for parsing dice notation (like "2d6+4", "3d6", or "3d10 + 2d6 - 5d4")
dart_dice_parser #
A library for parsing dice notation
Supported syntax #
Supported notation:
AdX
-- roll A dice of X sides, total will be returned as value- special dice variations:
AdF
-- roll A fudge dice (sides:[-1, -1, 0, 0, 1, 1]
)Ad%
-- roll A percentile dice (equivalent to1d100
)AD66
-- roll A D66, aka1d6*10 + 1d6
(NOTE: this must use uppercase D, lowercase d will be interpreted as 66-sided die)
- dropping high/low:
AdX-HN
-- roll A X-sided dice, drop N highestAdX-LN
-- roll A X-sided dice, drop N lowest
- addition/subtraction/multiplication and parenthesis are allowed
- numbers must be integers, and division is is not supported.
examples:
2d6 + 1
-- roll two six-sided dice, sum results and add one2d(2*10) + 3d100
-- roll 2 twenty-sided dice, sum results, add that to sum of 3 100-sided die1D66
-- roll a D66 -- aka two six-sided dice, multiply first by 10 and sum results1d%
-- roll one percentile dice4dF
-- roll four fudge dice ()2d20-H
-- roll 2d20, drop highest (disadvantage)2d20-L
-- roll 2d20, drop lowest (advantage)10d10-L3
-- roll 10d10, drop 10 lowest results
other dice notation info:
Usage #
A simple usage example:
import 'package:dart_dice_parser/dart_dice_parser.dart';
main() {
var diceExpression = "2d6 + 1 + 3d10";
print("$diceExpression : ${roll(diceExpression)}");
}
int roll(String diceStr) {
var result = DiceParser().evaluate(diceStr);
if (result.isFailure) {
print("Failure:");
print('\t${expression}');
print('\t${' ' * (result.position - 1)}^-- ${result.message}');
return 1;
} else {
return result.value;
}
}
Features and bugs #
Please file feature requests and bugs at the issue tracker.