Hi everyone,
first of I have to say that it was interesting to see so different (and yet working) approaches to this problem.
Here is my version:
public static List ScoreFrames (List rolls)
{
List<int> frameList = new List<int> ();
int tmpScore = 0, strikeOffsetIndex = 0;
bool isStrike, fullFrame = false;
for (int i = 0; i < rolls.Count; i++) {
tmpScore += rolls [i]; //sum roll score to temp score
isStrike = rolls [i] == 10 && (i + 1 + strikeOffsetIndex) % 2 != 0; //10 points on first bowl are a strike otherwise it's a spare
strikeOffsetIndex += isStrike ? 1 : 0; // it corrects the bowl counter so each turn has ever 2 launch
fullFrame = (i + 1 + strikeOffsetIndex) % 2 == 0; //each 2 launch you have a turn thanks to the correction given by the strike offset
//check exit condition: if all turn are played or in case of spare or strike you have enough data available to sum up otherwise you can exit
if (frameList.Count == 10 || (isStrike && !(rolls.Count >= i + 3)) || (tmpScore == 10 && !(rolls.Count >= i + 2)))
break;
//in case of fullFrame or strike we have to count the score
if (fullFrame || isStrike) {
//easy... save the score and if its a spare (or a strike) we always have to add the first subsequent roll, if its a strike we add also the second
frameList.Add (tmpScore + (tmpScore == 10 ? rolls [i + 1] : 0) + (isStrike ? rolls [i + 2] : 0));
tmpScore = 0; //reset the tmpScore
}
}
return frameList;
}
Share some comments.
Thanks.
AB