13 Commits
19 changed files with 144 additions and 149 deletions
+11 -5
View File
@@ -1,11 +1,17 @@
FROM gcc:latest FROM gcc:latest AS build
RUN g++ src/*.cpp -o Bowling
WORKDIR /app WORKDIR /app
COPY include/* /app/include/
COPY src/* /app/src/
RUN g++ src/*.cpp -o Bowling
FROM debian:sid-slim
WORKDIR /app
COPY --from=build /app/Bowling Bowling
COPY rolls.txt /app/ COPY rolls.txt /app/
#RUN g++ src/*.cpp -o Bowling
CMD ["./Bowling", "rolls.txt"] CMD ["./Bowling", "rolls.txt"]
-13
View File
@@ -1,13 +0,0 @@
#ifndef FILEHELPER_H
#define FILEHELPER_H
class FileHelper {
public:
FileHelper() = default;
~FileHelper() = default;
static bool fileExists(char *path);
};
#endif
-18
View File
@@ -1,18 +0,0 @@
#ifndef FILEREADER_H
#define FILEREADER_H
#include <string>
#include <vector>
using namespace std;
class FileReader {
public:
FileReader() = default;
~FileReader() = default;
static string getFile(char *path);
};
#endif
+19
View File
@@ -0,0 +1,19 @@
#ifndef FILEUTILS_H
#define FILEUTILS_H
#include <string>
#include <vector>
using namespace std;
class FileUtils {
public:
FileUtils() = default;
~FileUtils() = default;
static string getFile(char *path);
static bool fileExists(char *path);
};
#endif
-27
View File
@@ -1,27 +0,0 @@
#ifndef NUMBERHELPER_H
#define NUMBERHELPER_H
#include <string>
#include <vector>
#include "../include/Frame.h"
using namespace std;
class NumberHelper {
public:
NumberHelper() = default;
~NumberHelper() = default;
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);
};
#endif
+28
View File
@@ -0,0 +1,28 @@
#ifndef NUMBERUTILS_H
#define NUMBERUTILS_H
#include <string>
#include <vector>
#include "../include/Frame.h"
using namespace std;
class NumberUtils {
public:
NumberUtils() = default;
~NumberUtils() = default;
static vector<int> getRolls(const string& csv);
static bool validateRolls(const vector<int>& rolls);
static vector<Frame> createFrames(const 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);
static bool isNumber(const std::string& str);
};
#endif
+5 -2
View File
@@ -13,9 +13,12 @@ class PrintFrames {
~PrintFrames() = default; ~PrintFrames() = default;
static void printResult(const vector<Frame>& frames);
private:
static void printHeader(); static void printHeader();
static void printValue(string values); static string parseValue(const vector<Frame>& frames);
static string parseValue(vector<Frame> frames);
}; };
#endif #endif
+1 -1
View File
@@ -13,7 +13,7 @@ class ScoreCalculator {
~ScoreCalculator() = default; ~ScoreCalculator() = default;
static int getScore(vector<Frame> rolls); static int getScore(const vector<Frame>& frames);
}; };
#endif #endif
-13
View File
@@ -1,13 +0,0 @@
#include "../include/FileHelper.h"
#include <sys/stat.h>
bool FileHelper::fileExists(char *path){
struct stat s;
// Check if file exists, and if it isn't a folder.
if (stat(path, &s) == 0 && !(s.st_mode & S_IFDIR)){
return true;
}
return false;
}
-16
View File
@@ -1,16 +0,0 @@
#include "../include/FileReader.h"
#include <string>
#include <fstream>
string FileReader::getFile(char *path){
ifstream file(path);
string line;
// There is only one line in the file
getline(file, line);
file.close();
return line;
}
+28
View File
@@ -0,0 +1,28 @@
#include "../include/FileUtils.h"
#include <string>
#include <fstream>
#include <sys/stat.h>
bool FileUtils::fileExists(char *path){
struct stat s;
// Check if file exists, and if it isn't a folder.
if (stat(path, &s) == 0 && !(s.st_mode & S_IFDIR)){
return true;
}
return false;
}
string FileUtils::getFile(char *path){
ifstream file(path);
string line;
// There is only one line in the file
getline(file, line);
file.close();
return line;
}
+33 -24
View File
@@ -1,27 +1,33 @@
#include "../include/NumberHelper.h" #include "../include/NumberUtils.h"
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <cctype>
vector<int> NumberHelper::getRolls(string csv){ vector<int> NumberUtils::getRolls(const string& csv){
vector<int> rolls; vector<int> rolls;
stringstream ss(csv); stringstream ss(csv);
string number; string number;
// Get number from the CSV file, split by comma, and convert to int. // Get number from the CSV file, split by comma, and convert to int.
while (getline(ss, number, ',')) { while (getline(ss, number, ',')) {
if (!isNumber(number)) {
cerr << number << " is not a number.";
break;
}
rolls.push_back(stoi(number)); rolls.push_back(stoi(number));
} }
return rolls; return rolls;
} }
bool NumberHelper::validateRolls(vector<int> rolls){ bool NumberUtils::validateRolls(const vector<int>& rolls){
int rollCount = rolls.size(); int rollCount = rolls.size();
for(int i = 0; i < rollCount; ++i) { for (int i = 0; i < rollCount; ++i) {
if(rolls[i] > 10 || rolls[i] < 0) { if (rolls[i] > 10 || rolls[i] < 0) {
cerr << "Number: " << rolls[i] << " is invalid."; cerr << "Number: " << rolls[i] << " is invalid.";
return false; return false;
} }
@@ -43,46 +49,41 @@ bool NumberHelper::validateRolls(vector<int> rolls){
return true; return true;
} }
vector<Frame> NumberHelper::createFrames(vector<int> rolls){ vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
vector<Frame> frame; vector<Frame> frame;
int roll = 0; int roll = 0;
while (roll != rolls.size()) while (roll != rolls.size()) {
{
// Strike // Strike
if (rolls[roll] == 10) if (rolls[roll] == 10) {
{
// If we're on our last frame, and roll a strike, we're given two bonus rolls. // 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) { if (roll + 3 == rolls.size() && rolls[roll - 3] == 10) {
frame.push_back(CreateBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2])); frame.push_back(createBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
break; break;
} }
frame.push_back(CreateStrikeFrame(10)); frame.push_back(createStrikeFrame(10));
roll += 1; roll += 1;
} }
// Spare // Spare
else if (rolls[roll] + rolls[roll + 1] == 10) 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 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) if (roll + 3 == rolls.size()) {
{ frame.push_back(createBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
frame.push_back(CreateBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
break; break;
} }
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1])); frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2; roll += 2;
} }
// Open Frame // Open Frame
else else {
{ frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2; roll += 2;
} }
@@ -91,7 +92,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
return frame; return frame;
} }
Frame NumberHelper::CreateStrikeFrame(int i){ Frame NumberUtils::createStrikeFrame(int i){
struct Frame frame = Frame(); struct Frame frame = Frame();
vector<int> rolls; vector<int> rolls;
@@ -103,7 +104,7 @@ Frame NumberHelper::CreateStrikeFrame(int i){
return frame; return frame;
} }
Frame NumberHelper::CreateFreeFrame(int i, int j){ Frame NumberUtils::createFreeFrame(int i, int j){
struct Frame frame = Frame(); struct Frame frame = Frame();
vector<int> rolls; vector<int> rolls;
@@ -116,7 +117,7 @@ Frame NumberHelper::CreateFreeFrame(int i, int j){
return frame; return frame;
} }
Frame NumberHelper::CreateBonusFrame(int i, int j, int k){ Frame NumberUtils::createBonusFrame(int i, int j, int k){
struct Frame frame = Frame(); struct Frame frame = Frame();
vector<int> rolls; vector<int> rolls;
@@ -128,4 +129,12 @@ Frame NumberHelper::CreateBonusFrame(int i, int j, int k){
frame.Roll = rolls; frame.Roll = rolls;
return frame; return frame;
}
bool NumberUtils::isNumber(const std::string& str){
for (size_t i = 0; i < str.size(); ++i) {
if (!isdigit(str[i]) && !isspace(str[i])) return false;
}
return true;
} }
+10 -17
View File
@@ -2,16 +2,16 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
void PrintFrames::printResult(const vector<Frame>& frames) {
printHeader();
cout << parseValue(frames);
}
void PrintFrames::printHeader() { void PrintFrames::printHeader() {
cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl; cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl;
} }
void PrintFrames::printValue(string value) { string PrintFrames::parseValue(const vector<Frame>& frames) {
cout << value;
}
string PrintFrames::parseValue(vector<Frame> frames) {
string value;
stringstream ss; stringstream ss;
// There can only be 10 frames. // There can only be 10 frames.
@@ -31,14 +31,12 @@ string PrintFrames::parseValue(vector<Frame> frames) {
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) { else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) {
ss << "X, " << frames[i].Roll[2] << " "; ss << "X, " << frames[i].Roll[2] << " ";
} }
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 0) { else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 0) {
ss << frames[i].Roll[1] << ", - "; ss << frames[i].Roll[1] << ", - ";
} }
else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] == 10) { else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] == 10) {
ss << "-, " << frames[i].Roll[2] << " "; ss << "-, " << frames[i].Roll[2] << " ";
} }
else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 0) { else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 0) {
ss << frames[i].Roll[1] << ", - "; ss << frames[i].Roll[1] << ", - ";
} }
@@ -60,15 +58,12 @@ string PrintFrames::parseValue(vector<Frame> frames) {
if (frames[i + 1].Roll[0] + frames[i + 1].Roll[1] == 10) { if (frames[i + 1].Roll[0] + frames[i + 1].Roll[1] == 10) {
ss << "X, " << frames[i + 1].Roll[0] << ", /"; ss << "X, " << frames[i + 1].Roll[0] << ", /";
} }
else if (frames[i + 1].Roll[0] == 0 && frames[i + 1].Roll[1] != 0) { else if (frames[i + 1].Roll[0] == 0 && frames[i + 1].Roll[1] != 0) {
ss << "X, " << "-, " << frames[i + 1].Roll[1]; ss << "X, " << "-, " << frames[i + 1].Roll[1];
} }
else if (frames[i + 1].Roll[0] != 0 && frames[i + 1].Roll[1] == 0) { else if (frames[i + 1].Roll[0] != 0 && frames[i + 1].Roll[1] == 0) {
ss << "X, " << frames[i + 1].Roll[0] << ", -"; ss << "X, " << frames[i + 1].Roll[0] << ", -";
} } else {
else {
ss << "X, "; ss << "X, ";
} }
} }
@@ -102,8 +97,9 @@ string PrintFrames::parseValue(vector<Frame> frames) {
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 10 && frames[i].Roll[2] == 10) { else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 10 && frames[i].Roll[2] == 10) {
ss << "-, /, X"; ss << "-, /, X";
} else { } else {
ss << frames[i].Roll[0] << ", / "; ss << frames[i].Roll[0] << ", /, " << frames[i].Roll[2];
} }
continue; continue;
} }
@@ -121,12 +117,9 @@ string PrintFrames::parseValue(vector<Frame> frames) {
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 0) { else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 0) {
ss << "-, " << frames[i].Roll[1]; ss << "-, " << frames[i].Roll[1];
} }
else if (frames[i].Roll[0] != 0 && frames[i].Roll[1] == 0) { else if (frames[i].Roll[0] != 0 && frames[i].Roll[1] == 0) {
ss << frames[i].Roll[0] << ", -"; ss << frames[i].Roll[0] << ", -";
} } else {
else {
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1]; ss << frames[i].Roll[0] << ", " << frames[i].Roll[1];
} }
} }
+1 -1
View File
@@ -1,7 +1,7 @@
#include "../include/ScoreCalculator.h" #include "../include/ScoreCalculator.h"
#include "../include/Frame.h" #include "../include/Frame.h"
int ScoreCalculator::getScore(vector<Frame> frames) { int ScoreCalculator::getScore(const vector<Frame>& frames) {
int score = 0; int score = 0;
// There can only be 10 frames. // There can only be 10 frames.
+8 -12
View File
@@ -1,8 +1,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "../include/FileReader.h" #include "../include/FileUtils.h"
#include "../include/FileHelper.h" #include "../include/NumberUtils.h"
#include "../include/NumberHelper.h"
#include "../include/ScoreCalculator.h" #include "../include/ScoreCalculator.h"
#include "../include/PrintFrames.h" #include "../include/PrintFrames.h"
@@ -14,24 +13,21 @@ int main(int argc, char *argv[]) {
return 0; return 0;
} }
if (!FileHelper::fileExists(argv[1])) { if (!FileUtils::fileExists(argv[1])) {
cerr << "Filepath: " << argv[1] << " doesn't exist."; cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0; return 0;
} }
string file = FileReader::getFile(argv[1]); string file = FileUtils::getFile(argv[1]);
vector<int> rolls = NumberHelper::getRolls(file); vector<int> rolls = NumberUtils::getRolls(file);
if (!NumberHelper::validateRolls(rolls)) { if (!NumberUtils::validateRolls(rolls)) {
return 0; return 0;
} }
vector<Frame> frames = NumberHelper::createFrames(rolls); vector<Frame> frames = NumberUtils::createFrames(rolls);
string values = PrintFrames::parseValue(frames); PrintFrames::printResult(frames);
PrintFrames::printHeader();
PrintFrames::printValue(values);
int score = ScoreCalculator::getScore(frames); int score = ScoreCalculator::getScore(frames);
cout << "Score: " << score << endl; cout << "Score: " << score << endl;