Sorry but my coding wins are rare enough that I have to brag about them!
I found it easier to approach the problem head on as opposed to test by test, though was checking that the expected tests would pass as I added logic for the various conditions. Here’s what I came up with:
// returns a list of individual frame scores
public static List<int> ScoreFrames(List<int> rolls)
{
List<int> frameScores = new List<int>();
bool newFrame = true;
for (int i = 0; i < rolls.Count; i++ )
{
if(newFrame && rolls[i] == 10) // Strike!
{
if(rolls.Count > i + 2) // If we have the next 2 rolls completed, add them as bonuses and complete score for frame (10 for strike plus scores for next frame)
{
frameScores.Add(10 + rolls[i + 1] + rolls[i + 2]);
}
} else {
if(!newFrame && frameScores.Count < 10) // find score for frame, checking that we're not in the end of game bonus section
{
int frameScore = rolls[i] + rolls[i - 1]; // frame complete, take score from this bowl and previous
if (frameScore == 10 && rolls.Count > i + 1) frameScore += rolls[i + 1]; // if we have a spare and next roll is available, add next roll
if (frameScore != 10 || rolls.Count > i + 1) frameScores.Add(frameScore); // add frame score, unless it's a spare and next row not yet available
}
newFrame = !newFrame; // toggle whether it's a new frame, not done if strike as next will be new
}
}
return frameScores;
}