Files
Bowling/src/ScoreCalculator.cpp
T
2025-03-19 11:46:19 +01:00

46 lines
1.3 KiB
C++

#include "../include/ScoreCalculator.h"
int ScoreCalculator::getScore(vector<int> rolls) {
int score = 0;
int frame = 0;
while (true) {
// Strike
if (rolls[frame] == 10) {
// If we're on our last frame, and roll a strike, we're given two bonus rolls.
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
score += 10 + rolls[frame + 1] + rolls[frame + 2];
break;
}
score += 10 + rolls[frame + 1] + rolls[frame + 2];
frame += 1;
}
// Spare
else if (rolls[frame] + rolls[frame + 1] == 10) {
// If we're on our last frame, and roll a spare, we're given a bonus roll.
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
{
score += 10 + rolls[frame + 1];
break;
}
score += 10 + rolls[frame + 2];
frame += 2;
}
// Open Frame
else {
score += rolls[frame] + rolls[frame + 1];
frame += 2;
}
// No more frames
if (frame == rolls.size()) {
break;
}
}
return score;
}