Class Dice
The most important GoRogue.DiceNotation class -- contains functions to roll dice, and to retrieve an IDiceExpression instance representing a given expression.
Inheritance
Inherited Members
Namespace: GoRogue.DiceNotation
Assembly: GoRogue.dll
Syntax
public static class Dice
Fields
| Improve this Doc View SourceDiceParser
The parser that will be used to parse dice expressions given to the Parse(String) and Roll(String, IGenerator) functions. If you want to use a custom parser, you can assign an instance to this field.
Declaration
public static IParser DiceParser
Field Value
Type | Description |
---|---|
IParser |
Methods
| Improve this Doc View SourceParse(String)
Uses the IParser specified in the DiceParser variable to produce an IDiceExpression instance representing the given dice expression.
Declaration
public static IDiceExpression Parse(string expression)
Parameters
Type | Name | Description |
---|---|---|
System.String | expression | The string dice expression to parse. |
Returns
Type | Description |
---|---|
IDiceExpression | An IDiceExpression instance representing the parsed string. |
Remarks
Generally speaking, dice-parsing via the standard Roll(String, IGenerator) method is extremely fast. However, if you are repeating a dice roll many times, in a case where maximum performance is absolutely necessary, there is some benefit to retrieving an IDiceExpression instance instead of using the Roll function, and calling that expressions's Roll(IGenerator) method whenever a result is required.
Roll(String, IGenerator)
Uses the IParser specified in the DiceParser variable to parse the given dice expression, roll it, and return the result. This is the standard method for rolling dice.
Declaration
public static int Roll(string expression, IGenerator random = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | expression | The string dice expression to parse. |
Troschuetz.Random.IGenerator | random | RNG to use to perform the roll. If null is specified, the default RNG is used. |
Returns
Type | Description |
---|---|
System.Int32 | The result of evaluating the dice expression given. |
Remarks
This method is convenient and typically very fast, however technically, parsing is computationally more expensive than evaluation. If a dice expression will be rolled many times in a situation where maximum performance is required, it is more efficient to use the Parse(String) method once, and use the resulting IDiceExpression instance to roll the expression each time it is needed.