43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
#include "../include/ScoreCalculator.h"
|
|
#include "../include/Frame.h"
|
|
|
|
int ScoreCalculator::getScore(vector<Frame> frames) {
|
|
int score = 0;
|
|
|
|
// There can only be 10 frames.
|
|
for (int i = 0; i < 10; i++) {
|
|
// Strike
|
|
if (frames[i].Roll[0] == 10) {
|
|
if (frames[i].Roll.size() == 3) {
|
|
score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2];
|
|
continue;
|
|
}
|
|
|
|
// If the next frame has 3 or 2 rolls, then take those 2.
|
|
if (frames[i + 1].Roll.size() > 1) {
|
|
score += frames[i].Roll[0] + frames[i + 1].Roll[0] + frames[i + 1].Roll[1];
|
|
continue;
|
|
}
|
|
|
|
// Both next frames, that are not the last, are 10.
|
|
score += frames[i].Roll[0] + frames[i + 1].Roll[0] + frames[i + 2].Roll[0];
|
|
continue;
|
|
}
|
|
|
|
// Spare
|
|
if (frames[i].Roll[0] + frames[i].Roll[1] == 10) {
|
|
if (frames[i].Roll.size() == 3) {
|
|
score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2];
|
|
continue;
|
|
}
|
|
|
|
score += 10 + frames[i + 1].Roll[0];
|
|
continue;
|
|
}
|
|
|
|
// Open frame
|
|
score += frames[i].Roll[0] + frames[i].Roll[1];
|
|
}
|
|
|
|
return score;
|
|
} |