Skip to content

Instantly share code, notes, and snippets.

@AlvaroCaste
Created July 26, 2015 18:25
Show Gist options
  • Save AlvaroCaste/f2faf701c4b5b1632c68 to your computer and use it in GitHub Desktop.
Save AlvaroCaste/f2faf701c4b5b1632c68 to your computer and use it in GitHub Desktop.
package bowling
object Game {
val TotalPins = 10
val TotalFrames = 10
def scoreFor(line: Line): Int = sum(line.rolls, TotalFrames)
private def sum(rolls: Seq[Int], remainingFrames: Int): Int =
if (remainingFrames == 0) 0
else rolls match {
case Seq(10, rest @_*) => 10 + strikeBonus(rest) + sum(rest, remainingFrames - 1)
case Seq(first, second, rest @_*) if isSpare(first, second) =>
first + second + spareBonus(rest) + sum(rest, remainingFrames - 1)
case Seq(first, second, rest @_*) =>
first + second + sum(rest, remainingFrames - 1)
}
private def isSpare(first: Int, second: Int): Boolean = {
first + second == TotalPins
}
private def spareBonus(rest: Seq[Int]): Int = rest.head
private def strikeBonus(nextRolls: Seq[Int]): Int = nextRolls.take(2).sum
}
@AlvaroCaste
Copy link
Author

case Seq(xs @ _*) // Identifier xs is bound to the whole matched sequence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment