Dice

public struct Dice : Caching
extension Dice: Rollable
extension Dice: Equatable
extension Dice: Hashable
extension Dice: Describable

A collection of Die objects and/or a modifier that can be rolled using the roll() method.

The properties of Dice objects are immutable; use the addition operators to combine them with other Die objects or modifiers. You can use compound assignment operators if you want, so long as you declare the Dice object as a var instead of a let constant.

Author

Samasaur
  • The dice that make up this collection, along with how many times they appear.

    This [Die: Int] dictionary stores the types of dice that appear, paired with the number of times they appear. For example:

    let dice1 = Dice(Die.d6)
    dice1.dice // [d6: 1]
    
    let dice2 = Dice(Die.d6, Die.d6, Die.d4)
    dice2.dice // [d6: 2, d4: 1]
    

    To iterate through this dictionary, do the following:

    let dice = Dice(Die.d6, Die.d6, Die.d4)
    for (die, count) in dice.dice {
        print("\(die.description) appears \(count) time(s)")
    

    Output:

    A six-sided die appears 2 time(s)
    A four-sided die appears 1 time(s)
    

    To add a die to the dictionary (only applicable if using a mutable imitiation):

    let dice = Dice(Die.d6, Die.d6, Die.d4)
    let dieToAdd = Die.d8
    dice.dice[dieToAdd] = (dice.dice[dieToAdd] ?? 0) + 1
    // dice.dice is [d6: 2, d8: 1, d4: 1]
    let otherDie = Die.d4
    dice.dice[otherDie] = (dice.dice[otherDie] ?? 0) + 1
    // dice.dice is [d6: 2, d8: 1, d4: 2]
    

    To convert the dictionary to an array of (Die, Int) tuples, sorted by descending size of dice:

    let dice = Dice(Die.d6, Die.d6, Die.d4)
    let diceArray = dice.dice.sorted(by: { $0.key > $1.key })
    // diceArray is [(d6, 2), (d4, 1)]
    

    To convert the dictionary to an array of Die objects, including duplicates:

    let dice = Dice(Die.d6, Die.d6, Die.d4)
    var diceArray: [Die] = []
    for (die, count) in dice.dice {
        for _ in 0..<count {
            diceArray += die.copy()
        }
    }
    // diceArray is [d6, d6, d4]
    

    Declaration

    Swift

    public let dice: [Die : Int]
  • The number of dice in this Dice instance.

    Since

    0.2.0

    Declaration

    Swift

    public var numberOfDice: Int { get }
  • The modifier added or subtracted from these Dice

    This Int is added to the result whenever the roll() method is called.

    Declaration

    Swift

    public let modifier: Int
  • Creates a new Dice object with the specified dice.

    Declaration

    Swift

    public init(_ dice: Die...)

    Parameters

    dice

    The dice to include in this Dice object.

  • Creates a new Dice object with the specified dice.

    Declaration

    Swift

    public init(dice: [Die])

    Parameters

    dice

    The dice to include in this Dice object.

  • Creates a new Dice object with the given number of the specified die. This is essentially identical to Dice(copyOf: die * count), but is more efficient.

    Since

    0.6.0

    Declaration

    Swift

    public init(_ die: Die, count: Int)

    Parameters

    die

    The die to include in this Dice object.

    count

    The number of times the specified die should appear.

  • Creates a new Dice object with the specified dice. They should be in a (die: Die, count: Int) form, with the die being the type of die to add, and the count being the number of times to add it.

    Since

    0.6.0

    Declaration

    Swift

    public init(_ dieIntTuples: (die: Die, count: Int)...)

    Parameters

    dieIntTuples

    The dice to add, in the form (dieType, count).

  • Creates a new Dice object with the specified dice and modifier

    Declaration

    Swift

    public init(_ dice: Die..., withModifier modifier: Int)

    Parameters

    dice

    The dice to include in this Dice object.

    modifier

    The modifier to apply to every roll.

  • Creates a new Dice object with the specified dice and modifier

    Declaration

    Swift

    public init(dice: [Die], withModifier modifier: Int)

    Parameters

    dice

    The dice to include in this Dice object.

    modifier

    The modifier to apply to every roll

  • Creates a new Dice object with the given number of the specified die, along with the specified modifier. This is essentially identical to Dice(copyOf: (die * count) + modifier), but is more efficient.

    Since

    0.6.0

    Declaration

    Swift

    public init(_ die: Die, count: Int, withModifier modifier: Int)

    Parameters

    die

    The die to include in this Dice object.

    count

    The number of times the specified die should appear.

    modifier

    The modifier to apply to every roll.

  • Creates a new Dice object with the specified dice and modifier. The dice should be in a (die: Die, count: Int) form, with the die being the type of die to add, and the count being the number of times to add it.

    Since

    0.6.0

    Declaration

    Swift

    public init(_ dieIntTuples: (die: Die, count: Int)..., withModifier modifier: Int)

    Parameters

    dieIntTuples

    The dice to add, in the form (dieType, count).

    • modifier: The modifer to apply to every roll.
  • Creates a new Dice object from the specified string in dice notation.

    You cannot have a negative die AS A RESULT (-d6), a die with negative sides (d-6), or a die with 0 sides (d0). You cannot have an unreal modifier or use any operator except for addition and subtraction.

    You can have -d6s in your string, so long as they cancel each other out so that the final result is at least 0d6.

    Throws

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

    Declaration

    Swift

    public init(_ str: String) throws

    Parameters

    str

    The string to convert.

  • Creates a new Dice object that is a copy of the given Dice object.

    Declaration

    Swift

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

    Parameters

    other

    The other Dice object to copy.

  • The probabilities of all possible rolls.

    Since 0.22.0, caches previous computations, even if they were on different objects. See enableCaching, ENABLE_CACHING for caching configuration.

    In 0.24.0, the computation of this value was vastly overhauled, resulting in an up to 99.9% or more decrease in calculation times (up to 1000 or more times as fast)

    Since

    0.17.0

    Declaration

    Swift

    public var probabilities: Chances { get }
  • Whether or not Dice should cache the results of probability computations across objects.

    Note: The results of rolling are NOT cached.

    Setting this value to false and then to true will clear the cache. See ENABLE_CACHING for configuration of caching for all types at once.

    Since

    0.22.0

    Declaration

    Swift

    public static var enableCaching: Bool { get set }
  • Rolls this Dice object.

    This function rolls every Die included in this Dice object, adds them together, adds the modifier (if any), and returns the result

    Declaration

    Swift

    public func roll() -> Roll

    Return Value

    The sum of the rolls of every Die in this Dice + the modifier.

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

    This method simulates rolling a 1 on every die in this Dice object. It also includes the modifier, if applicable.

    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 every die in this Dice object. It also includes the modifier, if applicable.

    Since

    0.2.0

    Declaration

    Swift

    public var maximumResult: Roll { get }
  • The average result from using the roll() method. It is calculated with double numbers to avoid rounding errors.

    Since

    0.15.0

    Declaration

    Swift

    public var averageResult: Roll { get }
  • Determines whether this Dice object 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 Dice object can reach the target, using the given comparison.

  • Declaration

    Swift

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

    Swift

    public func hash(into hasher: inout Hasher)
  • A description of this Dice object.

    Dice().description // No dice, without a modifier.
    
    Dice(Die.d6).description // 1 6-sided die, with no modifier.
    
    Dice(Die.d6, Die.d6, Die.d4).description // 2 6-sided dice, 1 four-sided die, with no modifier.
    
    Dice(withModifier: 5).description // A modifier of 5.
    
    Dice(Die.d6, withModifier: 5).description // 1 6-sided die, with a modifier of 5.
    
    Dice(Die.d6, Die.d6, Die.d4, withModifier: 5).description // 2 6-sided dice, 1 four-sided die, with a modifier of 5.
    

    Declaration

    Swift

    public var description: String { get }
  • A short, debug-usable description of this Dice object.

    Dice().debugDescription // 0
    
    Dice(Die.d6).debugDescription // 1d6
    
    Dice(Die.d6, Die.d6, Die.d4).debugDescription // 2d6 + 1d4
    
    Dice(withModifier: 5).debugDescription // + 5
    
    Dice(Die.d6, withModifier: 5).debugDescription // 1d6 + 5
    
    Dice(Die.d6, Die.d6, Die.d4, withModifier: 5).debugDescription // 2d6 + 1d4 + 5
    

    Declaration

    Swift

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

    Declaration

    Swift

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

    Return Value

    A copy of the given Dice, with the same information, at a different memory location.

  • Adds some Dice and a Die together, creating a new Dice object.

    Declaration

    Swift

    static func + (lhs: Dice, rhs: Die) -> Dice

    Parameters

    lhs

    The dice to add.

    rhs

    The die to add.

    Return Value

    A new Dice object comprising of the Die and Dice added together.

  • Adds some Dice and a Die together, creating a new Dice object.

    Declaration

    Swift

    static func + (lhs: Die, rhs: Dice) -> Dice

    Parameters

    lhs

    The die to add.

    rhs

    The dice to add.

    Return Value

    A new Dice object comprising of the Die and Dice added together.

  • Adds some Dice together, creating a new Dice object.

    Declaration

    Swift

    static func + (lhs: Dice, rhs: Dice) -> Dice

    Parameters

    lhs

    The first set of dice to add.

    rhs

    The second set of dice to add.

    Return Value

    A new Dice object comprising of the Dice added together.

  • Adds a modifier to a Dice object.

    Declaration

    Swift

    static func + (lhs: Dice, rhs: Int) -> Dice

    Parameters

    lhs

    The dice.

    rhs

    The modifier to add.

    Return Value

    A new Dice object comprising of the modifier added to the first dice.

  • Adds a modifier to a Dice object.

    Declaration

    Swift

    static func + (lhs: Int, rhs: Dice) -> Dice

    Parameters

    lhs

    The modifier to add.

    rhs

    The dice.

    Return Value

    A new Dice object comprising of the modifier added to the first dice.

  • Adds the given dice to the given Dice object.

    Declaration

    Swift

    static func + (lhs: Dice, rhs: (die: Die, count: Int)) -> Dice

    Parameters

    lhs

    The Dice object.

    rhs

    The dice to add, in (Die, Int) tuples.

    Return Value

    A new Dice object comprising of the new dice added to the initial Dice object.

  • Adds the given dice to the given Dice object.

    Declaration

    Swift

    static func + (lhs: (die: Die, count: Int), rhs: Dice) -> Dice

    Parameters

    lhs

    The dice to add, in (Die, Int) tuples.

    rhs

    The Dice object.

    Return Value

    A new Dice object comprising of the new dice added to the initial Dice object.

  • Subtracts a modifier from a Dice object.

    Declaration

    Swift

    static func - (lhs: Dice, rhs: Int) -> Dice

    Parameters

    lhs

    The dice.

    rhs

    The modifier to subtract.

    Return Value

    A new Dice object comprising of the modifier subtracted from the first dice.

  • Multiplies the given Dice object by the given multiplier.

    This multiplies the count of each type of die, and the multiplier:

    let dice = Dice((Die.d6, 6), (Die.d4, 4), withModifier: 2) let newDice = dice * 3 //newDice is now 18d6 + 12d4 + 6

    Declaration

    Swift

    static func * (lhs: Dice, rhs: Int) -> Dice

    Parameters

    lhs

    The Dice object to multiply.

    rhs

    The multiplier.

    Return Value

    A new Dice object comprising of the given dice multiplied by the multiplier

  • Multiplies the given Dice object by the given multiplier.

    This multiplies the count of each type of die, and the multiplier:

    let dice = Dice((Die.d6, 6), (Die.d4, 4), withModifier: 2) let newDice = dice * 3 //newDice is now 18d6 + 12d4 + 6

    Declaration

    Swift

    static func * (lhs: Int, rhs: Dice) -> Dice

    Parameters

    lhs

    The multiplier.

    rhs

    The Dice object to multiply.

    Return Value

    A new Dice object comprising of the given dice multiplied by the multiplier