SPOILER FormatRolls Solution

A little late to the game on this one… I had put this course down while all the updates were taking place. Proud of the brevity to the solution, nonetheless:

public static string FormatRolls (List<int> rolls){
	StringBuilder sb_rolls = new StringBuilder (21);
	for (int i = 0; i < rolls.Count; i++) {
		if (rolls [i] == 0) {
			sb_rolls.Append ("-");
		} else {
			if ((sb_rolls.Length >= 18 && sb_rolls.Length <= 20) && (rolls [i] == 10)) { 						//Strike in last frame
				sb_rolls.Append ("X");
			} else if (sb_rolls.Length % 2 == 0 && rolls [i] == 10) { 											//A Strike
				sb_rolls.Append ("X ");
			} else if ((sb_rolls.Length % 2 != 0 || sb_rolls.Length > 19) && rolls [i] + rolls [i - 1] == 10) {	//A Spare
				sb_rolls.Append ("/"); 
			} else {
				sb_rolls.Append (rolls [i].ToString ());
			}
		}
	}
	return sb_rolls.ToString ();
}

And I had to scour the internet to double check that my understanding of 10th frame scoring was correct since it didn’t pass the Golden Tests provided. Spare on the last roll is marked as a “/”

Privacy & Terms