Compare commits

..

1 Commits

Author SHA1 Message Date
b80d0159f0 Fix rolls4.txt file. It had the wrong amount of rolls 2025-03-26 23:10:56 +01:00
20 changed files with 228 additions and 252 deletions

View File

@ -1,17 +1,11 @@
FROM gcc:latest AS build
WORKDIR /app
COPY include/* /app/include/
COPY src/* /app/src/
FROM gcc:latest
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"]

13
include/FileHelper.h Normal file
View 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
View 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

View File

@ -1,19 +0,0 @@
#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
include/NumberHelper.h Normal file
View File

@ -0,0 +1,27 @@
#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

View File

@ -1,28 +0,0 @@
#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

View File

@ -2,8 +2,6 @@
#define PRINTFRAMES_H
#include <vector>
#include <string>
#include "../include/Frame.h"
using namespace std;
@ -13,12 +11,8 @@ class PrintFrames {
~PrintFrames() = default;
static void printResult(const vector<Frame>& frames);
private:
static void printHeader();
static string parseValue(const vector<Frame>& frames);
static void printHeader(vector<int> rolls);
static void printValue(vector<int> rolls);
};
#endif

View File

@ -13,7 +13,7 @@ class ScoreCalculator {
~ScoreCalculator() = default;
static int getScore(const vector<Frame>& frames);
static int getScore(vector<Frame> rolls);
};
#endif

1
rolls4.txt Normal file
View File

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

13
src/FileHelper.cpp Normal file
View 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
View 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;
}

View File

@ -1,28 +0,0 @@
#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;
}

View File

@ -1,33 +1,27 @@
#include "../include/NumberUtils.h"
#include "../include/NumberHelper.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
vector<int> NumberUtils::getRolls(const string& csv){
vector<int> NumberHelper::getRolls(string csv){
vector<int> rolls;
stringstream ss(csv);
string number;
// 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));
}
return rolls;
}
bool NumberUtils::validateRolls(const vector<int>& 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) {
for(int i = 0; i < rollCount; ++i) {
if(rolls[i] > 10 || rolls[i] < 0) {
cerr << "Number: " << rolls[i] << " is invalid.";
return false;
}
@ -49,41 +43,46 @@ bool NumberUtils::validateRolls(const vector<int>& rolls){
return true;
}
vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
vector<Frame> NumberHelper::createFrames(vector<int> rolls){
vector<Frame> frame;
int roll = 0;
while (roll != rolls.size()) {
while (roll != rolls.size())
{
// 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 (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;
}
// 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 (roll + 3 == rolls.size()) {
frame.push_back(createBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
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]));
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2;
}
// Open Frame
else {
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
else
{
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2;
}
@ -92,7 +91,7 @@ vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
return frame;
}
Frame NumberUtils::createStrikeFrame(int i){
Frame NumberHelper::CreateStrikeFrame(int i){
struct Frame frame = Frame();
vector<int> rolls;
@ -104,7 +103,7 @@ Frame NumberUtils::createStrikeFrame(int i){
return frame;
}
Frame NumberUtils::createFreeFrame(int i, int j){
Frame NumberHelper::CreateFreeFrame(int i, int j){
struct Frame frame = Frame();
vector<int> rolls;
@ -117,7 +116,7 @@ Frame NumberUtils::createFreeFrame(int i, int j){
return frame;
}
Frame NumberUtils::createBonusFrame(int i, int j, int k){
Frame NumberHelper::CreateBonusFrame(int i, int j, int k){
struct Frame frame = Frame();
vector<int> rolls;
@ -129,12 +128,4 @@ Frame NumberUtils::createBonusFrame(int i, int j, int k){
frame.Roll = rolls;
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;
}

View File

@ -1,132 +1,116 @@
#include "../include/PrintFrames.h"
#include <iostream>
#include <sstream>
void PrintFrames::printResult(const vector<Frame>& frames) {
printHeader();
cout << parseValue(frames);
}
void PrintFrames::printHeader() {
cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl;
}
string PrintFrames::parseValue(const vector<Frame>& frames) {
stringstream ss;
void PrintFrames::printHeader(vector<int> rolls){
int frame = 0;
int i = 1;
// There can only be 10 frames.
for (int i = 0; i < 10; i++) {
ss << "|";
while (true) {
cout << "|";
// Strike
if (frames[i].Roll[0] == 10) {
if (frames[i].Roll.size() == 3) {
ss << "X, ";
if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 10) {
ss << "X, X";
}
else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 10) {
ss << frames[i].Roll[1] << ", X ";
}
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) {
ss << "X, " << frames[i].Roll[2] << " ";
}
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 0) {
ss << frames[i].Roll[1] << ", - ";
}
else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] == 10) {
ss << "-, " << frames[i].Roll[2] << " ";
}
else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 0) {
ss << frames[i].Roll[1] << ", - ";
}
else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] != 10) {
ss << "-, " << frames[i].Roll[2] << " ";
}
continue;
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) {
cout << " f" << i << " |" << endl;
break;
}
// If the next frame has 3 or 2 rolls, then take those 2.
if (frames[i + 1].Roll.size() > 1) {
if (frames[i + 1].Roll[0] == 10 && frames[i + 1].Roll[1] != 10) {
ss << "X, " << frames[i + 1].Roll[0] << " ";
}
else if (frames[i + 1].Roll[0] != 10 && frames[i + 1].Roll[1] == 10) {
ss << frames[i + 1].Roll[1] << ", X";
} else {
if (frames[i + 1].Roll[0] + frames[i + 1].Roll[1] == 10) {
ss << "X, " << frames[i + 1].Roll[0] << ", /";
}
else if (frames[i + 1].Roll[0] == 0 && frames[i + 1].Roll[1] != 0) {
ss << "X, " << "-, " << frames[i + 1].Roll[1];
}
else if (frames[i + 1].Roll[0] != 0 && frames[i + 1].Roll[1] == 0) {
ss << "X, " << frames[i + 1].Roll[0] << ", -";
} else {
ss << "X, ";
}
}
continue;
}
// Both next frames, that are not the last, are 10.
ss << "X ";
continue;
cout << " f" << i << " ";
frame += 1;
}
// Spare
if (frames[i].Roll[0] + frames[i].Roll[1] == 10) {
if (frames[i].Roll.size() == 3) {
if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 10 && frames[i].Roll[2] != 10) {
if (frames[i].Roll[2] == 0) {
ss << "-, /, -";
} else {
ss << "-, " << frames[i].Roll[1] << frames[i].Roll[2];
}
}
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) {
if (frames[i].Roll[2] == 0) {
ss << "-, /, -";
} else {
ss << "-, /, " << frames[i].Roll[2];
}
}
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] << ", /, " << frames[i].Roll[2];
}
continue;
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)
{
cout << " f" << i << " |" << endl;
break;
}
ss << frames[i].Roll[0] << ", /";
continue;
cout << " f" << i << " ";
frame += 2;
}
// Open frame
if (i == 9) {
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1] << " ";
} else {
if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 0) {
ss << "-, -";
}
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 0) {
ss << "-, " << frames[i].Roll[1];
}
else if (frames[i].Roll[0] != 0 && frames[i].Roll[1] == 0) {
ss << frames[i].Roll[0] << ", -";
} else {
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1];
}
// Open Frame
else {
cout << " f" << i << " ";
frame += 2;
}
++i;
// No more frames
if (frame == rolls.size()) {
cout << "|" << endl;
break;
}
}
}
void PrintFrames::printValue(vector<int> rolls){
int frame = 0;
ss << "|";
ss << endl;
return ss.str();
while (true) {
cout << "|";
// 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) {
if (rolls[frame + 1] == 10 && rolls[frame + 2] == 10) {
cout << "X, X, X|" << endl;
}
else if (rolls[frame + 1] == 0 && rolls[frame + 2] == 0) {
cout << "X, -, -|" << endl;
}
else if (rolls[frame + 1] == 0) {
cout << "X, -, " << rolls[frame + 2] << "|" << endl;
}
else if (rolls[frame + 2] == 0) {
cout << "X, " << rolls[frame + 1] << ", -|" << endl;
}
else {
cout << "X, " << rolls[frame + 1] << ", " << rolls[frame + 2] << "|" << endl;
}
break;
}
cout << "X ";
frame += 1;
}
// 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)
{
cout << rolls[frame] << ", /" << ", " << rolls[frame + 1] << "|" << endl;
break;
}
cout << rolls[frame] << ", /";
frame += 2;
}
// Open Frame
else {
if (rolls[frame] == 0) {
cout << "-" << ", " << rolls[frame + 1];
}
else if (rolls[frame + 1] == 0) {
cout << rolls[frame] << ", -";
}
cout << "" << rolls[frame] << ", " << rolls[frame + 1];
frame += 2;
}
if (frame == rolls.size()) {
cout << " |" << endl;
break;
}
}
}

View File

@ -1,7 +1,7 @@
#include "../include/ScoreCalculator.h"
#include "../include/Frame.h"
int ScoreCalculator::getScore(const vector<Frame>& frames) {
int ScoreCalculator::getScore(vector<Frame> frames) {
int score = 0;
// There can only be 10 frames.
@ -19,7 +19,7 @@ int ScoreCalculator::getScore(const vector<Frame>& frames) {
continue;
}
// Both next frames, that are not the last, are 10.
// [10] + [x] + [y]
score += frames[i].Roll[0] + frames[i + 1].Roll[0] + frames[i + 2].Roll[0];
continue;
}

View File

@ -1,7 +1,8 @@
#include <iostream>
#include <string>
#include "../include/FileUtils.h"
#include "../include/NumberUtils.h"
#include "../include/FileReader.h"
#include "../include/FileHelper.h"
#include "../include/NumberHelper.h"
#include "../include/ScoreCalculator.h"
#include "../include/PrintFrames.h"
@ -13,21 +14,22 @@ int main(int argc, char *argv[]) {
return 0;
}
if (!FileUtils::fileExists(argv[1])) {
if (!FileHelper::fileExists(argv[1])) {
cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
string file = FileUtils::getFile(argv[1]);
vector<int> rolls = NumberUtils::getRolls(file);
string file = FileReader::getFile(argv[1]);
vector<int> rolls = NumberHelper::getRolls(file);
if (!NumberUtils::validateRolls(rolls)) {
if (!NumberHelper::validateRolls(rolls)) {
return 0;
}
vector<Frame> frames = NumberUtils::createFrames(rolls);
PrintFrames::printHeader(rolls);
PrintFrames::printValue(rolls);
PrintFrames::printResult(frames);
vector<Frame> frames = NumberHelper::createFrames(rolls);
int score = ScoreCalculator::getScore(frames);
cout << "Score: " << score << endl;

View File

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

View File

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