Class Dice
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
dieCountintThe number of dice
sideCountintThe number of sides on each die
modifierintAn 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
MaxValue
The maximum value that can be rolled with the specified dice and modifier.
public int MaxValue { get; }
Property Value
MinValue
The minimum value that can be rolled with the specified dice and modifier.
public int MinValue { get; }
Property Value
Modifier
An optional modifier to add to the total of the dice rolled.
public int Modifier { get; }
Property Value
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
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
objobjectThe 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
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
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
Returns
- int
int
Exceptions
- ArgumentNullException
Thrown when the
objis 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
notationstringThe 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.