Scala Tutorial Through Katas: Bowling Game (Medium)

A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.

This article is part of the series "Scala Tutorial Through Katas". Articles are divided into easy, medium and hard. Beginners should start with easy ones and move towards more complicated once they feel more comfortable programming in Scala.

For the complete list of Scala katas and solutions please visit the index page

The article assumes that the reader is familiar with the basic usage of ScalaTest asserts and knows how to run them from his favorite Scala IDE (ours is IntelliJ IDEA with the Scala plugin).

Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to write the implementation for the first test, confirm that it passes and move to the next. Once all of the tests pass, the kata can be considered solved.

One possible solution is provided below the tests. Try to solve the kata by yourself first.

Bowling Game

Count and sum the scores of a bowling game of one player.

Original description by Martin Fowler: http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata
Wikipedia article with scoring rules: http://en.wikipedia.org/wiki/Ten-pin_bowling#Scoring

Following is the BDD scenario that should be used as the acceptance criteria.

[BDD SCENARIO]

Following unit tests were used in a TDD fashion while developing the solution.

[UNIT TESTS]

Test code can be found in the GitHub BowlingGame.scala.

[ONE POSSIBLE SOLUTION]

The solution code can be found in BowlingGame.scala solution.

What was your solution? Post it as a comment so that we can compare different ways to solve this kata.

1 thought on “Scala Tutorial Through Katas: Bowling Game (Medium)

  1. paul kinsky (@voidfraction)

    I’ll solve this later, but one thing to be aware of is that List[T] is a linked list, and therefore doesn’t support random access.

    So these three lines contain three O(n) operation.
    if (frames.size > 0 && !frames.last.frameFinished) {
    frames.last.roll2 = pins
    frames.last.frameFinished = true

    if you instead use lastOption you can avoid the if statement entirely, and reproduce this logic with a single traversal of the list.

    Reply

Leave a Reply