Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/scala/com/github/funprog/funbot/DiceBot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import scala.util.Random

class DiceBot(val random: Random) {
private lazy val regex = """(?i)\s*roll\s+(\d+)([d|n])(\d+)\s*""".r
private lazy val regexLotto = """(?i)\s*roll\s+(\d+)(lotto)\s*""".r
private lazy val regexInteger = """(?i)(\d+)\s*""".r


/** Processes an `input` to return sequential string values
* representing order.
*
Expand All @@ -23,6 +25,7 @@ class DiceBot(val random: Random) {
val result = method.toLowerCase match {
case "d" => rollNormalDice(dice, count)
case "n" => rollNonDupDice(dice, count)
case "lotto" => rollForNewLife(count, List.empty)
}
Some(result.mkString(", "))
} else {
Expand All @@ -31,6 +34,7 @@ class DiceBot(val random: Random) {
}

private def parseInput(input: String): (Boolean, Int, String, String) = input match {
case regexLotto(count, method) => (true, count.toInt, method, "")
case regex(count, method, max) => (true, count.toInt, method, max)
case _ => (false, 0, "", "")
}
Expand All @@ -47,4 +51,14 @@ class DiceBot(val random: Random) {
private def rollNonDupDice(dice: List[String], count: Int): List[String] = {
random.shuffle(dice).take(count)
}
private def rollForNewLife(count: Int, result: List[String]): List[String] = {
if (count == 0) {
result
} else {
val `newLife and bonus` = random.shuffle((1 to 45).toList.map(_.toString)).take(7)
val bonus = `newLife and bonus`.head
val newLifeNumber = `newLife and bonus`.tail.mkString(" ")
rollForNewLife(count - 1, result ::: List(s"\n[$newLifeNumber] and bonus(#$bonus)"))
}
}
}
31 changes: 31 additions & 0 deletions src/test/scala/com/github/funprog/funbot/DiceBotTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.scalatest.FunSuite
import scala.util.Random

class DiceBotTest extends FunSuite {

Seq(
"invalid string",
"",
Expand Down Expand Up @@ -97,4 +98,34 @@ class DiceBotTest extends FunSuite {
// Verify outcome
assert(expected == actual)
}

Seq("roll 1lotto",
"roll 3LOTTO",
"roll 4Lotto",
"roll 2loTTo").foreach(
input => {
test(s"process with '$input' test") {
val dicebotRandom = new DiceBot(new Random)

val actual = dicebotRandom.process(input)

assert(classOf[Some[String]] == actual.getClass)
}
})

Seq("roll lotto",
"roll 3lotto1",
"roll lotto12"
).foreach(
input => {
test(s"process with '$input' test") {
val dicebotRandom = new DiceBot(new Random)

val actual = dicebotRandom.process(input)

assert(actual == None)
}
}
)

}