This is a functional approach to problem described by standard Bowling Game Kata - to implement the rules of calculating a score in a bowling game. The inspiration was the implementation by Uncle Bob: http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata
The game consists of 10 frames as shown above. In each frame the player has two opportunities to knock down 10 pins. The score for the frame is the total number of pins knocked down, plus bonuses for strikes and spares.
A spare is when the player knocks down all 10 pins in two tries. The bonus for that frame is the number of pins knocked down by the next roll.
A strike is when the player knocks down all 10 pins on his first try. The bonus for that frame is the value of the next two balls rolled.
In the tenth frame a player who rolls a spare or strike is allowed to roll the extra balls to complete the frame. However no more than three balls can be rolled in tenth frame.
Any feedback of my pattern-match-with-recursion implementation f bowling game kata will be greatly appreciated:
import org.scalatest.{FlatSpec, Matchers}
class BowlingGameTest extends FlatSpec with Matchers {
"sanity test" should "pass" in {
true shouldBe true
}
"gutter game" should "have no points" in {
val score = BowlingGame.count(Seq.fill(20)(0))
score shouldBe 0
}
"all ones" should "return sum of points" in {
val score = BowlingGame.count(Seq.fill(20)(1))
score shouldBe 20
}
"spare" should "get bonus from next throw" in {
val points: Seq[Int] = Seq(5, 5, 8, 0) ++ Seq.fill(16)(0)
val score = BowlingGame.count(points)
score shouldBe 26
}
"multiple spares" should "get bonus from each next throw" in {
val points: Seq[Int] = Seq(5, 5, 8, 2, 7) ++ Seq.fill(15)(0)
val score = BowlingGame.count(points)
score shouldBe 42
}
"strike" should "get bonus points from two next throws" in {
val points: Seq[Int] = Seq(10, 5, 2, 0) ++ Seq.fill(16)(0)
val score = BowlingGame.count(points)
score shouldBe 24
}
"multiple strikes" should "get bonus points from two next throws" in {
val points: Seq[Int] = Seq(10, 10, 5, 2, 0) ++ Seq.fill(15)(0)
val score = BowlingGame.count(points)
score shouldBe 49
}
"spare in last round" should "get bonus points from a single additional throw" in {
val points: Seq[Int] = Seq.fill(18)(0) ++ Seq(5, 5, 8)
val score = BowlingGame.count(points)
score shouldBe 18
}
"strike in last round" should "get bonus points from two additional throws" in {
val points: Seq[Int] = Seq.fill(18)(0) ++ Seq(10, 5, 3)
val score = BowlingGame.count(points)
score shouldBe 18
}
"strike in last round and in first additional throw" should "get no bonus points from additional throws" in {
val points: Seq[Int] = Seq.fill(18)(0) ++ Seq(10, 10, 3)
val score = BowlingGame.count(points)
score shouldBe 23
}
"maximum game" should "get bonuses in each round" in {
val score= BowlingGame.count(Seq.fill(12)(10))
score shouldBe 300
}
"partial game" should "return score so far" in {
BowlingGame.count(Seq(1,2,3)) shouldBe 6
}
}
object BowlingGame {
def count(points: Seq[Int]): Int = {
def countHelper(ps: Seq[Int], score: Int): Int = ps match {
case Nil => score
case left :: Nil => score + left
case lastRoundStrike :: additional1 :: additional2 :: Nil => score + lastRoundStrike + additional1 + additional2
case strike :: next :: nextnext :: _ if strike == 10 => countHelper(ps.tail, score + strike + next + nextnext)
case spareL :: spareR :: next :: pss if spareL + spareR == 10 => countHelper(next :: pss, score + spareL + spareR + next)
case left :: right :: pss => countHelper(pss, score + left + right)
}
countHelper(points, 0)
}
}