Initial implementation of calculation of the score

This commit is contained in:
Rasmus Rasmussen 2025-03-18 15:39:43 +01:00
parent b99b13383f
commit e52031d3df
3 changed files with 43 additions and 8 deletions

View File

@ -1 +1 @@
2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6, 10, 3, 2
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10

1
rolls1.txt Normal file
View File

@ -0,0 +1 @@
10, 3, 2, 2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6

View File

@ -4,6 +4,8 @@
#include "../Include/FileHelper.h"
#include "../Include/NumberHelper.h"
using namespace std;
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Please provide a CSV formatted file.";
@ -11,17 +13,49 @@ int main(int argc, char *argv[]) {
}
if (!FileHelper::FileExists(argv[1])) {
std::cerr << "Filepath: " << argv[1] << " doesn't exist.";
cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
std::string file = FileReader::GetFile(argv[1]);
string file = FileReader::GetFile(argv[1]);
std::vector<int> numbers = NumberHelper::GetNumbers(file);
vector<int> rolls = NumberHelper::GetNumbers(file);
for(int i = 0; i < numbers.size(); i++) {
std::cout << numbers[i] << ' ';
// Calculate score
int score = 0;
int frame = 0;
while (true) {
if (rolls[frame] == 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) {
score += 10 + rolls[frame + 2];
frame += 2;
continue;
}
// Open Frame
score += rolls[frame] + rolls[frame + 1];
frame += 2;
if (frame == rolls.size()) {
break;
}
}
cout << "Score: " << score << std::endl;
return 0;
}