RollCraft 0.1.6

dotnet add package RollCraft --version 0.1.6
                    
NuGet\Install-Package RollCraft -Version 0.1.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RollCraft" Version="0.1.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RollCraft" Version="0.1.6" />
                    
Directory.Packages.props
<PackageReference Include="RollCraft" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RollCraft --version 0.1.6
                    
#r "nuget: RollCraft, 0.1.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RollCraft@0.1.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RollCraft&version=0.1.6
                    
Install as a Cake Addin
#tool nuget:?package=RollCraft&version=0.1.6
                    
Install as a Cake Tool

RollCraft

NuGet NuGet

A sophisticated library for creating dice expressions in .NET.

Installation

dotnet add package RollCraft

Features

Error Handling

RollCraft uses the Result type to handle errors in a functional way. This uses the MonadCraft library (which is another library of mine) which provides the Result type.

The rationale behind this decision is to avoid using exceptions, which can be expensive in terms of performance, especially since there's a lot of error handling in this library.

Parsing

RollCraft can parse dice expressions through the use of strings into a DiceExpression object.

using MonadCraft;
using RollCraft;

Result<RollError, DiceExpression<int>> expression = DiceExpressionParser.Parse<int>("2d6 + 3");

RollCraft has the notion of specifying the type of the number result, which is done through generics.

Note: Only int and double are supported at the moment.

using MonadCraft;
using RollCraft;

Result<RollError, DiceExpression<double>> expression = DiceExpressionParser.Parse<double>("2d6 / 2.2");

Evaluation

Basic usage of RollCraft involves creating a DiceExpressionEvaluator object and using it to evaluate a dice expression:

using MonadCraft;
using RollCraft;

DiceExpressionEvaluator evaluator = DiceExpresionEvaluator.CreateRandom();
Result<RollError, DiceExpressionResult<RollError, int>> result = evaluator.Evaluate("2d6 + 3");

result.Perform(
    success: (r) => Console.WriteLine(r),
    failure: (e) => Console.WriteLine($"Error: {e.Message}")
);

Besides providing the dice expression string directly, you can also use the DiceExpression object directly which has the benefit of already being parsed:

using MonadCraft;
using RollCraft;

DiceExpressionEvaluator evaluator = DiceExpresionEvaluator.CreateRandom();
Result<RollError, DiceExpression<int>> expression = DiceExpressionParser.Parse<int>("2d6 + 3");

Result<RollError, DiceExpressionResult<RollError, int>> result = evaluator.Evaluate(expression);
// Or
Result<RollError, DiceExpressionResult<RollError, int>> result = expression.Bind(e => evaluator.Evaluate(e));

The DiceExpressionEvaluator can be created with a variety of static constructors:

CreateRandom() // Creates a roller that always rolls a random number on the dice
CreateSeededRandom(int seed) // Creates a roller that always rolls a random number on the dice with a seed
CreateMinimum() // Creates a roller that always returns the minimum value on the dice
CreateMaximum() // Creates a roller that always returns the maximum value on the dice
CreateFixedAverage() // Creates a roller that always returns the fixed average value of the dice

The DiceExpressionResult object contains the result of the evaluation, which includes the individual dice rolls and the final result. The number generic type that's used in parsing and evaluation will be the same type as the result.

Expression Syntax

The syntax for dice expressions is as follows:

  • Arithmetic operators: +, -, *, /
  • Unary: -
  • Brackets: (, )
  • Dice: NdM, where N is the number of dice and M is the number of sides
  • Dice Modifiers:
    • !: Exploding (4d6! explodes on 6)
    • k or kh: Keep highest (4d6kh3 keeps the highest 3 dice)
    • r: Reroll (4d6r keeps rerolling the dice on 1)
    • ro: Reroll once (4d6ro rerolls the dice on 1 once)
    • min: Minimum (4d6min2 Rolls below 2 are treated as 2)
    • max: Maximum (4d6max5 Rolls above 5 are treated as 5)
  • Compare operators (applies to Exploding, Reroll and Reroll once):
    • <=: Less than or equal to (4d6r<=2 rerolls the dice on 2 or less)
    • <: Less than (4d6r<3 rerolls the dice on 2 or less)
    • >=: Greater than or equal to (4d6!>=4 explodes on 4 or more)
    • >: Greater than (4d6!>4 explodes on more than 4)
    • =: Equal to (4d6r=4 rerolls the dice on 4)
    • <>: Not equal to (4d6r<>6 rerolls the dice on values other than 6)
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.6 182 8/27/2025
0.1.5 112 12/10/2024
0.1.4 111 10/23/2024
0.1.3 138 10/18/2024
0.1.2 142 10/18/2024
0.1.1 106 10/17/2024
0.1.0 109 10/17/2024