15 Commits
Author SHA1 Message Date
owner dbfba6a7dd Removed 'ghost' files 2025-03-27 22:25:54 +01:00
owner 2778a490a7 Changed header names to be more consistent 2025-03-27 21:44:51 +01:00
owner 2ec206b5bb Changed makefile 2025-03-27 18:11:50 +01:00
owner 35cd28b6be Cleaned up main.cpp 2025-03-27 18:09:44 +01:00
owner efc1b264b8 Made code style more consistent, use const reference in parameters, and removed a redundant check 2025-03-27 18:05:20 +01:00
owner 1e51718b04 Merge pull request 'improve-dockerfile' (#13) from improve-dockerfile into main
Reviewed-on: #13
2025-03-27 16:29:12 +00:00
owner aa141a8f22 Changed from ubuntu:25.04 to debian:sid-slim 2025-03-27 17:28:49 +01:00
owner 2e69b2e4cf Merge pull request 'main' (#12) from main into simplify-dockerfile
Reviewed-on: #12
2025-03-27 14:40:53 +00:00
owner 531732c524 Merge pull request 'simplify-printing' (#11) from simplify-printing into main
Reviewed-on: #11
2025-03-27 14:39:43 +00:00
owner 002d8a0804 Add missing value if the last frame is a spare 2025-03-27 15:38:31 +01:00
owner 8222f5e0b3 Merge pull request 'main' (#10) from main into simplify-printing
Reviewed-on: #10
2025-03-27 14:24:03 +00:00
owner b8e2e8e2fd Merge pull request 'validate-input' (#9) from validate-input into main
Reviewed-on: #9
2025-03-27 14:22:04 +00:00
owner 1228b7150a Validate if there are more or less than the allowed rolls, if the numbers are valid and if the number isn't a number. 2025-03-27 15:07:45 +01:00
owner 4a09cbbb33 Merge pull request 'main' (#8) from main into validate-input
Reviewed-on: #8
2025-03-27 12:57:56 +00:00
owner 0ebfb2b86d Merge pull request 'simplify-printing' (#7) from simplify-printing into main
Reviewed-on: #7
2025-03-27 12:54:27 +00:00
19 changed files with 144 additions and 149 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"]
-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;
static void printResult(const vector<Frame>& frames);
private:
static void printHeader();
static void printValue(string values);
static string parseValue(vector<Frame> frames);
static string parseValue(const vector<Frame>& frames);
};
#endif
+1 -1
View File
@@ -13,7 +13,7 @@ class ScoreCalculator {
~ScoreCalculator() = default;
static int getScore(vector<Frame> rolls);
static int getScore(const vector<Frame>& frames);
};
#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 <string>
#include <sstream>
#include <vector>
#include <cctype>
vector<int> NumberHelper::getRolls(string csv){
vector<int> NumberUtils::getRolls(const 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 NumberHelper::validateRolls(vector<int> rolls){
bool NumberUtils::validateRolls(const 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;
}
@@ -43,46 +49,41 @@ bool NumberHelper::validateRolls(vector<int> rolls){
return true;
}
vector<Frame> NumberHelper::createFrames(vector<int> rolls){
vector<Frame> NumberUtils::createFrames(const 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() && rolls[roll] + rolls[roll + 1] == 10)
{
frame.push_back(CreateBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
if (roll + 3 == rolls.size()) {
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;
}
@@ -91,7 +92,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
return frame;
}
Frame NumberHelper::CreateStrikeFrame(int i){
Frame NumberUtils::createStrikeFrame(int i){
struct Frame frame = Frame();
vector<int> rolls;
@@ -103,7 +104,7 @@ Frame NumberHelper::CreateStrikeFrame(int i){
return frame;
}
Frame NumberHelper::CreateFreeFrame(int i, int j){
Frame NumberUtils::createFreeFrame(int i, int j){
struct Frame frame = Frame();
vector<int> rolls;
@@ -116,7 +117,7 @@ Frame NumberHelper::CreateFreeFrame(int i, int j){
return frame;
}
Frame NumberHelper::CreateBonusFrame(int i, int j, int k){
Frame NumberUtils::createBonusFrame(int i, int j, int k){
struct Frame frame = Frame();
vector<int> rolls;
@@ -128,4 +129,12 @@ Frame NumberHelper::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;
}
+10 -17
View File
@@ -2,16 +2,16 @@
#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;
}
void PrintFrames::printValue(string value) {
cout << value;
}
string PrintFrames::parseValue(vector<Frame> frames) {
string value;
string PrintFrames::parseValue(const vector<Frame>& frames) {
stringstream ss;
// 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) {
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] << ", - ";
}
@@ -60,15 +58,12 @@ string PrintFrames::parseValue(vector<Frame> frames) {
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 {
} else {
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) {
ss << "-, /, X";
} else {
ss << frames[i].Roll[0] << ", / ";
ss << frames[i].Roll[0] << ", /, " << frames[i].Roll[2];
}
continue;
}
@@ -121,12 +117,9 @@ string PrintFrames::parseValue(vector<Frame> frames) {
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 {
} else {
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1];
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
#include "../include/ScoreCalculator.h"
#include "../include/Frame.h"
int ScoreCalculator::getScore(vector<Frame> frames) {
int ScoreCalculator::getScore(const vector<Frame>& frames) {
int score = 0;
// There can only be 10 frames.
+8 -12
View File
@@ -1,8 +1,7 @@
#include <iostream>
#include <string>
#include "../include/FileReader.h"
#include "../include/FileHelper.h"
#include "../include/NumberHelper.h"
#include "../include/FileUtils.h"
#include "../include/NumberUtils.h"
#include "../include/ScoreCalculator.h"
#include "../include/PrintFrames.h"
@@ -14,24 +13,21 @@ int main(int argc, char *argv[]) {
return 0;
}
if (!FileHelper::fileExists(argv[1])) {
if (!FileUtils::fileExists(argv[1])) {
cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
string file = FileReader::getFile(argv[1]);
vector<int> rolls = NumberHelper::getRolls(file);
string file = FileUtils::getFile(argv[1]);
vector<int> rolls = NumberUtils::getRolls(file);
if (!NumberHelper::validateRolls(rolls)) {
if (!NumberUtils::validateRolls(rolls)) {
return 0;
}
vector<Frame> frames = NumberHelper::createFrames(rolls);
vector<Frame> frames = NumberUtils::createFrames(rolls);
string values = PrintFrames::parseValue(frames);
PrintFrames::printHeader();
PrintFrames::printValue(values);
PrintFrames::printResult(frames);
int score = ScoreCalculator::getScore(frames);
cout << "Score: " << score << endl;