Dice
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.
-
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
Dieobjects, 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
Diceinstance.Since
0.2.0Declaration
Swift
public var numberOfDice: Int { get } -
The modifier added or subtracted from these
DiceThis
Intis added to the result whenever theroll()method is called.Declaration
Swift
public let modifier: Int -
Creates a new
Diceobject with the specified dice.Declaration
Swift
public init(_ dice: Die...)Parameters
diceThe dice to include in this
Diceobject. -
Creates a new
Diceobject with the specified dice.Declaration
Swift
public init(dice: [Die])Parameters
diceThe dice to include in this
Diceobject. -
Creates a new
Diceobject with the given number of the specified die. This is essentially identical toDice(copyOf: die * count), but is more efficient.Since
0.6.0
Declaration
Swift
public init(_ die: Die, count: Int)Parameters
dieThe die to include in this
Diceobject.countThe number of times the specified die should appear.
-
Creates a new
Diceobject 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
dieIntTuplesThe dice to add, in the form (dieType, count).
-
Creates a new
Diceobject with the specified dice and modifierDeclaration
Swift
public init(_ dice: Die..., withModifier modifier: Int)Parameters
diceThe dice to include in this
Diceobject.modifierThe modifier to apply to every roll.
-
Creates a new
Diceobject with the specified dice and modifierDeclaration
Swift
public init(dice: [Die], withModifier modifier: Int)Parameters
diceThe dice to include in this
Diceobject.modifierThe modifier to apply to every roll
-
Creates a new
Diceobject with the given number of the specified die, along with the specified modifier. This is essentially identical toDice(copyOf: (die * count) + modifier), but is more efficient.Since
0.6.0
Declaration
Swift
public init(_ die: Die, count: Int, withModifier modifier: Int)Parameters
dieThe die to include in this
Diceobject.countThe number of times the specified die should appear.
modifierThe modifier to apply to every roll.
-
Creates a new
Diceobject 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
dieIntTuplesThe dice to add, in the form (dieType, count).
- modifier: The modifer to apply to every roll.
-
Creates a new
Diceobject 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 least0d6.Throws
AnError.IllegalNumberOfSideserror when the number of sides is less than or equal to 0Declaration
Swift
public init(_ str: String) throwsParameters
strThe string to convert.
-
Creates a new
Diceobject that is a copy of the givenDiceobject.Declaration
Swift
@available(*, deprecated, message: "Dice is now a struct; copying is not necessary") public init(copyOf other: Dice)Parameters
otherThe other
Diceobject 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_CACHINGfor 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.0Declaration
Swift
public var probabilities: Chances { get } -
Whether or not
Diceshould cache the results of probability computations across objects.Note: The results of rolling are NOT cached.
Setting this value to
falseand then totruewill clear the cache. SeeENABLE_CACHINGfor configuration of caching for all types at once.Since
0.22.0Declaration
Swift
public static var enableCaching: Bool { get set } -
Determines whether this
Diceobject can reach the targetRollusing the given comparison type.Since
0.15.0
Declaration
Swift
public func canReach(_ target: Roll, _ comparisonType: RollComparison) -> BoolParameters
targetThe target to check reachibility for.
comparisonTypeThe comparison to use when checking reachibility.
Return Value
Whether or not this
Diceobject 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
Diceobject.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
Diceobject.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 + 5Declaration
Swift
public var debugDescription: String { get } -
Returns a copy of the given
Dicewith separate memory.Declaration
Swift
@available(*, deprecated, message: "Dice is now a struct; copying is not necessary") func copy() -> DiceReturn Value
A copy of the given
Dice, with the same information, at a different memory location. -
Adds some
Dicetogether, creating a newDiceobject.Declaration
Swift
static func + (lhs: Dice, rhs: Dice) -> DiceParameters
lhsThe first set of dice to add.
rhsThe second set of dice to add.
Return Value
A new
Diceobject comprising of theDiceadded together. -
Adds a modifier to a
Diceobject.Declaration
Swift
static func + (lhs: Dice, rhs: Int) -> DiceParameters
lhsThe dice.
rhsThe modifier to add.
Return Value
A new
Diceobject comprising of the modifier added to the first dice. -
Adds a modifier to a
Diceobject.Declaration
Swift
static func + (lhs: Int, rhs: Dice) -> DiceParameters
lhsThe modifier to add.
rhsThe dice.
Return Value
A new
Diceobject comprising of the modifier added to the first dice. -
Adds the given dice to the given
Diceobject.Declaration
Swift
static func + (lhs: Dice, rhs: (die: Die, count: Int)) -> DiceParameters
lhsThe
Diceobject.rhsThe dice to add, in
(Die, Int)tuples.Return Value
A new
Diceobject comprising of the new dice added to the initialDiceobject. -
Adds the given dice to the given
Diceobject.Declaration
Swift
static func + (lhs: (die: Die, count: Int), rhs: Dice) -> DiceParameters
lhsThe dice to add, in
(Die, Int)tuples.rhsThe
Diceobject.Return Value
A new
Diceobject comprising of the new dice added to the initialDiceobject. -
Subtracts a modifier from a
Diceobject.Declaration
Swift
static func - (lhs: Dice, rhs: Int) -> DiceParameters
lhsThe dice.
rhsThe modifier to subtract.
Return Value
A new
Diceobject comprising of the modifier subtracted from the first dice. -
Multiplies the given
Diceobject 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) -> DiceParameters
lhsThe
Diceobject to multiply.rhsThe multiplier.
Return Value
A new
Diceobject comprising of the given dice multiplied by the multiplier -
Multiplies the given
Diceobject 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) -> DiceParameters
lhsThe multiplier.
rhsThe
Diceobject to multiply.Return Value
A new
Diceobject comprising of the given dice multiplied by the multiplier
View on GitHub
Dice Structure Reference