Class Die

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

Represents a single die with a fixed number of sides.

public sealed class Die : IDie, IEquatable<Die>
Inheritance
Die
Implements
Inherited Members
Extension Methods

Constructors

Die(int)

Constructs a new instance of Die with the specified number of sides.

public Die(int sideCount = 6)

Parameters

sideCount int

The number of sides on the die

Examples

var d20 = new Die(20);
int result = d20.Roll(); // a value in [1, 20]

Remarks

sideCount of 2 represents a coin toss

Exceptions

ArgumentOutOfRangeException

sideCount is less than 2

Properties

MaxValue

The maximum value that can be rolled on the die. Equal to SideCount.

public int MaxValue { get; }

Property Value

int

MinValue

The minimum value that can be rolled on the die. Always 1.

public int MinValue { get; }

Property Value

int

SideCount

The number of sides on the 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 Die instance with the same SideCount; otherwise, false.

Equals(Die?)

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

public bool Equals(Die? other)

Parameters

other Die

The other instance of Die to compare with this instance.

Returns

bool

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

GetHashCode()

Generates a hash code for this instance based on its SideCount.

public override int GetHashCode()

Returns

int

A hash code derived from SideCount.

Roll()

Rolls the die and returns the value rolled.

public int Roll()

Returns

int

A uniform random value in [MinValue, MaxValue] inclusive.

Examples

var d6 = new Die(6);
int result = d6.Roll(); // a value in [1, 6]

ToString()

Returns a string representation of the die in the format "dY" where Y is the number of sides.

public override string ToString()

Returns

string

The die in standard dY notation.

Examples

string notation = new Die(20).ToString(); // "d20"