/** Scorer applies the scoring rules to compute a score */ public class Scorer { private int ball; private int[] rolls = new int[21]; private int currentRoll = 0; /** Add one roll to our collection */ public void addRoll(int pins) { rolls[currentRoll++] = pins; } /** Compute the score for a given frame (cumulative) */ public int scoreForFrame(int theFrame) { ball = 0; int score=0; // Iterate from the start to the given frame for (int currentFrame = 0; currentFrame < theFrame; currentFrame++) { if (strike()) { score += 10 + nextTwoBalls(); } else if (spare()) { score += 10 + nextBall(); } else { score += twoBallsInFrame(); } } return score; } private boolean strike() { if (rolls[ball] == 10) { ball++; return true; } return false; } private boolean spare() { if ((rolls[ball] + rolls[ball+1]) == 10) { ball += 2; return true; } return false; } private int nextTwoBalls() { return rolls[ball] + rolls[ball+1]; } private int nextBall() { return rolls[ball]; } private int twoBallsInFrame() { return rolls[ball++] + rolls[ball++]; } }