A powerful and expressive dice expression parser.
You can install Dice with JitPack (Gradle example shown below), check out the Releases page, or download the source code and build it yourself.
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.swordglowsblue:dice:master-SNAPSHOT'
}
See here.
Most basic dice notation should work outright. See the page on Expression Syntax for details.
import com.swordglowsblue.dice.*
val expr: DiceExpr = DiceParser.parse("4d6")
// => BasicDice(Const(4), Const(6))
val result: EvalResult = expr.eval()
result.expr //: DiceExpr - The expression
result.value //: Int - The result of the expression
result.subRolls //: List<RollResult> - Dice rolls made during evaluation
val rollResult: RollResult = expr.subRolls[0]
result.expr //: Dice - The dice that were rolled
result.value //: Int - The result of the roll
result.rolls //: List<Int> - Individual die results
result.subRolls //: List<RollResult> - Other rolls made as part of this roll