Compare commits

..

No commits in common. "61b00d40dbcadb4ccab5697ecf3c34e759b1d90e" and "c14a29a070f21ed2d5fe2ede5e859a74d2f7a494" have entirely different histories.

10 changed files with 33 additions and 173 deletions

View File

@ -1,11 +1,11 @@
FROM gcc:latest FROM gcc:latest
RUN g++ src/*.cpp -o Bowling
WORKDIR /app WORKDIR /app
COPY rolls.txt /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"] CMD ["./Bowling", "rolls.txt"]

View File

@ -1,11 +0,0 @@
#ifndef FRAME_H
#define FRAME_H
#include <vector>
using namespace std;
struct Frame {
vector<int> Roll;
};
#endif

View File

@ -3,7 +3,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "../include/Frame.h"
using namespace std; using namespace std;
@ -14,13 +13,6 @@ class NumberHelper {
~NumberHelper() = default; ~NumberHelper() = default;
static vector<int> getRolls(string csv); static vector<int> getRolls(string csv);
static bool validateRolls(vector<int> rolls);
static vector<Frame> createFrames(vector<int> rolls);
private:
static Frame CreateStrikeFrame(int i);
static Frame CreateFreeFrame(int i, int j);
static Frame CreateBonusFrame(int i, int j, int k);
}; };

View File

@ -3,7 +3,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "../include/Frame.h"
using namespace std; using namespace std;
@ -13,7 +12,7 @@ class ScoreCalculator {
~ScoreCalculator() = default; ~ScoreCalculator() = default;
static int getScore(vector<Frame> rolls); static int getScore(vector<int> rolls);
}; };
#endif #endif

View File

@ -1 +0,0 @@
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10

View File

@ -1 +0,0 @@
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5

View File

@ -1 +0,0 @@
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5

View File

@ -1,5 +1,4 @@
#include "../include/NumberHelper.h" #include "../include/NumberHelper.h"
#include <iostream>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
@ -16,116 +15,3 @@ vector<int> NumberHelper::getRolls(string csv){
return rolls; return rolls;
} }
bool NumberHelper::validateRolls(vector<int> 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<Frame> NumberHelper::createFrames(vector<int> rolls){
vector<Frame> 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<int> rolls;
rolls.push_back(i);
frame.Roll = rolls;
return frame;
}
Frame NumberHelper::CreateFreeFrame(int i, int j){
struct Frame frame = Frame();
vector<int> 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<int> rolls;
rolls.push_back(i);
rolls.push_back(j);
rolls.push_back(k);
frame.Roll = rolls;
return frame;
}

View File

@ -1,42 +1,45 @@
#include "../include/ScoreCalculator.h" #include "../include/ScoreCalculator.h"
#include "../include/Frame.h"
int ScoreCalculator::getScore(vector<Frame> frames) { int ScoreCalculator::getScore(vector<int> rolls) {
int score = 0; int score = 0;
int frame = 0;
// There can only be 10 frames. while (true) {
for (int i = 0; i < 10; i++) {
// Strike // Strike
if (frames[i].Roll[0] == 10) { if (rolls[frame] == 10) {
if (frames[i].Roll.size() == 3) { // If we're on our last frame, and roll a strike, we're given two bonus rolls.
score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2]; if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
continue; score += 10 + rolls[frame + 1] + rolls[frame + 2];
break;
} }
// If the next frame has 3 or 2 rolls, then take those 2. score += 10 + rolls[frame + 1] + rolls[frame + 2];
if (frames[i + 1].Roll.size() > 1) { frame += 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 // Spare
if (frames[i].Roll[0] + frames[i].Roll[1] == 10) { else if (rolls[frame] + rolls[frame + 1] == 10) {
if (frames[i].Roll.size() == 3) { // If we're on our last frame, and roll a spare, we're given a bonus roll.
score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2]; if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
continue; {
score += 10 + rolls[frame + 1];
break;
} }
score += 10 + frames[i + 1].Roll[0]; score += 10 + rolls[frame + 2];
continue; frame += 2;
} }
// Open frame // Open Frame
score += frames[i].Roll[0] + frames[i].Roll[1]; else {
score += rolls[frame] + rolls[frame + 1];
frame += 2;
}
// No more frames
if (frame == rolls.size()) {
break;
}
} }
return score; return score;

View File

@ -22,16 +22,10 @@ int main(int argc, char *argv[]) {
string file = FileReader::getFile(argv[1]); string file = FileReader::getFile(argv[1]);
vector<int> rolls = NumberHelper::getRolls(file); vector<int> rolls = NumberHelper::getRolls(file);
if (!NumberHelper::validateRolls(rolls)) {
return 0;
}
PrintFrames::printHeader(rolls); PrintFrames::printHeader(rolls);
PrintFrames::printValue(rolls); PrintFrames::printValue(rolls);
vector<Frame> frames = NumberHelper::createFrames(rolls); int score = ScoreCalculator::getScore(rolls);
int score = ScoreCalculator::getScore(frames);
cout << "Score: " << score << endl; cout << "Score: " << score << endl;
return 0; return 0;