diff --git a/includes/game.h b/includes/game.h index a118040..6a25937 100644 --- a/includes/game.h +++ b/includes/game.h @@ -9,6 +9,7 @@ class Game { public: void roll(int pins); int score() const; + int frameScore(int frame) const; }; #endif \ No newline at end of file diff --git a/sources/game.cpp b/sources/game.cpp index a6193a0..33f9327 100644 --- a/sources/game.cpp +++ b/sources/game.cpp @@ -47,4 +47,37 @@ int Game::score() const { } return total; +} + +int Game::frameScore(int frame) 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 (rolls[rollIndex] == 10) { + if (rollIndex + 2 >= rolls.size()) + return 0; + return 10 + rolls[rollIndex + 1] + rolls[rollIndex + 2]; + } + + if (rollIndex + 1 >= rolls.size()) + return 0; + + if (rolls[rollIndex] + rolls[rollIndex + 1] == 10) { + if (rollIndex + 2 >= rolls.size()) + return 0; + return 10 + rolls[rollIndex + 2]; + } + + return rolls[rollIndex] + rolls[rollIndex + 1]; } \ No newline at end of file diff --git a/tests/test_game.cpp b/tests/test_game.cpp index c31db3d..b98c25c 100644 --- a/tests/test_game.cpp +++ b/tests/test_game.cpp @@ -108,4 +108,81 @@ TEST(GameTest, ScoreWithStrike) { EXPECT_EQ(game.score(), 0); game.roll(4); EXPECT_EQ(game.score(), 24); +} + +TEST(GameTest, SecondFrameScore) { + Game game; + game.roll(0); + game.roll(2); + game.roll(3); + game.roll(5); + for (int i = 0; i < 16; ++i) + game.roll(0); + EXPECT_EQ(game.frameScore(2), 8); +} + +TEST(GameTest, FrameScoreWithStrike) { + Game game; + game.roll(10); + game.roll(5); + game.roll(4); + + for (int i = 0; i < 17; ++i) + game.roll(0); + EXPECT_EQ(game.frameScore(1), 19); +} + +TEST(GameTest, FrameScoreWithSpare) { + Game game; + game.roll(6); + game.roll(4); + game.roll(5); + for (int i = 0; i < 17; ++i) + game.roll(0); + EXPECT_EQ(game.frameScore(1), 15); +} + +TEST(GameTest, FrameScoreOfTenthFrameWithStrike) { + Game game; + for (int i = 0; i < 18; ++i) + game.roll(0); + game.roll(10); + game.roll(4); + game.roll(3); + EXPECT_EQ(game.frameScore(10), 17); +} + +TEST(GameTest, FrameScoreOfTenthFrameWithSpare) { + Game game; + for (int i = 0; i < 18; ++i) + game.roll(0); + game.roll(6); + game.roll(4); + game.roll(3); + EXPECT_EQ(game.frameScore(10), 13); +} + +TEST(GameTest, FrameScoreOfTenthFrame) { + Game game; + for (int i = 0; i < 18; ++i) + game.roll(0); + game.roll(4); + game.roll(4); + EXPECT_EQ(game.frameScore(10), 8); +} + +TEST(GameTest, FrameScoreAfterStrike) { + Game game; + game.roll(10); + game.roll(2); + game.roll(3); + EXPECT_EQ(game.frameScore(2), 5); +} + +TEST(GameTest, FrameScoreTenthFrameIncomplete) { + Game game; + for (int i = 0; i < 18; ++i) + game.roll(0); + game.roll(10); + EXPECT_EQ(game.frameScore(10), 0); } \ No newline at end of file