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

# sir-lazlo's solution

## to Bowling in the Scala Track

Published at Aug 10 2020 · 0 comments
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)
}
}``````
``````package bowling

import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.util.Try

case class Bowling() {
private val MAX_SCORE = 10
private val MAX_FRAMES = 10

private val rolls = mutable.Queue[Int]()

def roll(pins: Int): Bowling = {
rolls.enqueue(pins)
this
}

def score(): Either[Boolean, Int] = {
val frames: List[Frame] = Try(createFrameList()).getOrElse(List())
if (frames.size != MAX_FRAMES || frames.exists(!_.isLegal))
return Left(true)

Right(calculateScore(frames))
}

private def createFrameList(): List[Frame] = {
val framesListBuffer = ListBuffer.empty[Frame]

while (rolls.nonEmpty) {
if (framesListBuffer.size == MAX_FRAMES - 1) {
framesListBuffer += new FinalFrame()
}
else if (framesListBuffer.nonEmpty && framesListBuffer.last.isInstanceOf[FinalFrame]) {
framesListBuffer.last.asInstanceOf[FinalFrame].frames += createFrame()
} else {
framesListBuffer += createFrame()
}
}
framesListBuffer.toList
}

private def createFrame(): Frame = {
rolls.dequeue() match {
case MAX_SCORE => new StrikeFrame()
case x if rolls.isEmpty => new FillBall(x)
case x if x + rolls.front == MAX_SCORE => new SpareFrame(x, rolls.dequeue())
case x => new OpenFrame(x, rolls.dequeue())
}
}

private def calculateScore(frames: List[Frame]): Int = {
var score = 0

frames.zipWithIndex.foreach { case (frame: Frame, index: Int) =>
score += frame.score()

frame match {
case _: StrikeFrame =>
val next2Rolls = frames.slice(index + 1, index + 3).flatMap {
_.rolls
}.take(2)
score += next2Rolls.sum
case _: SpareFrame => score += frames(index + 1).rolls.head
case _ => Unit
}
}
score
}

class StrikeFrame extends Frame {
override def rolls = List(MAX_SCORE)

override def isLegal: Boolean = true
}

class SpareFrame(firstThrow: Int, secondThrow: Int) extends Frame {
override def rolls = List(firstThrow, secondThrow)
}

class OpenFrame(firstThrow: Int, secondThrow: Int) extends Frame {
override def rolls = List(firstThrow, secondThrow)
}

class FillBall(firstThrow: Int) extends Frame {
override def rolls = List(firstThrow)
}

class FinalFrame() extends Frame {

val frames: ListBuffer[Frame] = ListBuffer.empty[Frame]

override def score(): Int = frames.map(_.score()).sum

override def rolls: List[Int] = frames.toList.flatMap(_.rolls)

override def isLegal: Boolean = {
val correctCombinations = frames.size match {
case 2 => frames.head match {
case _: SpareFrame => frames.last.isInstanceOf[FillBall] || frames.last.isInstanceOf[StrikeFrame]
case _: StrikeFrame => frames.last.isInstanceOf[OpenFrame] || frames.last.isInstanceOf[SpareFrame]
case _ => return false
}
case 3 => frames.forall {
_.isInstanceOf[StrikeFrame]
} || frames.count {
_.isInstanceOf[StrikeFrame]
} == 2 && frames.last.isInstanceOf[FillBall]
}
correctCombinations && frames.forall(_.isLegal)
}

}

sealed trait Frame {
def rolls: List[Int]

def score(): Int = rolls.sum

def isLegal: Boolean = checkLegality(rolls)

def checkLegality(rolls: List[Int]): Boolean = {
val legalRange = 0 to MAX_SCORE

if (!rolls.forall(legalRange.contains)) return false
if (score > MAX_SCORE) return false
true
}

}

}``````

### 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?