Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions sources/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
}
24 changes: 24 additions & 0 deletions tests/test_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading