diff --git a/sources/game.cpp b/sources/game.cpp index 3badeeb..0ee7b67 100644 --- a/sources/game.cpp +++ b/sources/game.cpp @@ -3,10 +3,16 @@ void Game::roll(int pins) { rolls.push_back(pins); }; int Game::score() const { - int total; - - for (int roll : rolls) { - total += roll; + int total = 0; + int rollIndex = 0; + for (int frame = 0; frame < 10; ++frame) { + if (rolls[rollIndex] == 10) { + total += 10 + rolls[rollIndex + 1] + rolls[rollIndex + 2]; + rollIndex++; + } else { + total += rolls[rollIndex] + rolls[rollIndex + 1]; + rollIndex += 2; + } } return total; diff --git a/tests/test_game.cpp b/tests/test_game.cpp index 933c058..e532da8 100644 --- a/tests/test_game.cpp +++ b/tests/test_game.cpp @@ -13,4 +13,14 @@ TEST(GameTest, AllOnes) { for (int i = 0; i < 20; i++) game.roll(1); EXPECT_EQ(game.score(), 20); +} + +TEST(GameTest, StrikeForFirstThrowInFrame) { + Game game; + game.roll(10); + game.roll(4); + game.roll(2); + for (int i = 0; i < 16; i++) + game.roll(0); + EXPECT_EQ(game.score(), 10 + 4 + 2 + 4 + 2); } \ No newline at end of file