ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# joe-warren-roo's solution

## to Bowling in the Scala Track

Published at Aug 06 2020 · 1 comment
Instructions
Test suite
Solution

Score a bowling game.

Bowling is a game where players roll a heavy ball to knock down pins arranged in a triangle. Write code to keep track of the score of a game of bowling.

## Scoring Bowling

The game consists of 10 frames. A frame is composed of one or two ball throws with 10 pins standing at frame initialization. There are three cases for the tabulation of a frame.

• An open frame is where a score of less than 10 is recorded for the frame. In this case the score for the frame is the number of pins knocked down.

• A spare is where all ten pins are knocked down by the second throw. The total value of a spare is 10 plus the number of pins knocked down in their next throw.

• A strike is where all ten pins are knocked down by the first throw. The total value of a strike is 10 plus the number of pins knocked down in the next two throws. If a strike is immediately followed by a second strike, then the value of the first strike cannot be determined until the ball is thrown one more time.

Here is a three frame example:

Frame 1 Frame 2 Frame 3
X (strike) 5/ (spare) 9 0 (open frame)

Frame 1 is (10 + 5 + 5) = 20

Frame 2 is (5 + 5 + 9) = 19

Frame 3 is (9 + 0) = 9

This means the current running total is 48.

The tenth frame in the game is a special case. If someone throws a strike or a spare then they get a fill ball. Fill balls exist to calculate the total of the 10th frame. Scoring a strike or spare on the fill ball does not give the player more fill balls. The total value of the 10th frame is the total number of pins knocked down.

For a tenth frame of X1/ (strike and a spare), the total value is 20.

For a tenth frame of XXX (three strikes), the total value is 30.

## Requirements

Write code to keep track of the score of a game of bowling. It should support two operations:

• `roll(pins : int)` is called each time the player rolls a ball. The argument is the number of pins knocked down.
• `score() : int` is called only at the very end of the game. It returns the total score for that game.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

Please see the learning and installation pages if you need any help.

## Source

The Bowling Game Kata at but UncleBob http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### BowlingSuite.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.0.1 */
class BowlingTest extends FunSuite with Matchers {

test("should be able to score a game with all zeros") {
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(0))
}

test("should be able to score a game with no strikes or spares") {
pending
val score = List(3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(90))
}

test("a spare followed by zeros is worth ten points") {
pending
val score = List(6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(10))
}

test("points scored in the roll after a spare are counted twice") {
pending
val score = List(6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(16))
}

test("consecutive spares each get a one roll bonus") {
pending
val score = List(5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(31))
}

test("a spare in the last frame gets a one roll bonus that is counted once") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(17))
}

test("a strike earns ten points in a frame with a single roll") {
pending
val score = List(10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(10))
}

test("points scored in the two rolls after a strike are counted twice as a bonus") {
pending
val score = List(10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(26))
}

test("consecutive strikes each get the two roll bonus") {
pending
val score = List(10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(81))
}

test("a strike in the last frame gets a two roll bonus that is counted once") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(18))
}

test("rolling a spare with the two roll bonus does not get a bonus roll") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(20))
}

test("strikes with the two roll bonus do not get bonus rolls") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(30))
}

test("a strike with the one roll bonus after a spare in the last frame does not get a bonus") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(20))
}

test("all strikes is a perfect game") {
pending
val score = List(10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(300))
}

test("rolls cannot score negative points") {
pending
val score = List().foldLeft(Bowling())((acc, roll) => acc.roll(roll)).roll(-1).score()
score.isLeft should be (true)
}

test("a roll cannot score more than 10 points") {
pending
val score = List().foldLeft(Bowling())((acc, roll) => acc.roll(roll)).roll(11).score()
score.isLeft should be (true)
}

test("two rolls in a frame cannot score more than 10 points") {
pending
val score = List(5).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).roll(6).score()
score.isLeft should be (true)
}

