Skip to content

Instantly share code, notes, and snippets.

@zetashift
Created July 31, 2018 18:42
Show Gist options
  • Select an option

  • Save zetashift/36f971c23486aa972228ab26374eb1f6 to your computer and use it in GitHub Desktop.

Select an option

Save zetashift/36f971c23486aa972228ab26374eb1f6 to your computer and use it in GitHub Desktop.
Daily Programmer #364 [Easy]
def rollDice(n: String): String = {
val input = n.split("d")
val numberOfRolls = input(0).toInt
val numberOfSides = input(1).toInt
var r = scala.util.Random
var rolls = scala.collection.mutable.ArrayBuffer[Int]()
var result = 0
for (n <- (1 to numberOfRolls)) {
val currentRoll = r.nextInt(numberOfSides + 1)
rolls += currentRoll
result = result + currentRoll
}
s"$result: ${rolls.mkString(", ")}"
}
println(rollDice("3d20"))
println(rollDice("6d4"))
println(rollDice("1d2"))
println(rollDice("1d8"))
println(rollDice("100d100"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment