Die

public struct Die
extension Die: Rollable
extension Die: Equatable
extension Die: Comparable
extension Die: Hashable
extension Die: Describable

A representation of a single die.

It can be rolled using the roll() method, which will give a Roll result.

A Die object cannot change. Use the addition operators and the Dice class to represent more complex dice expressions.

Author

Samasaur
  • The number of sides on this Die. This value does not need to be possible (for example, it can be 13), but it does need to be larger than 0.

    Declaration

    Swift

    public let sides: Int
  • Creates a new Die with the given number of sides.

    This initializer will fail if the number of sides is <= 0.

    Throws

    An Error.IllegalNumberOfSides error when the number of sides is less than or equal to 0

    Declaration

    Swift

    public init(sides: Int) throws

    Parameters

    sides

    The number of sides for this die.

  • Creates a new Die from the given string in dice notation.

    You cannot have a negative die (-d6), a die with negative sides (d-6), or a die with 0 sides (d0). Because this is a Die initializer, you can only have one die (no 2d6).

    Throws

    Numerous errors if the string is malformed or empty.

    Declaration

    Swift

    public init(_ str: String) throws

    Parameters

    str

    The string to convert from.

  • Creates a new Die that is a copy of the given Die.

    Declaration

    Swift

    @available(*, deprecated, message: "CustomDie is now a struct; copying is not necessary")
    public init(copyOf other: Die)

    Parameters

    other

    The other Die to copy.

  • The probabilities of all possible rolls.

    Since

    0.17.0

    Declaration

    Swift

    public var probabilities: Chances { get }
  • Rolls this Die and returns the result as a Roll.

    This function returns a random integer in the range 1 to sides, inclusive ([1, sides]).

    Declaration

    Swift

    public func roll() -> Roll

    Return Value

    A random value from 1 to sides.

  • The minimum possible result from using the roll() method.

    This method simulates rolling a 1 on this die.

    Since

    0.2.0

    Declaration

    Swift

    public var minimumResult: Roll { get }
  • The maximum possible result from using the roll() method.

    This method simulates rolling the maximum on this die.

    Since

    0.2.0

    Declaration

    Swift

    public var maximumResult: Roll { get }
  • The exact (double) average result from using the roll() method. This is used in the Dice method to avoid rounding errors.

    Since

    0.15.0

    Declaration

    Swift

    public var doubleAverageResult: Double { get }
  • The average result from using the roll() method.

    Since

    0.15.0

    Declaration

    Swift

    public var averageResult: Roll { get }
  • Determines whether this Die can reach the target Roll using the given comparison type.

    Since

    0.15.0

    Declaration

    Swift

    public func canReach(_ target: Roll, _ comparisonType: RollComparison) -> Bool

    Parameters

    target

    The target to check reachibility for.

    comparisonType

    The comparison to use when checking reachibility.

    Return Value

    Whether or not this die can reach the target, using the given comparison.

  • Declaration

    Swift

    public static func == (lhs: Die, rhs: Die) -> Bool
  • Declaration

    Swift

    public static func < (lhs: Die, rhs: Die) -> Bool
  • Declaration

    Swift

    public func hash(into hasher: inout Hasher)
  • Declaration

    Swift

    public var description: String { get }
  • Declaration

    Swift

    public var debugDescription: String { get }
  • Returns a copy of the given Die with separate memory.

    Declaration

    Swift

    @available(*, deprecated, message: "Die is now a struct; copying is not necessary")
    func copy() -> Die

    Return Value

    A copy of the given Die, with the same number of sides, at a different memory location.

  • d4

    A four-sided die (d4).

    Declaration

    Swift

    static var d4: Die { get }
  • d6

    A six-sided die (d6).

    Declaration

    Swift

    static var d6: Die { get }
  • d8

    An eight-sided die (d8).

    Declaration

    Swift

    static var d8: Die { get }
  • d10

    A ten-sided die (d10).

    Declaration

    Swift

    static var d10: Die { get }
  • d12

    A twelve-sided die (d12).

    Declaration

    Swift

    static var d12: Die { get }
  • d20

    A twenty-sided die (d20).

    Declaration

    Swift

    static var d20: Die { get }
  • A one hundred-sided die (d100).

    Declaration

    Swift

    static var d100: Die { get }