test("bonus roll after a strike in the last frame cannot score more than 10 points") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).roll(11).score()
score.isLeft should be (true)
}

test("two bonus rolls after a strike in the last frame cannot score more than 10 points") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).roll(6).score()
score.isLeft should be (true)
}

test("two bonus rolls after a strike in the last frame can score more than 10 points if one is a strike") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score should be (Right(26))
}

test("the second bonus rolls after a strike in the last frame cannot be a strike if the first one is not a strike") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).roll(10).score()
score.isLeft should be (true)
}

test("second bonus roll after a strike in the last frame cannot score more than 10 points") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).roll(11).score()
score.isLeft should be (true)
}

test("an unstarted game cannot be scored") {
pending
val score = List().foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score.isLeft should be (true)
}

test("an incomplete game cannot be scored") {
pending
val score = List(0, 0).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score.isLeft should be (true)
}

test("cannot roll if game already has ten frames") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).roll(0).score()
score.isLeft should be (true)
}

test("bonus rolls for a strike in the last frame must be rolled before score can be calculated") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score.isLeft should be (true)
}

test("both bonus rolls for a strike in the last frame must be rolled before score can be calculated") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score.isLeft should be (true)
}

test("bonus roll for a spare in the last frame must be rolled before score can be calculated") {
pending
val score = List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3).foldLeft(Bowling())((acc, roll) => acc.roll(roll)).score()
score.isLeft should be (true)
}
}``````
``````case class Bowling(rolls: List[Int]) {
private def lastFrame(rolls: List[Int]): Either[String, Int] =
rolls match {
// two or more strikes
case List(10, 10, _) => Right(rolls.sum)
// one strike
case List(10, x, y) if x + y <= 10 => Right(rolls.sum)
// a spair
case List(x, y, z) if x + y == 10 => Right(rolls.sum)
// a regular last frame
case List(x, y) if x != 10 && x + y < 10 => Right(rolls.sum)
case _                                   => Left("something went wrong")
}

private def regularFrame(
rolls: List[Int],
framesLeft: Int
): Either[String, Int] =
for {
(frameScore, nextRolls) <- rolls match {
// a strike
case (10 :: (next @ (n1 :: n2 :: _))) => Right((10 + n1 + n2, next))
// a spare
case (a :: b :: (next @ (n1 :: _))) if a + b == 10 =>
Right((10 + n1, next))
// a regular frame
case (a :: b :: next) if a + b < 10 => Right((a + b, next))
// an none of the above
case _ => Left("something went wrong")
}
nextScore <- incrementalScore(nextRolls, framesLeft - 1)
} yield nextScore + frameScore

private def incrementalScore(
rolls: List[Int],
framesLeft: Int
): Either[String, Int] =
if (framesLeft == 0) {
lastFrame(rolls)
} else {
regularFrame(rolls, framesLeft)
}
private def validateRange(rolls: List[Int]): Either[String, List[Int]] =
if (rolls.forall(0 to 10 contains _)) {
Right(rolls)
} else {
Left("roll out of range")
}

def score(): Either[String, Int] =
validateRange(rolls).flatMap(rolls => incrementalScore(rolls.reverse, 9))
def roll(pins: Int): Bowling = Bowling(pins :: rolls)
}

object Bowling {
def apply(): Bowling = Bowling(List.empty)
}``````

Solution Author
commented 47 days ago

This feels like I've overcomplicated it, but on the other hand, I'm currently stumped for "better ways to write this

In a nutshell, this folds forward over the rolls, to produce a "list" of frames, and then "folds" (recurses) backwards over the frames, to produce the score.

At the time of writing, this feels like "inherent" rather than "accidental" complexity.

With that said, I worry that I'm about to read other solutions to this, and then come to the conclusion that my code is bad.

Edit, I'm now much happier with this solution, it's a shame exercism doesn't let you publish multiple versions though

(edited 40 days ago)

### What can you learn from this solution?

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?