Created
July 26, 2015 18:25
-
-
Save AlvaroCaste/f2faf701c4b5b1632c68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
case Seq(xs @ _*) // Identifier xs is bound to the whole matched sequence