Swiftpack.co - Package - bsiegfreid/Dice


Dice is a Swift library that provides functions for rolling dice and a command line executable. It was created as a sample project using Swift 5.2 and the Swift Package Manager. Part of this exercise was using the GitHub release function. Tag and title the release with a semantic version number, such as v1.2.3, and the Swift Package Manager will be able to pull that release as a dependency into your project directly from GitHub.

This module is designed for use with the Swift Package Manager. In your Package.swift file add a reference to this project:

let package = Package(
    name: "myapp",
    dependencies: [
        .package(url: "https://github.com/bsiegfreid/Dice", from: "0.1.0"),
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
            name: "myapp",
            dependencies: [
                .product(name: "Dice", package: "Dice"),
            name: "myapp",
            dependencies: ["myapp"]),

Within your Swift module import Dice:

import Dice

You can specify number of rolls and faces. In this example you can roll three six-sided dice:

let result = roll(3, d:6)

You can request a roll using standard tabletop dice notation or within a range. This throws to check for invalid range syntax. Range is a starting integer, followed by a dash, with an ending integer. If you input a reversed range, such as 3-1, then Dice will correct it to allow proper rolling.

let result1 = try roll("3d6")
let result2 = try roll("1-4")

To build a release version and install:

swift build --configuration release
cp -f .build/release/roll /usr/local/bin/roll

After installation you can roll the dice from the command line:

roll 3d6
roll 1-100

There is now the option to drop the lowest roll:

roll 4d6 --drop-lowest


Stars: 1


Used By

Total: 0


v0.2.0 - 2020-09-06 03:15:56

Added ability to drop the lowest roll in a multiple dice roll.

v0.1.0 - 2020-08-17 02:29:13

Added support for standard tabletop dice notation and an RNG seed.

v0.0.2 - 2020-08-16 17:33:00

Added ability to request a range roll such as let result = try rollRange("1-4")

v0.0.1 - 2020-08-16 15:44:27

This is an extremely basic but functional release in order to test the process.