Class Dice

Namespace
Wolfgang.D20
Assembly
Wolfgang.D20.Dice.dll

Represents a number of dice, each with the specified number of sides and an optional modifier.

public sealed class Dice : IDice, IEquatable<Dice>, IEqualityComparer<Dice>
Inheritance
Dice
Implements
Inherited Members

Constructors

Dice(int, int, int)

Constructs a new instance of Dice with the specified number of dice, sides, and modifier.

public Dice(int dieCount = 1, int sideCount = 6, int modifier = 0)

Parameters

dieCount int

The number of dice

sideCount int

The number of sides on each die

modifier int

An optional modifier to add to the result

Remarks

sideCount of 2 represents a coin toss

Exceptions

ArgumentOutOfRangeException

dieCount is less than 1

ArgumentOutOfRangeException

sideCount is less than 2

Properties

DieCount

The number of dice to roll. Must be greater than 0.

public int DieCount { get; }

Property Value

int

MaxValue

The maximum value that can be rolled with the specified dice and modifier.

public int MaxValue { get; }

Property Value

int

MinValue

The minimum value that can be rolled with the specified dice and modifier.

public int MinValue { get; }

Property Value

int

Modifier

An optional modifier to add to the total of the dice rolled.

public int Modifier { get; }

Property Value

int

Remarks

The value can be positive or negative, and can be used to adjust the result of the roll.

SideCount

The number of sides on each die. Must be at least 2.

public int SideCount { get; }

Property Value

int

Remarks

A value of 2 represents a coin toss, 3 represents a three-sided die, etc.

Methods

Equals(object?)

Checks if this instance is equal to another object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with this instance. It can be null or of any type.

Returns

bool

True if the object is a Dice instance and has the same DieCount, SideCount, and Modifier as this instance; otherwise, false.

Equals(Dice?)

Checks if this instance is equal to another instance of Dice.

public bool Equals(Dice? other)

Parameters

other Dice

The other instance of Dice to compare with this instance.

Returns

bool

True if both instances have the same DieCount, SideCount, and Modifier; otherwise, false.

Equals(Dice?, Dice?)

Determines whether two Dice instances are equal.

public bool Equals(Dice? x, Dice? y)

Parameters

x Dice

The first Dice instance.

y Dice

The second Dice instance.

Returns

bool

true if the two instances are equal; otherwise, false.

GetHashCode()

Generates a hash code for this instance based on its DieCount, SideCount, and Modifier.

public override int GetHashCode()

Returns

int

int

GetHashCode(Dice)

Generates a hash code for this instance based on its DieCount, SideCount, and Modifier.

public int GetHashCode(Dice obj)

Parameters

obj Dice

The Dice instance for which to generate the hash code.

Returns

int

int

Exceptions

ArgumentNullException

Thrown when the obj is null.

Roll()

Rolls the dice and returns the total value rolled, including any modifier.

public int Roll()

Returns

int

int

ToString()

Returns a string representation of the dice in the format "XdY+Z" where: x is the number of dice, y is the number of sides on each die, z is the modifier (if any).

public override string ToString()

Returns

string

string

Remarks

If the modifier is 0, it is omitted from the string.

TryParse(string?)

Tries to parse a string representation of dice notation into a Dice instance.

public static Result<Dice?> TryParse(string? notation)

Parameters

notation string

The string representation of the dice notation.

Returns

Result<Dice>

A Wolfgang.TryPattern.Result<T> containing the parsed Dice instance if successful; otherwise, a failed result with Wolfgang.TryPattern.Result.ErrorMessage describing the failure. Accessing Wolfgang.TryPattern.Result<T>.Value on a failed result throws InvalidOperationException.