Score calculation has been moved to it's own file

This commit is contained in:
2025-03-18 16:40:31 +01:00
parent e52031d3df
commit 43cd0101f3
6 changed files with 75 additions and 35 deletions
+47
View File
@@ -0,0 +1,47 @@
#include "../Include/ScoreCalculator.h"
ScoreCalculator::ScoreCalculator() {};
ScoreCalculator::~ScoreCalculator() {};
int ScoreCalculator::GetScore(vector<int> rolls) {
// Calculate score
int score = 0;
int frame = 0;
while (true) {
if (rolls[frame] == 10) {
// If we're on our last frame, and roll a 10.
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;
continue;
}
// Spare
if (rolls[frame] + rolls[frame + 1] == 10) {
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
{
score += 10 + rolls[frame + 1];
break;
}
score += 10 + rolls[frame + 2];
frame += 2;
continue;
}
// Open Frame
score += rolls[frame] + rolls[frame + 1];
frame += 2;
if (frame == rolls.size()) {
break;
}
}
return score;
}