Last active
December 13, 2015 21:38
-
-
Save ybonnel/4978115 to your computer and use it in GitHub Desktop.
Calcul de score de bowling
This file contains hidden or 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 fr.ybo.battle | |
| object Exercice3 { | |
| def calculate(feuille:String):Int = { | |
| // decomposition en frames | |
| var countFrame = 0 | |
| var inFrame = false | |
| feuille.foreach( oneCoup => { | |
| oneCoup match { | |
| case 'X' => { | |
| countFrame+=1 | |
| inFrame=false | |
| } | |
| case _ => { | |
| if (inFrame) { | |
| inFrame = false | |
| countFrame += 1 | |
| } else { | |
| inFrame = true | |
| } | |
| } | |
| } | |
| }) | |
| if (inFrame) { | |
| countFrame += 1 | |
| } | |
| val moreThan10Frames = countFrame > 10 | |
| ("00" + feuille).sliding(3).zipWithIndex.map( | |
| oneCoup => { | |
| val lastCoup = oneCoup._1.charAt(2) | |
| val score = (lastCoup match { | |
| case 'X' => 10 | |
| case '/' => 10 - oneCoup._1.charAt(1).toString.toInt | |
| case '-' => 0 | |
| case _ => lastCoup.toString.toInt | |
| }) | |
| var multiple = 1 | |
| if (oneCoup._1.charAt(0) == 'X' && (oneCoup._2 < feuille.length - 1 || !moreThan10Frames)) { | |
| multiple += 1 | |
| } | |
| if ((oneCoup._1.charAt(1) == '/' || oneCoup._1.charAt(1) == 'X') && (oneCoup._2 < feuille.length - 2 || !moreThan10Frames)) { | |
| multiple += 1 | |
| } | |
| score * multiple | |
| } | |
| ).sum | |
| } | |
| def main(args:Array[String]) { | |
| println(calculate("12121212121212121212")) | |
| println(calculate("------------------32")) | |
| println(calculate("XXXXXXXXXXXX")) | |
| println(calculate("5/5/5/5/5/5/5/5/5/5/5")) | |
| println(calculate("-----------------X52")) | |
| println(calculate("XXX728/17X7/9-XX8")) | |
| println(calculate("XXX728/17X7/9-X2/")) | |
| println(calculate("2/2-5/6/-99-X----XX1")) | |
| println(calculate("122/X12121212123--3")) | |
| } | |
| } |
This file contains hidden or 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
| 30 | |
| 5 | |
| 300 | |
| 150 | |
| 24 | |
| 180 | |
| 172 | |
| 89 | |
| 57 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment