GREEN: store rolls in a list and check for spare

This commit is contained in:
fiatcode 2026-02-10 11:52:49 +07:00
parent f34c820c65
commit b6007c8115

View file

@ -1,11 +1,24 @@
class BowlingGame { class BowlingGame {
int _score = 0; final List<int> _rolls = [];
void roll(int pins) { void roll(int pins) {
_score += pins; _rolls.add(pins);
} }
int score() { int score() {
return _score; int totalScore = 0;
int rollIndex = 0;
for (int frame = 0; frame < 10; frame++) {
if (_rolls[rollIndex] + _rolls[rollIndex + 1] == 10) {
totalScore += 10 + _rolls[rollIndex + 2];
rollIndex += 2;
} else {
totalScore += _rolls[rollIndex] + _rolls[rollIndex + 1];
rollIndex += 2;
}
}
return totalScore;
} }
} }