diff --git a/BowlingGame/src/main/scala/Game.scala b/BowlingGame/src/main/scala/Game.scala new file mode 100644 index 0000000..14fbbf3 --- /dev/null +++ b/BowlingGame/src/main/scala/Game.scala @@ -0,0 +1,39 @@ +class Game { + var rolls: List[Int] = List() + + def roll(pins: Int): Unit = { + if (rolls.length >= 20) { + throw new IllegalArgumentException() + } + rolls = rolls :+ pins + if (pins == 10 && rolls.length % 2 == 1) { + rolls = rolls :+ 0 + } + } + + private def isStrike(index: Int): Boolean = { + if (index <= 2) { + return false + } + + val even = index % 2 == 0 + val shouldFirstRollBeDoubled = even && rolls(index-2) == 10 + val shouldSecondRollBeDoubled = rolls(index-3) == 10 + shouldFirstRollBeDoubled || shouldSecondRollBeDoubled + } + + def score(): Int = { + var res = 0 + for (i <- rolls.indices) { + res += rolls(i) + val even = i % 2 == 0 + if (isStrike(i)) { + res += rolls(i) + } + if (even && i > 0 && rolls(i-1) + rolls(i-2) == 10) { + res += rolls(i) + } + } + res + } +} diff --git a/BowlingGame/src/test/scala/Example.scala b/BowlingGame/src/test/scala/Example.scala deleted file mode 100644 index e289f80..0000000 --- a/BowlingGame/src/test/scala/Example.scala +++ /dev/null @@ -1,11 +0,0 @@ -import org.scalatest._ -import flatspec._ -import matchers._ - -class Example extends AnyFlatSpec with should.Matchers { - - "A List" should "drop the first values" in { - val stack = List(3, 2) - stack.drop(1) should be (List(2)) - } -} diff --git a/BowlingGame/src/test/scala/GameSpec.scala b/BowlingGame/src/test/scala/GameSpec.scala new file mode 100644 index 0000000..8fc1aae --- /dev/null +++ b/BowlingGame/src/test/scala/GameSpec.scala @@ -0,0 +1,80 @@ +import org.scalatest._ +import flatspec._ +import matchers._ + +class GameSpec extends AnyFlatSpec with should.Matchers { + "beginning of game" should "return score of 0 " in { + val game = new Game() + val result = game.score() + result should be(0) + } + + "game after one roll of 1" should "return a score of 1" in { + val game = new Game() + game.roll(1) + + val result = game.score() + result should be(1) + } + + "full game" should "have 10 frames" in { + val game = new Game() + for (i <- 0 until 20) { + game.roll(1); + } + val result = game.score() + result should be(20) + } + + "full game" should "not have more than 10 frames" in { + val game = new Game() + for (i <- 0 until 20) { + game.roll(1); + } + assertThrows[IllegalArgumentException] { + game.roll(1) + } + } + + "a spare" should "compute the score properly" in { + val game = new Game() + game.roll(7) + game.roll(3) + game.roll(4) + + game.score() should be (18) + } + + "a game" should "compute the double the next roll after a spare" in { + val game = new Game() + game.roll(7) + game.roll(3) + game.roll(7) + game.roll(1) + + game.score() should be (25) + } + + "a game" should "compute a strike" in { + val game = new Game() + game.roll(10) + game.roll(3) + game.roll(4) + + game.score() should be (24) + } + + "a game" should "compute a 0-10 spare" in { + val game = new Game() + game.roll(0) + game.roll(10) + game.roll(3) + game.roll(4) + + game.score() should be (20) + } + + + + +}