diff --git a/includes/game.h b/includes/game.h index 6a25937..d0b1992 100644 --- a/includes/game.h +++ b/includes/game.h @@ -10,6 +10,7 @@ class Game { void roll(int pins); int score() const; int frameScore(int frame) const; + int rollScore(int frame, int roll) const; }; #endif \ No newline at end of file diff --git a/sources/game.cpp b/sources/game.cpp index 33f9327..f124cf8 100644 --- a/sources/game.cpp +++ b/sources/game.cpp @@ -80,4 +80,28 @@ int Game::frameScore(int frame) const { } return rolls[rollIndex] + rolls[rollIndex + 1]; +} + +int Game::rollScore(int frame, int roll) const { + int rollIndex = 0; + + for (int f = 1; f < frame; ++f) { + if (rollIndex >= rolls.size()) + return 0; + if (rolls[rollIndex] == 10) + rollIndex += 1; + else + rollIndex += 2; + } + + if (rollIndex >= rolls.size()) + return 0; + + if (roll == 2 && rolls[rollIndex] == 10) + return 0; + + if (rollIndex + (roll - 1) >= rolls.size()) + return 0; + + return rolls[rollIndex + (roll - 1)]; } \ No newline at end of file diff --git a/tests/test_game.cpp b/tests/test_game.cpp index b98c25c..aaef7df 100644 --- a/tests/test_game.cpp +++ b/tests/test_game.cpp @@ -185,4 +185,28 @@ TEST(GameTest, FrameScoreTenthFrameIncomplete) { game.roll(0); game.roll(10); EXPECT_EQ(game.frameScore(10), 0); +} + +TEST(GameTest, SecondRollInFirstFrame) { + Game game; + game.roll(0); + game.roll(5); + for (int i = 0; i < 18; ++i) + game.roll(0); + EXPECT_EQ(game.rollScore(1, 2), 5); +} + +TEST(GameTest, RollScoreWithStrike) { + Game game; + game.roll(0); + game.roll(5); + game.roll(10); + game.roll(5); + game.roll(4); + EXPECT_EQ(game.rollScore(1, 1), 0); + EXPECT_EQ(game.rollScore(1, 2), 5); + EXPECT_EQ(game.rollScore(2, 1), 10); + EXPECT_EQ(game.rollScore(2, 2), 0); + EXPECT_EQ(game.rollScore(3, 1), 5); + EXPECT_EQ(game.rollScore(3, 2), 4); } \ No newline at end of file