Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dugdaniels/4956b84173d42655c10531b77b3060e9 to your computer and use it in GitHub Desktop.
Save dugdaniels/4956b84173d42655c10531b77b3060e9 to your computer and use it in GitHub Desktop.
Bowling scoring logic
public static List<int> ScoreFrames(List<int> rolls) {
List<int> frameList = new List<int>();
int rollNumber = 0;
int currentFrame = 0;
int rollsRemainingInFrame = 2;
foreach (int roll in rolls) if (frameList.Count < 10) {
rollNumber++;
rollsRemainingInFrame--;
currentFrame += roll;
if (currentFrame == 10 && rollsRemainingInFrame >= 1) {
if (rolls.Count >= rollNumber + 2) {
currentFrame += rolls[rollNumber] + rolls[rollNumber + 1];
frameList.Add(currentFrame);
}
currentFrame = 0;
rollsRemainingInFrame = 2;
}
if (currentFrame == 10 && rollsRemainingInFrame == 0) {
if (rolls.Count >= rollNumber + 1) {
currentFrame += rolls[rollNumber];
frameList.Add(currentFrame);
}
currentFrame = 0;
rollsRemainingInFrame = 2;
}
if (rollsRemainingInFrame == 0) {
frameList.Add(currentFrame);
currentFrame = 0;
rollsRemainingInFrame = 2;
}
}
return frameList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment