Bowling game Kata in Scala - pattern match 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 =&gt; score
      case left :: Nil =&gt; score + left
      case lastRoundStrike :: additional1 :: additional2 :: Nil =&gt; score + lastRoundStrike + additional1 + additional2
      case strike :: next :: nextnext :: _ if strike == 10 =&gt; countHelper(ps.tail, score + strike + next + nextnext)
      case spareL :: spareR :: next :: pss if spareL + spareR == 10 =&gt; countHelper(next :: pss, score + spareL + spareR + next)
      case left :: right :: pss =&gt; countHelper(pss, score + left + right)
    }

    countHelper(points, 0)
  }

}
Some tips:

When pattern matching and inspecting multiple values of a sequence type (Seq, List, Array, etc) you can reduce the length of your expression by using sequence patterns instead of explicitly breaking the sequence up with the :: operator. So instead of a :: b :: c :: d :: Nil you can more concisely use Seq(a, b, c, d).

When you want to check for explicit values while pattern matching on an object you can use those values instead of replacing them with variables that are then checked with an if statement. For example case Seq(a, b, c) if a == 10 =&gt; ...  can be more concisely expressed as case Seq(10, b, c) =&gt; ...

When using pattern matching on sequences (especially when the case expression goes beyond the standard head :: tail) it can enhance legibility to use single letter variable names and comments instead of long variable names. I think that we would both agree that good code should describe itself through both value and function names. However, in this case there is no way of adding intuition with function names and so all of the description ended up in your value names.
Like I said before, do what works for you. If legibility was our only metric then maybe pattern matching isn't the ideal way to implement your function. It could still be recursive, but instead of cases you could use functions. Do whatever suits you the best but when possible I would recommend short variable names accompanied by comments over long variable names.


Some code:
object BowlingGame {
  def count(points: Seq[Int]): Int = {
    def helper(ps: Seq[Int], acc: Int): Int = ps match {
      case Nil =&gt; 
        acc

      case Seq(a) =&gt;      
        acc + a

      // strike or spare in last round
      case xs @ Seq(a, b, c) =&gt;                
        acc + xs.sum

      // strike
      case Seq(10, a, b, _*) =&gt;
        helper(ps.tail, acc + 10 + a + b)

      // spare
      case Seq(a, b, c, _*) if a + b == 10 =&gt;
        helper(ps.drop(2), acc + a + b + c)

      // neither a spare or a strike
      case Seq(a, b, _*) =&gt;
        helper(ps.drop(2), acc + a + b)
    }
    helper(points, 0)
  }
}