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

artemkorsakov's solution

to Bowling in the Scala Track

Published at Jul 04 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)
}
}``````
``````case class Bowling() {
val frames: Array[Frame] = Array.ofDim[Frame](10)
var index: Int = -1
var isFail: Boolean = false

def score(): Either[Boolean, Int] = if (canNotCalculateScore) Left(true) else Right(frames.map(frame => frame.score.right.getOrElse(0)).sum)

def canNotCalculateScore: Boolean = isNotStarted || !isCompletedMainPart || isFail || existFailFrame

def isNotStarted: Boolean = index == -1

def isCompletedMainPart: Boolean = index >= 9 && !frames.last.isOpen

def existFailFrame: Boolean = frames.exists(frame => frame.score.isLeft)

def roll(roll: Int): Bowling = {
if (isFail || isRollInvalid(roll)) {
isFail = true
} else if (isNotStarted) {
createNextFrame(roll)
} else if (isCompletedMainPart && existFailFrame) {
} else if (index <= 9 && frames(index).isOpen) {
} else if (index < 9 && !frames(index).isOpen) {
createNextFrame(roll)
} else {
isFail = true
}
this
}

private def addBonusAfterMainPart(roll: Int): Unit = {
index += 1
val last = frames.last
index match {
case 11 => if (last.isStrike && (last.bonus1.get == 10 || last.bonus1.get + roll <= 10)) last.bonus2 = Some(roll)
case _ => isFail = true
}
}

private def addRollToClosedFrame(roll: Int): Unit = {
if (frames(index - 1).isSpareOrStrike) {
frames(index - 1).bonus1 = Some(roll)
}
if (frames(index - 1).isStrike) {
}
}

private def addBonusToPrevStrike(roll: Int, idx: Int): Unit = if (idx > 0 && frames(idx - 1).isStrike) frames(idx - 1).bonus2 = Some(roll)

private def addRollToOpenFrame(roll: Int): Unit = {
if (frames(index).ball1 + roll > 10) {
isFail = true
} else {
frames(index).ball2 = Some(roll)
}
}

private def createNextFrame(roll: Int): Unit = {
index += 1
frames(index) = Frame(roll)
}

private def isRollInvalid(roll: Int): Boolean = roll < 0 || roll > 10
}

case class Frame(var ball1: Int) {
var ball2: Option[Int] = None
var bonus1: Option[Int] = None
var bonus2: Option[Int] = None

def isOpen: Boolean = ball1 < 10 && ball2.isEmpty

def score: Either[Boolean, Int] = (isStrike, isSpare, ball2.isDefined, bonus1.isDefined, bonus2.isDefined) match {
case (true, false, false, true, true) => Right(10 + bonus1.get + bonus2.get)
case (false, true, true, true, false) => Right(10 + bonus1.get)
case (false, false, true, false, false) => Right(ball1 + ball2.get)
case _ => Left(true)
}

def isSpare: Boolean = (ball1 < 10) && (ball1 + ball2.getOrElse(0) == 10)

def isStrike: Boolean = ball1 == 10

def isSpareOrStrike: Boolean = isSpare || isStrike

}``````