Spoiler - Alternative ScoreFrames Solution

Hey, just wanted to share an alternative solution for calculating the frame scores. This method handles the 21st frame implicitly. It identifies if the current bowl is the first or second bowl of the frame and only calculates strikes on first bowls. Anything after that last strike is implicitly ignored either because the frames list is at 10 or because there aren’t enough frames to complete a strike/spare.

Here’s the code:

public static List<int> ScoreFrames(List<int> rolls)
{
	List<int> frames = new List<int>();
	bool firstBowl = true; // Identifies if we're working with the first or second bowl of a frame
		for(int i=0; i<rolls.Count; i++)
		{
			if(firstBowl)
			{
				if(rolls[i]>=10)					// Bowled a strike
				{
					if(rolls.Count >= i+3) 			// Check that the next two bowls are available before trying to use them
					{								// Uses i+3 because i starts at 0, but Count starts at 1
						frames.Add(rolls[i] + rolls[i+1] + rolls[i+2]);
					}
				} else
				{
					firstBowl = false; 				// No actions needed on first bowl unless it's a strike
				}
			} else
			{
				firstBowl = true; 					// Next bowl will always be first of a frame
				if(rolls[i]+rolls[i-1] >= 10) 		// Bowled a spare
				{
					if(rolls.Count >= i+2) 			// Check that the next bowl is available before trying to use it
					{								// Uses i+2 because i starts at 0, but Count starts at 1
						frames.Add(rolls[i] + rolls[i-1] + rolls[i+1]);
					}
				} else
				{
					if(frames.Count < 10) 			// If we already have 10 frames, then we're done
					{
						frames.Add(rolls[i] + rolls[i-1]);
					}
				}
			}
		}

		return frames;
	}

Privacy & Terms