Spoiler - Concise Scoring for BowlMaster

I tried to go to instacod.es but I kept getting 503 errors, so I’m just pasting the code in here.

I got the code down to 14 lines, but I think it’s bordering on being a bit “too clever”.

It relies on the fact that if either a strike ~OR~ a spare is rolled for a frame, you sum up 3 balls:
[index]+ [index+1] + [index+2]
And a normal frame is just the sum of 2 balls
[index]+ [index+1]

const int spare = 10; // I like to name my magic numbers
const int strike = 10; 
const in maxFrames = 10;
public static List<int> ScoreFrames (List<int> rolls) {
    List<int> frames = new List<int>();
    int index = 0;
    while (index + 1 < rolls.Count) { // always need current and next roll to score
        if (frames.Count >= maxFrames) { break; }
        int a = rolls[index];
        int b = rolls[index+1];
        if (a + b >= spare) { 
            if (index+2 >= rolls.Count) { break; } // can't get 3rd roll
            int c = rolls[index+2];
            frames.Add(a + b + c); // Spare ~OR~ Strike sums 3 balls
        } else { 
            frames.Add(a + b); // Normal Frame sums 2 balls
        }
        index += (a == strike) ? 1 : 2;
    }
    return frames;
}
3 Likes