Compare commits
No commits in common. "main" and "validate-input" have entirely different histories.
main
...
validate-i
12
Dockerfile
12
Dockerfile
@ -1,17 +1,11 @@
|
|||||||
FROM gcc:latest AS build
|
FROM gcc:latest
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY include/* /app/include/
|
|
||||||
COPY src/* /app/src/
|
|
||||||
|
|
||||||
RUN g++ src/*.cpp -o Bowling
|
RUN g++ src/*.cpp -o Bowling
|
||||||
|
|
||||||
FROM debian:sid-slim
|
|
||||||
|
|
||||||
WORKDIR /app
|
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
include/FileHelper.h
Normal file
13
include/FileHelper.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#ifndef FILEHELPER_H
|
||||||
|
#define FILEHELPER_H
|
||||||
|
|
||||||
|
class FileHelper {
|
||||||
|
public:
|
||||||
|
FileHelper() = default;
|
||||||
|
|
||||||
|
~FileHelper() = default;
|
||||||
|
|
||||||
|
static bool fileExists(char *path);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
18
include/FileReader.h
Normal file
18
include/FileReader.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#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
|
@ -1,16 +1,16 @@
|
|||||||
#ifndef FILEUTILS_H
|
#ifndef FILEUTIL_H
|
||||||
#define FILEUTILS_H
|
#define FILEUTIL_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class FileUtils {
|
class FileUtil {
|
||||||
public:
|
public:
|
||||||
FileUtils() = default;
|
FileUtil() = default;
|
||||||
|
|
||||||
~FileUtils() = default;
|
~FileUtil() = default;
|
||||||
|
|
||||||
static string getFile(char *path);
|
static string getFile(char *path);
|
||||||
static bool fileExists(char *path);
|
static bool fileExists(char *path);
|
@ -1,5 +1,5 @@
|
|||||||
#ifndef NUMBERUTILS_H
|
#ifndef NUMBERHELPER_H
|
||||||
#define NUMBERUTILS_H
|
#define NUMBERHELPER_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -7,15 +7,15 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class NumberUtils {
|
class NumberHelper {
|
||||||
public:
|
public:
|
||||||
NumberUtils() = default;
|
NumberHelper() = default;
|
||||||
|
|
||||||
~NumberUtils() = default;
|
~NumberHelper() = default;
|
||||||
|
|
||||||
static vector<int> getRolls(const string& csv);
|
static vector<int> getRolls(string csv);
|
||||||
static bool validateRolls(const vector<int>& rolls);
|
static bool validateRolls(vector<int> rolls);
|
||||||
static vector<Frame> createFrames(const vector<int>& rolls);
|
static vector<Frame> createFrames(vector<int> rolls);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Frame createStrikeFrame(int i);
|
static Frame createStrikeFrame(int i);
|
@ -13,12 +13,9 @@ class PrintFrames {
|
|||||||
|
|
||||||
~PrintFrames() = default;
|
~PrintFrames() = default;
|
||||||
|
|
||||||
|
|
||||||
static void printResult(const vector<Frame>& frames);
|
|
||||||
|
|
||||||
private:
|
|
||||||
static void printHeader();
|
static void printHeader();
|
||||||
static string parseValue(const vector<Frame>& frames);
|
static void printValue(string values);
|
||||||
|
static string parseValue(vector<Frame> frames);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -13,7 +13,7 @@ class ScoreCalculator {
|
|||||||
|
|
||||||
~ScoreCalculator() = default;
|
~ScoreCalculator() = default;
|
||||||
|
|
||||||
static int getScore(const vector<Frame>& frames);
|
static int getScore(vector<Frame> rolls);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
13
src/FileHelper.cpp
Normal file
13
src/FileHelper.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#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
src/FileReader.cpp
Normal file
16
src/FileReader.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#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;
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
#include "../include/FileUtils.h"
|
#include "../include/FileUtil.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
bool FileUtils::fileExists(char *path){
|
bool FileUtil::fileExists(char *path){
|
||||||
struct stat s;
|
struct stat s;
|
||||||
|
|
||||||
// Check if file exists, and if it isn't a folder.
|
// Check if file exists, and if it isn't a folder.
|
||||||
@ -14,7 +14,7 @@ bool FileUtils::fileExists(char *path){
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
string FileUtils::getFile(char *path){
|
string FileUtil::getFile(char *path){
|
||||||
ifstream file(path);
|
ifstream file(path);
|
||||||
|
|
||||||
string line;
|
string line;
|
@ -1,11 +1,11 @@
|
|||||||
#include "../include/NumberUtils.h"
|
#include "../include/NumberHelper.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
|
||||||
vector<int> NumberUtils::getRolls(const string& csv){
|
vector<int> NumberHelper::getRolls(string csv){
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
stringstream ss(csv);
|
stringstream ss(csv);
|
||||||
string number;
|
string number;
|
||||||
@ -23,11 +23,11 @@ vector<int> NumberUtils::getRolls(const string& csv){
|
|||||||
return rolls;
|
return rolls;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NumberUtils::validateRolls(const vector<int>& rolls){
|
bool NumberHelper::validateRolls(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;
|
||||||
}
|
}
|
||||||
@ -49,14 +49,16 @@ bool NumberUtils::validateRolls(const vector<int>& rolls){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
|
vector<Frame> NumberHelper::createFrames(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]));
|
||||||
@ -69,9 +71,11 @@ vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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()) {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
@ -82,7 +86,8 @@ vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
@ -92,7 +97,7 @@ vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame NumberUtils::createStrikeFrame(int i){
|
Frame NumberHelper::createStrikeFrame(int i){
|
||||||
struct Frame frame = Frame();
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -104,7 +109,7 @@ Frame NumberUtils::createStrikeFrame(int i){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame NumberUtils::createFreeFrame(int i, int j){
|
Frame NumberHelper::createFreeFrame(int i, int j){
|
||||||
struct Frame frame = Frame();
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -117,7 +122,7 @@ Frame NumberUtils::createFreeFrame(int i, int j){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame NumberUtils::createBonusFrame(int i, int j, int k){
|
Frame NumberHelper::createBonusFrame(int i, int j, int k){
|
||||||
struct Frame frame = Frame();
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -131,7 +136,7 @@ Frame NumberUtils::createBonusFrame(int i, int j, int k){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NumberUtils::isNumber(const std::string& str){
|
bool NumberHelper::isNumber(const std::string& str){
|
||||||
for (size_t i = 0; i < str.size(); ++i) {
|
for (size_t i = 0; i < str.size(); ++i) {
|
||||||
if (!isdigit(str[i]) && !isspace(str[i])) return false;
|
if (!isdigit(str[i]) && !isspace(str[i])) return false;
|
||||||
}
|
}
|
@ -2,16 +2,15 @@
|
|||||||
#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;
|
||||||
}
|
}
|
||||||
|
|
||||||
string PrintFrames::parseValue(const vector<Frame>& frames) {
|
void PrintFrames::printValue(string value) {
|
||||||
|
cout << value;
|
||||||
|
}
|
||||||
|
|
||||||
|
string PrintFrames::parseValue(vector<Frame> frames) {
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
|
|
||||||
// There can only be 10 frames.
|
// There can only be 10 frames.
|
||||||
@ -31,12 +30,14 @@ string PrintFrames::parseValue(const 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] << ", - ";
|
||||||
}
|
}
|
||||||
@ -58,12 +59,15 @@ string PrintFrames::parseValue(const 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, ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,9 +101,8 @@ string PrintFrames::parseValue(const 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] << ", /, " << frames[i].Roll[2];
|
ss << frames[i].Roll[0] << ", / ";
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,9 +120,12 @@ string PrintFrames::parseValue(const 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,7 +1,7 @@
|
|||||||
#include "../include/ScoreCalculator.h"
|
#include "../include/ScoreCalculator.h"
|
||||||
#include "../include/Frame.h"
|
#include "../include/Frame.h"
|
||||||
|
|
||||||
int ScoreCalculator::getScore(const vector<Frame>& frames) {
|
int ScoreCalculator::getScore(vector<Frame> frames) {
|
||||||
int score = 0;
|
int score = 0;
|
||||||
|
|
||||||
// There can only be 10 frames.
|
// There can only be 10 frames.
|
||||||
|
19
src/main.cpp
19
src/main.cpp
@ -1,7 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "../include/FileUtils.h"
|
#include "../include/FileUtil.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"
|
||||||
|
|
||||||
@ -13,21 +13,24 @@ int main(int argc, char *argv[]) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!FileUtils::fileExists(argv[1])) {
|
if (!FileUtil::fileExists(argv[1])) {
|
||||||
cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
string file = FileUtils::getFile(argv[1]);
|
string file = FileUtil::getFile(argv[1]);
|
||||||
vector<int> rolls = NumberUtils::getRolls(file);
|
vector<int> rolls = NumberHelper::getRolls(file);
|
||||||
|
|
||||||
if (!NumberUtils::validateRolls(rolls)) {
|
if (!NumberHelper::validateRolls(rolls)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<Frame> frames = NumberUtils::createFrames(rolls);
|
vector<Frame> frames = NumberHelper::createFrames(rolls);
|
||||||
|
|
||||||
PrintFrames::printResult(frames);
|
string values = PrintFrames::parseValue(frames);
|
||||||
|
|
||||||
|
PrintFrames::printHeader();
|
||||||
|
PrintFrames::printValue(values);
|
||||||
|
|
||||||
int score = ScoreCalculator::getScore(frames);
|
int score = ScoreCalculator::getScore(frames);
|
||||||
cout << "Score: " << score << endl;
|
cout << "Score: " << score << endl;
|
||||||
|
Loading…
Reference in New Issue
Block a user