Skip to content
Merged
1 change: 1 addition & 0 deletions includes/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Game {
public:
void roll(int pins);
int score() const;
int frameScore(int frame) const;
};

#endif
33 changes: 33 additions & 0 deletions sources/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
77 changes: 77 additions & 0 deletions tests/test_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading