/// <summary> /// Parses the dice string and returns a rollable cup for the string representation. /// Dice string can be a basic dice roll representation for example "2d8+4" or "3d6-1" /// </summary> /// <returns>A rollable cup for this dice string</returns> /// <param name="diceString">Dice string to parse</param> public static Cup ParseDice(string diceString) { var cup = new Cup(); var regEx = new Regex("^(?<dieCount>\\d+)?d(?<dieSides>\\d+)(?<modifier>\\+\\d+)?"); var match = regEx.Match(diceString); var dieCount = DefaultOrNumber(match.Groups["dieCount"].Value, 1); var dieSides = DefaultOrNumber(match.Groups["dieSides"].Value, 1); var modifier = DefaultOrNumber(match.Groups["modifier"].Value, 0); var dice = Die.GetDice((DiceSides)dieSides, dieCount); cup.AddDice(dice); cup.Modifier = modifier; return(cup); }
public static Cup ParseDice(this Cup cup, string diceString) { if(string.IsNullOrEmpty(diceString)) return cup; var regEx = new Regex("^(?<dieCount>\\d+)?d(?<dieSides>\\d+)(?<modifier>\\+\\d+)?"); var match = regEx.Match(diceString); var dieCount = DefaultOrNumber(match.Groups["dieCount"].Value, 1); var dieSides = DefaultOrNumber(match.Groups["dieSides"].Value, 1); var modifier = DefaultOrNumber(match.Groups["modifier"].Value, 0); var dice = Die.GetDice((DiceSides)dieSides, dieCount); cup.AddDice(dice); cup.Modifier = modifier; return cup; }