dart_dice_parser 0.2.2 copy "dart_dice_parser: ^0.2.2" to clipboard
dart_dice_parser: ^0.2.2 copied to clipboard

outdated

A dart library for parsing dice notation (like "2d6+4", "3d6", or "3d10 + 2d6 - 5d4")

example/dart_dice_parser.dart

import 'dart:io';

import 'package:args/args.dart';
import 'package:dart_dice_parser/dart_dice_parser.dart';

main(List<String> arguments) {
  var argParser = new ArgParser()
    ..addOption(
      "num",
      abbr: "n",
      help: "number of times to roll the expression",
      defaultsTo: "1",
    );

  var results = argParser.parse(arguments);

  exit(roll(int.parse(results["num"]), results.rest.join(" ")));
}

int roll(int numRolls, String expression) {
  if (expression.isEmpty) {
    print("Supply a dice expression. e.g. '2d6+1'");
    return 1;
  }
  print("Evaluating: $expression\n");
  var diceParser = new DiceParser();

  print("\t\t [${diceParser.parser.parse(expression)}]\n");

  for (int i = 0; i < numRolls; i++) {
    print("${i + 1}: ${diceParser.roll(expression)}\n");
  }
  return 0;
}
2
likes
0
pub points
56%
popularity

Publisher

unverified uploader

A dart library for parsing dice notation (like "2d6+4", "3d6", or "3d10 + 2d6 - 5d4")

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

petitparser

More

Packages that depend on dart_dice_parser