From f5e96e7704afa23ef32381bae0f1ad894e250848 Mon Sep 17 00:00:00 2001 From: rasmus Date: Wed, 26 Mar 2025 23:03:23 +0100 Subject: [PATCH 1/4] Now iterating over a Frame object --- include/Frame.h | 11 +++++ include/NumberHelper.h | 7 ++++ include/ScoreCalculator.h | 3 +- rolls4.txt | 1 + src/NumberHelper.cpp | 87 +++++++++++++++++++++++++++++++++++++++ src/ScoreCalculator.cpp | 53 +++++++++++------------- src/main.cpp | 4 +- 7 files changed, 136 insertions(+), 30 deletions(-) create mode 100644 include/Frame.h create mode 100644 rolls4.txt diff --git a/include/Frame.h b/include/Frame.h new file mode 100644 index 0000000..0ae8f34 --- /dev/null +++ b/include/Frame.h @@ -0,0 +1,11 @@ +#ifndef FRAME_H +#define FRAME_H + +#include +using namespace std; + +struct Frame { + vector Roll; +}; + +#endif \ No newline at end of file diff --git a/include/NumberHelper.h b/include/NumberHelper.h index daa95f9..ba28ed5 100644 --- a/include/NumberHelper.h +++ b/include/NumberHelper.h @@ -3,6 +3,7 @@ #include #include +#include "../include/Frame.h" using namespace std; @@ -14,6 +15,12 @@ class NumberHelper { static vector getRolls(string csv); static bool validateRolls(vector rolls); + static vector createFrames(vector rolls); + + private: + static Frame CreateStrikeFrame(int i); + static Frame CreateFreeFrame(int i, int j); + static Frame CreateBonusFrame(int i, int j, int k); }; diff --git a/include/ScoreCalculator.h b/include/ScoreCalculator.h index d38289e..724a60e 100644 --- a/include/ScoreCalculator.h +++ b/include/ScoreCalculator.h @@ -3,6 +3,7 @@ #include #include +#include "../include/Frame.h" using namespace std; @@ -12,7 +13,7 @@ class ScoreCalculator { ~ScoreCalculator() = default; - static int getScore(vector rolls); + static int getScore(vector rolls); }; #endif \ No newline at end of file diff --git a/rolls4.txt b/rolls4.txt new file mode 100644 index 0000000..5818318 --- /dev/null +++ b/rolls4.txt @@ -0,0 +1 @@ +5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5 diff --git a/src/NumberHelper.cpp b/src/NumberHelper.cpp index 07b4230..08add36 100644 --- a/src/NumberHelper.cpp +++ b/src/NumberHelper.cpp @@ -41,4 +41,91 @@ bool NumberHelper::validateRolls(vector rolls){ } return true; +} + +vector NumberHelper::createFrames(vector rolls){ + vector frame; + + int roll = 0; + + while (roll != rolls.size()) + { + // Strike + if (rolls[roll] == 10) + { + // If we're on our last frame, and roll a strike, we're given two bonus rolls. + if (roll + 3 == rolls.size() && rolls[roll - 3] == 10) { + frame.push_back(CreateBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2])); + break; + } + + frame.push_back(CreateStrikeFrame(10)); + + roll += 1; + } + + // Spare + else if (rolls[roll] + rolls[roll + 1] == 10) + { + // If we're on our last frame, and roll a spare, we're given a bonus roll. + if (roll + 3 == rolls.size() && rolls[roll] + rolls[roll + 1] == 10) + { + frame.push_back(CreateBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2])); + break; + } + + frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1])); + + roll += 2; + } + + // Open Frame + else + { + frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1])); + + roll += 2; + } + } + + return frame; +} + +Frame NumberHelper::CreateStrikeFrame(int i){ + struct Frame frame = Frame(); + + vector rolls; + + rolls.push_back(i); + + frame.Roll = rolls; + + return frame; +} + +Frame NumberHelper::CreateFreeFrame(int i, int j){ + struct Frame frame = Frame(); + + vector rolls; + + rolls.push_back(i); + rolls.push_back(j); + + frame.Roll = rolls; + + return frame; +} + +Frame NumberHelper::CreateBonusFrame(int i, int j, int k){ + struct Frame frame = Frame(); + + vector rolls; + + rolls.push_back(i); + rolls.push_back(j); + rolls.push_back(k); + + frame.Roll = rolls; + + return frame; } \ No newline at end of file diff --git a/src/ScoreCalculator.cpp b/src/ScoreCalculator.cpp index 5b5e147..c265053 100644 --- a/src/ScoreCalculator.cpp +++ b/src/ScoreCalculator.cpp @@ -1,45 +1,42 @@ #include "../include/ScoreCalculator.h" +#include "../include/Frame.h" -int ScoreCalculator::getScore(vector rolls) { +int ScoreCalculator::getScore(vector frames) { int score = 0; - int frame = 0; - while (true) { + // There can only be 10 frames. + for (int i = 0; i < 10; i++) { // 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; + 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; } - score += 10 + rolls[frame + 1] + rolls[frame + 2]; - frame += 1; + // 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; + } + + // [10] + [x] + [y] + score += frames[i].Roll[0] + frames[i + 1].Roll[0] + frames[i + 2].Roll[0]; + continue; } // 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; + 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 + rolls[frame + 2]; - frame += 2; + score += 10 + frames[i + 1].Roll[0]; + continue; } - // Open Frame - else { - score += rolls[frame] + rolls[frame + 1]; - frame += 2; - } - - // No more frames - if (frame == rolls.size()) { - break; - } + // Open frame + score += frames[i].Roll[0] + frames[i].Roll[1]; } return score; diff --git a/src/main.cpp b/src/main.cpp index 04d7af2..bc7d111 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,7 +29,9 @@ int main(int argc, char *argv[]) { PrintFrames::printHeader(rolls); PrintFrames::printValue(rolls); - int score = ScoreCalculator::getScore(rolls); + vector frames = NumberHelper::createFrames(rolls); + + int score = ScoreCalculator::getScore(frames); cout << "Score: " << score << endl; return 0; From 7d9da3cc50c116235604248bebceead73ba72ffb Mon Sep 17 00:00:00 2001 From: rasmus Date: Thu, 27 Mar 2025 12:24:02 +0100 Subject: [PATCH 2/4] The printing function now only prints a single string, that has been created by the function PrintFrames::parseValue(vector frames) that parses the frames --- include/PrintFrames.h | 7 +- rolls4.txt | 2 +- src/PrintFrames.cpp | 163 +++++++++++++++++----------------------- src/ScoreCalculator.cpp | 2 +- src/main.cpp | 8 +- 5 files changed, 82 insertions(+), 100 deletions(-) diff --git a/include/PrintFrames.h b/include/PrintFrames.h index d4b508d..782d82e 100644 --- a/include/PrintFrames.h +++ b/include/PrintFrames.h @@ -2,6 +2,8 @@ #define PRINTFRAMES_H #include +#include +#include "../include/Frame.h" using namespace std; @@ -11,8 +13,9 @@ class PrintFrames { ~PrintFrames() = default; - static void printHeader(vector rolls); - static void printValue(vector rolls); + static void printHeader(); + static void printValue(string values); + static string parseValue(vector frames); }; #endif \ No newline at end of file diff --git a/rolls4.txt b/rolls4.txt index 5818318..2c19309 100644 --- a/rolls4.txt +++ b/rolls4.txt @@ -1 +1 @@ -5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5 +0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5 diff --git a/src/PrintFrames.cpp b/src/PrintFrames.cpp index 58edc9b..6f28fc8 100644 --- a/src/PrintFrames.cpp +++ b/src/PrintFrames.cpp @@ -1,116 +1,93 @@ #include "../include/PrintFrames.h" #include +#include -void PrintFrames::printHeader(vector rolls){ - int frame = 0; - int i = 1; - - while (true) { - cout << "|"; - - // 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) { - cout << " f" << i << " |" << endl; - break; - } - - cout << " f" << i << " "; - 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) - { - cout << " f" << i << " |" << endl; - break; - } - - cout << " f" << i << " "; - frame += 2; - } - - // Open Frame - else { - cout << " f" << i << " "; - frame += 2; - } - - ++i; - - // No more frames - if (frame == rolls.size()) { - cout << "|" << endl; - break; - } - } +void PrintFrames::printHeader() { + cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl; } -void PrintFrames::printValue(vector rolls){ - int frame = 0; +void PrintFrames::printValue(string value) { + cout << value; +} + +string PrintFrames::parseValue(vector frames) { + string value; + stringstream ss; - while (true) { - cout << "|"; - + int score = 0; + + // There can only be 10 frames. + for (int i = 0; i < 10; i++) { + ss << "|"; // 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) { - if (rolls[frame + 1] == 10 && rolls[frame + 2] == 10) { - cout << "X, X, X|" << endl; + if (frames[i].Roll[0] == 10) { + if (frames[i].Roll.size() == 3) { + ss << "X, "; + + if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 10) { + ss << "X, X"; } - else if (rolls[frame + 1] == 0 && rolls[frame + 2] == 0) { - cout << "X, -, -|" << endl; + else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 10) { + ss << frames[i].Roll[1] << ", X "; } - else if (rolls[frame + 1] == 0) { - cout << "X, -, " << rolls[frame + 2] << "|" << endl; - } - else if (rolls[frame + 2] == 0) { - cout << "X, " << rolls[frame + 1] << ", -|" << endl; - } - else { - cout << "X, " << rolls[frame + 1] << ", " << rolls[frame + 2] << "|" << endl; + else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) { + ss << "X, " << frames[i].Roll[2] << " "; } - break; + continue; } - cout << "X "; - frame += 1; + // 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]; + + if (frames[i + 1].Roll[0] == 10 && frames[i + 1].Roll[1] != 10) { + ss << "X, " << frames[i + 1].Roll[0] << " "; + } + else if (frames[i + 1].Roll[0] != 10 && frames[i + 1].Roll[1] == 10) { + ss << frames[i + 1].Roll[1] << ", X"; + } else { + if (frames[i + 1].Roll[0] + frames[i + 1].Roll[1] == 10) { + ss << "X, " << frames[i + 1].Roll[0] << " ,/"; + } else { + ss << "X, "; + } + } + + 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]; + ss << "X "; + + continue; } // 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) - { - cout << rolls[frame] << ", /" << ", " << rolls[frame + 1] << "|" << endl; - break; + 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]; + ss << frames[i].Roll[0] << ", / "; + continue; } - cout << rolls[frame] << ", /"; - frame += 2; + //score += 10 + frames[i + 1].Roll[0]; + ss << frames[i].Roll[0] << ", /"; + continue; } - // Open Frame - else { - if (rolls[frame] == 0) { - cout << "-" << ", " << rolls[frame + 1]; - } - else if (rolls[frame + 1] == 0) { - cout << rolls[frame] << ", -"; - } - - cout << "" << rolls[frame] << ", " << rolls[frame + 1]; - frame += 2; - } - - if (frame == rolls.size()) { - cout << " |" << endl; - break; + // Open frame + //score += frames[i].Roll[0] + frames[i].Roll[1]; + if (i == 9) { + ss << frames[i].Roll[0] << ", " << frames[i].Roll[1] << " "; + } else { + ss << frames[i].Roll[0] << ", " << frames[i].Roll[1]; } } + + ss << "|"; + ss << endl; + + return ss.str(); } \ No newline at end of file diff --git a/src/ScoreCalculator.cpp b/src/ScoreCalculator.cpp index c265053..d85529b 100644 --- a/src/ScoreCalculator.cpp +++ b/src/ScoreCalculator.cpp @@ -19,7 +19,7 @@ int ScoreCalculator::getScore(vector frames) { continue; } - // [10] + [x] + [y] + // 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; } diff --git a/src/main.cpp b/src/main.cpp index bc7d111..b800f92 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,11 +26,13 @@ int main(int argc, char *argv[]) { return 0; } - PrintFrames::printHeader(rolls); - PrintFrames::printValue(rolls); - vector frames = NumberHelper::createFrames(rolls); + string values = PrintFrames::parseValue(frames); + + PrintFrames::printHeader(); + PrintFrames::printValue(values); + int score = ScoreCalculator::getScore(frames); cout << "Score: " << score << endl; From aa81717fd978ee12381f7ed1eabfb706d70cd2a8 Mon Sep 17 00:00:00 2001 From: rasmus Date: Thu, 27 Mar 2025 12:26:17 +0100 Subject: [PATCH 3/4] Removing the redundant score calculation from parseValues, as the overarching logic is the same as the calculateScore function --- src/PrintFrames.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/PrintFrames.cpp b/src/PrintFrames.cpp index 6f28fc8..6ba206a 100644 --- a/src/PrintFrames.cpp +++ b/src/PrintFrames.cpp @@ -14,8 +14,6 @@ string PrintFrames::parseValue(vector frames) { string value; stringstream ss; - int score = 0; - // There can only be 10 frames. for (int i = 0; i < 10; i++) { ss << "|"; @@ -39,8 +37,6 @@ string PrintFrames::parseValue(vector frames) { // 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]; - if (frames[i + 1].Roll[0] == 10 && frames[i + 1].Roll[1] != 10) { ss << "X, " << frames[i + 1].Roll[0] << " "; } @@ -58,7 +54,6 @@ string PrintFrames::parseValue(vector frames) { } // 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]; ss << "X "; continue; @@ -67,18 +62,15 @@ string PrintFrames::parseValue(vector frames) { // 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]; ss << frames[i].Roll[0] << ", / "; continue; } - //score += 10 + frames[i + 1].Roll[0]; ss << frames[i].Roll[0] << ", /"; continue; } // Open frame - //score += frames[i].Roll[0] + frames[i].Roll[1]; if (i == 9) { ss << frames[i].Roll[0] << ", " << frames[i].Roll[1] << " "; } else { From 76284c3951146654c55abc9814113acb67507d6e Mon Sep 17 00:00:00 2001 From: rasmus Date: Thu, 27 Mar 2025 13:54:02 +0100 Subject: [PATCH 4/4] Add 0 roll handling aswell --- rolls4.txt | 2 +- rolls5.txt | 1 + src/PrintFrames.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 rolls5.txt diff --git a/rolls4.txt b/rolls4.txt index 2c19309..cd432e6 100644 --- a/rolls4.txt +++ b/rolls4.txt @@ -1 +1 @@ -0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5 +5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5 diff --git a/rolls5.txt b/rolls5.txt new file mode 100644 index 0000000..9a56ce2 --- /dev/null +++ b/rolls5.txt @@ -0,0 +1 @@ +5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 10, 10 diff --git a/src/PrintFrames.cpp b/src/PrintFrames.cpp index 6ba206a..fc4d3de 100644 --- a/src/PrintFrames.cpp +++ b/src/PrintFrames.cpp @@ -32,6 +32,20 @@ string PrintFrames::parseValue(vector frames) { ss << "X, " << frames[i].Roll[2] << " "; } + else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 0) { + ss << frames[i].Roll[1] << ", - "; + } + else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] == 10) { + ss << "-, " << frames[i].Roll[2] << " "; + } + + else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 0) { + ss << frames[i].Roll[1] << ", - "; + } + else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] != 10) { + ss << "-, " << frames[i].Roll[2] << " "; + } + continue; } @@ -44,8 +58,17 @@ string PrintFrames::parseValue(vector frames) { ss << frames[i + 1].Roll[1] << ", X"; } else { if (frames[i + 1].Roll[0] + frames[i + 1].Roll[1] == 10) { - ss << "X, " << frames[i + 1].Roll[0] << " ,/"; - } else { + ss << "X, " << frames[i + 1].Roll[0] << ", /"; + } + + else if (frames[i + 1].Roll[0] == 0 && frames[i + 1].Roll[1] != 0) { + ss << "X, " << "-, " << frames[i + 1].Roll[1]; + } + else if (frames[i + 1].Roll[0] != 0 && frames[i + 1].Roll[1] == 0) { + ss << "X, " << frames[i + 1].Roll[0] << ", -"; + } + + else { ss << "X, "; } } @@ -62,7 +85,25 @@ string PrintFrames::parseValue(vector frames) { // Spare if (frames[i].Roll[0] + frames[i].Roll[1] == 10) { if (frames[i].Roll.size() == 3) { - ss << frames[i].Roll[0] << ", / "; + if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 10 && frames[i].Roll[2] != 10) { + if (frames[i].Roll[2] == 0) { + ss << "-, /, -"; + } else { + ss << "-, " << frames[i].Roll[1] << frames[i].Roll[2]; + } + } + else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) { + if (frames[i].Roll[2] == 0) { + ss << "-, /, -"; + } else { + ss << "-, /, " << frames[i].Roll[2]; + } + } + else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 10 && frames[i].Roll[2] == 10) { + ss << "-, /, X"; + } else { + ss << frames[i].Roll[0] << ", / "; + } continue; } @@ -74,7 +115,20 @@ string PrintFrames::parseValue(vector frames) { if (i == 9) { ss << frames[i].Roll[0] << ", " << frames[i].Roll[1] << " "; } else { - ss << frames[i].Roll[0] << ", " << frames[i].Roll[1]; + if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 0) { + ss << "-, -"; + } + else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 0) { + ss << "-, " << frames[i].Roll[1]; + } + + else if (frames[i].Roll[0] != 0 && frames[i].Roll[1] == 0) { + ss << frames[i].Roll[0] << ", -"; + } + + else { + ss << frames[i].Roll[0] << ", " << frames[i].Roll[1]; + } } }