From b6007c8115ab4a546ca638cd5ccb07dba9ba8376 Mon Sep 17 00:00:00 2001 From: fiatcode Date: Tue, 10 Feb 2026 11:52:49 +0700 Subject: [PATCH] GREEN: store rolls in a list and check for spare --- lib/bowling_game.dart | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/bowling_game.dart b/lib/bowling_game.dart index b4983c5..e58e92b 100644 --- a/lib/bowling_game.dart +++ b/lib/bowling_game.dart @@ -1,11 +1,24 @@ class BowlingGame { - int _score = 0; + final List _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; } }