Difference between revisions of "Dice/RollResult/Values"

From DiceRoller Documentation
(→‎Examples: I realize now that omitting add dice was a dumb idea)
Line 31: Line 31:
 
         StringBuilder sb = new StringBuilder();
 
         StringBuilder sb = new StringBuilder();
 
         RollResult result = Roller.Roll("(2d20 + 2) * 1.5"); // rolls a 6 and an 8
 
         RollResult result = Roller.Roll("(2d20 + 2) * 1.5"); // rolls a 6 and an 8
        bool needAdd = false;
 
 
         foreach (DieResult die in result.Values)
 
         foreach (DieResult die in result.Values)
 
         {
 
         {
 
             if (die.DieType == DieType.Special)
 
             if (die.DieType == DieType.Special)
 
             {
 
             {
                needAdd = false;
 
 
                 switch ((SpecialDie)die.Value)
 
                 switch ((SpecialDie)die.Value)
 
                 {
 
                 {
 +
                    case SpecialDie.Add:
 +
                        sb.Append(" + ");
 +
                        break;
 
                     case SpecialDie.Subtract:
 
                     case SpecialDie.Subtract:
 
                         sb.Append(" - ");
 
                         sb.Append(" - ");
Line 53: Line 54:
 
                     case SpecialDie.CloseParen:
 
                     case SpecialDie.CloseParen:
 
                         sb.Append(")");
 
                         sb.Append(")");
                        needAdd = true;
 
 
                         break;
 
                         break;
 
                 }
 
                 }
Line 59: Line 59:
 
             else
 
             else
 
             {
 
             {
                if (needAdd)
 
                {
 
                    sb.Append(" + ");
 
                }
 
 
 
                 sb.Append(die.Value);
 
                 sb.Append(die.Value);
                needAdd = true;
 
 
             }
 
             }
 
         }
 
         }

Revision as of 22:44, 10 April 2017

Gets the results of each individual die rolled.

  • Namespace: Dice
  • Assembly: DiceRoller (in DiceRoller.dll)

Syntax

public IReadOnlyList<DieResult> Values { get; private set; }

Property Value

Type: System.Collections.Generic.IReadOnlyList<Dice.DieResult>

The values of the individual dice rolled.

Remarks

This is not necessarily all dice rolled, just the ones that are exposed to the user. For example, in (1d8)d6, 1d8 is rolled, and that many d6s are rolled. Values will only contain the results of the d6s. Inspecting the d8 requires walking the AST beginning at RollResult.RollRoot. This property is read-only.

Examples

This example iterates through Values to display a roll result.

using System;
using System.Text;
using Dice;

class Sample
{
    public static void Main()
    {
        StringBuilder sb = new StringBuilder();
        RollResult result = Roller.Roll("(2d20 + 2) * 1.5"); // rolls a 6 and an 8
        foreach (DieResult die in result.Values)
        {
            if (die.DieType == DieType.Special)
            {
                switch ((SpecialDie)die.Value)
                {
                    case SpecialDie.Add:
                        sb.Append(" + ");
                        break;
                    case SpecialDie.Subtract:
                        sb.Append(" - ");
                        break;
                    case SpecialDie.Multiply:
                        sb.Append(" * ");
                        break;
                    case SpecialDie.Divide:
                        sb.Append(" / ");
                        break;
                    case SpecialDie.OpenParen:
                        sb.Append("(");
                        break;
                    case SpecialDie.CloseParen:
                        sb.Append(")");
                        break;
                }
            }
            else
            {
                sb.Append(die.Value);
            }
        }

        Console.WriteLine(sb.ToString()); // outputs "(6 + 8 + 2) * 1.5"
    }
}