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;