Difference between revisions of "Dice/RollResult/Values"

From DiceRoller Documentation
(→‎Examples: I realize now that omitting add dice was a dumb idea)
Line 18: Line 18:
  
 
== Examples ==
 
== Examples ==
This example iterates through Values to display a roll result.
+
This example iterates through Values to display a roll result. Note that the following code is how [[../ToString|RollResult.ToString()]] is implemented.
  
 
<syntaxhighlight lang="C#">
 
<syntaxhighlight lang="C#">

Revision as of 02:59, 14 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. Note that the following code is how RollResult.ToString() is implemented.

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"
    }
}