Created
July 31, 2018 18:42
-
-
Save zetashift/36f971c23486aa972228ab26374eb1f6 to your computer and use it in GitHub Desktop.
Daily Programmer #364 [Easy]
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
| 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