diff --git a/Dockerfile b/Dockerfile index d6f6ece..cb1b009 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,11 @@ FROM gcc:latest +RUN g++ src/*.cpp -o Bowling + WORKDIR /app COPY rolls.txt /app/ -COPY include/* /app/include/ -COPY src/* /app/src/ -RUN g++ src/*.cpp -o Bowling +#RUN g++ src/*.cpp -o Bowling CMD ["./Bowling", "rolls.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 a1da688..ba28ed5 100644 --- a/include/NumberHelper.h +++ b/include/NumberHelper.h @@ -3,6 +3,7 @@ #include #include +#include "../include/Frame.h" using namespace std; @@ -13,6 +14,13 @@ class NumberHelper { ~NumberHelper() = default; 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/rolls2.txt b/rolls2.txt new file mode 100644 index 0000000..2daa3f9 --- /dev/null +++ b/rolls2.txt @@ -0,0 +1 @@ +10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 diff --git a/rolls3.txt b/rolls3.txt new file mode 100644 index 0000000..b32232c --- /dev/null +++ b/rolls3.txt @@ -0,0 +1 @@ +5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 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 bc45262..08add36 100644 --- a/src/NumberHelper.cpp +++ b/src/NumberHelper.cpp @@ -1,4 +1,5 @@ #include "../include/NumberHelper.h" +#include #include #include #include @@ -14,4 +15,117 @@ vector NumberHelper::getRolls(string csv){ } return rolls; +} + +bool NumberHelper::validateRolls(vector rolls){ + int rollCount = rolls.size(); + + for(int i = 0; i < rollCount; ++i) { + if(rolls[i] > 10 || rolls[i] < 0) { + cerr << "Number: " << rolls[i] << " is invalid."; + return false; + } + } + + // A game can not have less than 12 rolls, and more than 21. + if (rollCount < 12 || rollCount > 21) { + cerr << "Incorrect amount of rolls."; + return false; + } + + // 10th frame rule. + if (rollCount == 21) { + if (rolls[18] != 10 && rolls[18] + rolls[19] != 10) { + return false; + } + } + + 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 91b7114..bc7d111 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,10 +22,16 @@ int main(int argc, char *argv[]) { string file = FileReader::getFile(argv[1]); vector rolls = NumberHelper::getRolls(file); + if (!NumberHelper::validateRolls(rolls)) { + return 0; + } + 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;