7 changed files with 88 additions and 22 deletions
+11 -5
View File
@@ -1,11 +1,17 @@
FROM gcc:latest
RUN g++ src/*.cpp -o Bowling
FROM gcc:latest AS build
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/
#RUN g++ src/*.cpp -o Bowling
CMD ["./Bowling", "rolls.txt"]
+19
View File
@@ -0,0 +1,19 @@
#ifndef FILEUTIL_H
#define FILEUTIL_H
#include <string>
#include <vector>
using namespace std;
class FileUtil {
public:
FileUtil() = default;
~FileUtil() = default;
static string getFile(char *path);
static bool fileExists(char *path);
};
#endif
+4 -3
View File
@@ -18,9 +18,10 @@ class NumberHelper {
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);
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);
};
+28
View File
@@ -0,0 +1,28 @@
#include "../include/FileUtil.h"
#include <string>
#include <fstream>
#include <sys/stat.h>
bool FileUtil::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 FileUtil::getFile(char *path){
ifstream file(path);
string line;
// There is only one line in the file
getline(file, line);
file.close();
return line;
}
+22 -8
View File
@@ -3,6 +3,7 @@
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
vector<int> NumberHelper::getRolls(string csv){
vector<int> rolls;
@@ -11,6 +12,11 @@ vector<int> NumberHelper::getRolls(string csv){
// Get number from the CSV file, split by comma, and convert to int.
while (getline(ss, number, ',')) {
if (!isNumber(number)) {
cerr << number << " is not a number.";
break;
}
rolls.push_back(stoi(number));
}
@@ -55,11 +61,11 @@ vector<Frame> NumberHelper::createFrames(vector<int> 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) {
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;
}
frame.push_back(CreateStrikeFrame(10));
frame.push_back(createStrikeFrame(10));
roll += 1;
}
@@ -70,11 +76,11 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
// 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]));
frame.push_back(createBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
break;
}
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2;
}
@@ -82,7 +88,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
// Open Frame
else
{
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2;
}
@@ -91,7 +97,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
return frame;
}
Frame NumberHelper::CreateStrikeFrame(int i){
Frame NumberHelper::createStrikeFrame(int i){
struct Frame frame = Frame();
vector<int> rolls;
@@ -103,7 +109,7 @@ Frame NumberHelper::CreateStrikeFrame(int i){
return frame;
}
Frame NumberHelper::CreateFreeFrame(int i, int j){
Frame NumberHelper::createFreeFrame(int i, int j){
struct Frame frame = Frame();
vector<int> rolls;
@@ -116,7 +122,7 @@ Frame NumberHelper::CreateFreeFrame(int i, int j){
return frame;
}
Frame NumberHelper::CreateBonusFrame(int i, int j, int k){
Frame NumberHelper::createBonusFrame(int i, int j, int k){
struct Frame frame = Frame();
vector<int> rolls;
@@ -129,3 +135,11 @@ Frame NumberHelper::CreateBonusFrame(int i, int j, int k){
return frame;
}
bool NumberHelper::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;
}
+1 -2
View File
@@ -11,7 +11,6 @@ void PrintFrames::printValue(string value) {
}
string PrintFrames::parseValue(vector<Frame> frames) {
string value;
stringstream ss;
// There can only be 10 frames.
@@ -102,7 +101,7 @@ string PrintFrames::parseValue(vector<Frame> frames) {
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] << ", / ";
ss << frames[i].Roll[0] << ", /, " << frames[i].Roll[2];
}
continue;
}
+3 -4
View File
@@ -1,7 +1,6 @@
#include <iostream>
#include <string>
#include "../include/FileReader.h"
#include "../include/FileHelper.h"
#include "../include/FileUtil.h"
#include "../include/NumberHelper.h"
#include "../include/ScoreCalculator.h"
#include "../include/PrintFrames.h"
@@ -14,12 +13,12 @@ int main(int argc, char *argv[]) {
return 0;
}
if (!FileHelper::fileExists(argv[1])) {
if (!FileUtil::fileExists(argv[1])) {
cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
string file = FileReader::getFile(argv[1]);
string file = FileUtil::getFile(argv[1]);
vector<int> rolls = NumberHelper::getRolls(file);
if (!NumberHelper::validateRolls(rolls)) {