项目作者: kiliankoe

项目描述 :
🎲 Evaluate dice notation in Swift
高级语言: Swift
项目地址: git://github.com/kiliankoe/D20.git
创建时间: 2019-09-10T08:40:21Z
项目社区:https://github.com/kiliankoe/D20

开源协议:MIT License

下载


D20

Need to evaluate dice notation in Swift? This package is for you.

Examples

  1. let roll = Roll("1d20")!.roll()
  2. roll.result
  3. // $R0: Int = 18
  4. let roll = Roll("3d8+1d6")!.roll()
  5. roll.result
  6. // $R0: Int = 19
  7. roll.expression
  8. // $R1: String = "1+6+7+5"
  9. // Percentage die shorthand is also supported.
  10. let roll = Roll("d%")
  11. D(20).average
  12. // $R0: Double = 10.5

The rest of the API is pretty self-explanatory. Dice and Rolls have attributes for accessing the max, half and average values of their rolls. Use the roll() function to actually throw the dice on both of these types and get some random values.

CLI

This package also includes a small CLI to roll the dice directly in your terminal.

  1. $ d20 2d6+4
  2. # 14 (6+4)+4
  3. $ alias dagger='d20 1d4+2'
  4. $ dagger
  5. # 6 (4)+2
  6. # CRIT!
  7. # Invoke it without an argument to enter REPL mode.
  8. $ d20
  9. > d4
  10. 3 (3)
  11. > 2d4
  12. 6 (2+4)

Installing it is easy. Just run the following after cloning the repo.

  1. $ swift build -c release
  2. $ cp .build/release/d20 /usr/local/bin/d20

Logo Credits

D20 by Lonnie Tapscott from the Noun Project