From f92e323ed9a0c2fed6b1ff2e5b1dcb52b47dcc7f Mon Sep 17 00:00:00 2001 From: LEEJeyeon Date: Sun, 7 Aug 2016 17:26:55 +0900 Subject: [PATCH] add lotto bot --- .../com/github/funprog/funbot/DiceBot.scala | 14 +++++++++ .../github/funprog/funbot/DiceBotTest.scala | 31 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/main/scala/com/github/funprog/funbot/DiceBot.scala b/src/main/scala/com/github/funprog/funbot/DiceBot.scala index 105599f..95c2c4d 100644 --- a/src/main/scala/com/github/funprog/funbot/DiceBot.scala +++ b/src/main/scala/com/github/funprog/funbot/DiceBot.scala @@ -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. * @@ -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 { @@ -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, "", "") } @@ -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)")) + } + } } diff --git a/src/test/scala/com/github/funprog/funbot/DiceBotTest.scala b/src/test/scala/com/github/funprog/funbot/DiceBotTest.scala index d54709d..5fee5d3 100644 --- a/src/test/scala/com/github/funprog/funbot/DiceBotTest.scala +++ b/src/test/scala/com/github/funprog/funbot/DiceBotTest.scala @@ -5,6 +5,7 @@ import org.scalatest.FunSuite import scala.util.Random class DiceBotTest extends FunSuite { + Seq( "invalid string", "", @@ -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) + } + } + ) + }