roll method

int roll(
  1. String roll
)

Compute an arithmetic expression from a roll defined on standard notation format.

Example:

  d20.roll('2d6+3');
  d20.roll('2d20-L');
  d20.roll('d% * 8');
  d20.roll('cos(2 * 5d20)');

Implementation

int roll(String roll) {
  final newRoll = _sanitizeStringNotation(roll).replaceAllMapped(
    _singleDiceRegExp,
    (Match m) => _rollSingleDie(m).finalResult.toString(),
  );

  final exactResult = _parser
      .parse(newRoll)
      // ignore: avoid_as
      .evaluate(EvaluationType.REAL, _context) as double;

  return exactResult.round();
}