GREEN: store rolls in a list and check for spare
This commit is contained in:
parent
f34c820c65
commit
b6007c8115
1 changed files with 16 additions and 3 deletions
|
|
@ -1,11 +1,24 @@
|
|||
class BowlingGame {
|
||||
int _score = 0;
|
||||
final List<int> _rolls = [];
|
||||
|
||||
void roll(int pins) {
|
||||
_score += pins;
|
||||
_rolls.add(pins);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue