Exemple #1
0
        private List <Die> ParseDie(string notation)
        {
            // Parse the notation and find valid dice and any attributes
            List <Die> parsed  = new List <Die>();
            string     pattern = notationPatterns[PatternType.Notation];

            MatchCollection notationMatches;

            notationMatches = Regex.Matches(notation, pattern);

            for (int i = 0; i < notationMatches.Count; ++i)
            {
                Match notationMatch = notationMatches[i];
                // Number of times to roll the die
                int quantity = 1;
                if (notationMatch.Groups[2].Length > 0 && int.TryParse(notationMatch.Groups[2].Value, out int temp))
                {
                    quantity = temp;
                }

                // How many sides the die has
                int    sides       = 0;
                string sidesString = "";
                if (int.TryParse(notationMatch.Groups[3].Value, out temp))
                {
                    sides = temp;
                }
                else
                {
                    sidesString = notationMatch.Groups[3].Value;
                }

                // If the number of sides was not a number it must been a fudge die (or a percentage, but we'll check that later)
                bool   fudge       = false;
                string fudgeString = "";
                if (sidesString.Length > 0)
                {
                    fudge = Regex.IsMatch(sidesString, notationPatterns[PatternType.Fudge]);
                    if (fudge)
                    {
                        fudgeString = Regex.Match(sidesString, notationPatterns[PatternType.Fudge]).Groups[0].Value;
                    }
                }

                // Whether to explode the dice rolls
                bool explode = notationMatch.Groups[5].Length > 0;

                // Whether to penetrate the dice rolls
                bool penetrate = new string[] { "!p", "!!p" }.Contains(notationMatch.Groups[5].Value);

                // Whether to compound exploding dice rolls
                bool compound = new string[] { "!!", "!!p" }.Contains(notationMatch.Groups[5].Value);

                // The compare point for exploding/penetrating dice
                ComparePoint comparePoint = new ComparePoint();
                if (notationMatch.Groups[6].Length > 0)
                {
                    comparePoint.Operator = notationMatch.Groups[6].Value;
                    if (!int.TryParse(notationMatch.Groups[7].Value, out comparePoint.Value))
                    {
                        comparePoint.Value = 0;
                    }
                }
                else if (explode)
                {
                    comparePoint.Operator = "=";
                    comparePoint.Value    = fudge ? 1 : ((sidesString == "%") ? 100 : sides);
                }
                parsed.Add(new Die()
                {
                    Quantity     = quantity,
                    Sides        = sides,
                    SidesString  = sidesString,
                    Fudge        = fudge,
                    FudgeString  = fudgeString,
                    Explode      = explode,
                    Penetrate    = penetrate,
                    Compound     = compound,
                    ComparePoint = comparePoint
                });
            }

            return(parsed);
        }
Exemple #2
0
 /// <summary>
 /// Check whether value matches the given compare point
 /// </summary>
 /// <param name="comparePoint"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 private bool IsComparePoint(ComparePoint comparePoint, int value)
 {
     return(comparePoint.Operator != null?CompareNumbers(value, comparePoint.Value, comparePoint.Operator) : false);
 }