Last active
July 4, 2016 05:04
-
-
Save esnya/8d5e256bac2152500977cdf1e53b954e to your computer and use it in GitHub Desktop.
`[n] d [m]` dice roll for Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.nekometer.ukatama.scala | |
object dice { | |
import scala.util.Random | |
class DiceResult(val sum: Int, val list: Seq[Int]) { | |
override def toString = "DiceResult(" + sum + "," + list + ")" | |
def penalty(i: Int): Int = sum - i | |
def p = penalty(_) | |
def bonus(i: Int): Int = sum + i | |
def b = bonus(_) | |
} | |
object DiceResult { | |
def apply(list: Seq[Int]): DiceResult = new DiceResult(list.sum, list) | |
} | |
implicit class Dice (n: Int) { | |
private val r = new Random | |
def d(f: Int): DiceResult = DiceResult((0 to n - 1).map(_ => r.nextInt(f) + 1)) | |
def d(): DiceResult = d(6) | |
} | |
implicit def diceResultToInt(r: DiceResult): Int = r.sum | |
def D66(): Int = { | |
(0 to 1) | |
.map(_ => (1 d 6).sum) | |
.sortWith((a, b) => a < b) | |
.reduce(_ * 10 + _) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import info.nekometer.ukatama.scala.dice._; | |
2 d 6 | |
1 d 100 | |
2 d 6 p 2 | |
2 d 6 b 3 | |
D66 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment