Got it in less than 30 minutes and 9 lines of code (my solution included)

Here it is. I have to say, I didn’t do the normal test+code+refactor, as I should. I just thought for a while how to approach, and start already writing the code thinking how would affect special cases.
Well, it worked. Here’s my code condensed in 9 lines:

bool isSecondBall = false;
for (int roll =0; roll < rolls.Count;roll++){
if (!isSecondBall && rolls[roll] >= 10 && rolls.Count >= roll + 3){
framelist.Add(rolls[roll] + rolls[roll + 1] + rolls[roll + 2]);}
else if (isSecondBall && (rolls[roll] + rolls[roll - 1] >= 10) && rolls.Count >= roll + 2){
framelist.Add(rolls[roll] + rolls[roll + 1] + rolls[roll - 1]); isSecondBall = !isSecondBall;}
else if (isSecondBall && (rolls[roll] + rolls[roll - 1] < 10)){
framelist.Add(rolls[roll] + rolls[roll - 1]); isSecondBall = !isSecondBall;}
else if (!isSecondBall && framelist.Count < 10) { isSecondBall = true; } ;}
return framelist;}

It was a nice challenge, thanks Ben!

1 Like

Privacy & Terms