Compare commits

..

27 Commits

Author SHA1 Message Date
dbfba6a7dd Removed 'ghost' files 2025-03-27 22:25:54 +01:00
2778a490a7 Changed header names to be more consistent 2025-03-27 21:44:51 +01:00
2ec206b5bb Changed makefile 2025-03-27 18:11:50 +01:00
35cd28b6be Cleaned up main.cpp 2025-03-27 18:09:44 +01:00
efc1b264b8 Made code style more consistent, use const reference in parameters, and removed a redundant check 2025-03-27 18:05:20 +01:00
1e51718b04 Merge pull request 'improve-dockerfile' (#13) from improve-dockerfile into main
Reviewed-on: #13
2025-03-27 16:29:12 +00:00
aa141a8f22 Changed from ubuntu:25.04 to debian:sid-slim 2025-03-27 17:28:49 +01:00
2e69b2e4cf Merge pull request 'main' (#12) from main into simplify-dockerfile
Reviewed-on: #12
2025-03-27 14:40:53 +00:00
531732c524 Merge pull request 'simplify-printing' (#11) from simplify-printing into main
Reviewed-on: #11
2025-03-27 14:39:43 +00:00
002d8a0804 Add missing value if the last frame is a spare 2025-03-27 15:38:31 +01:00
8222f5e0b3 Merge pull request 'main' (#10) from main into simplify-printing
Reviewed-on: #10
2025-03-27 14:24:03 +00:00
b8e2e8e2fd Merge pull request 'validate-input' (#9) from validate-input into main
Reviewed-on: #9
2025-03-27 14:22:04 +00:00
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
4a09cbbb33 Merge pull request 'main' (#8) from main into validate-input
Reviewed-on: #8
2025-03-27 12:57:56 +00:00
0ebfb2b86d Merge pull request 'simplify-printing' (#7) from simplify-printing into main
Reviewed-on: #7
2025-03-27 12:54:27 +00:00
76284c3951 Add 0 roll handling aswell 2025-03-27 13:54:02 +01:00
aa81717fd9 Removing the redundant score calculation from parseValues, as the overarching logic is the same as the calculateScore function 2025-03-27 12:26:17 +01:00
7d9da3cc50 The printing function now only prints a single string, that has been created by the function PrintFrames::parseValue(vector<Frame> frames) that parses the frames 2025-03-27 12:24:02 +01:00
61b00d40db Merge pull request 'simplify-score-calculation' (#6) from simplify-score-calculation into main
Reviewed-on: #6
2025-03-26 22:10:20 +00:00
f5e96e7704 Now iterating over a Frame object 2025-03-26 23:03:23 +01:00
05ba979f58 Testing roll combination validation 2025-03-26 12:44:11 +01:00
8fdd4c18fd Validate if roll is higher than 10, and lower than 0 2025-03-26 12:05:45 +01:00
c14a29a070 Add comment 2025-03-19 11:58:53 +01:00
3420815c74 Update README.md 2025-03-19 10:56:40 +00:00
f076e0e35c Remove unused text file 2025-03-19 11:51:10 +01:00
ceddebcd80 Merge pull request 'Remove unused folder' (#5) from cleanup into main
Reviewed-on: #5
2025-03-19 10:50:03 +00:00
b285704ae1 Merge pull request 'cleanup' (#4) from cleanup into main
Reviewed-on: #4
2025-03-19 10:48:15 +00:00
23 changed files with 392 additions and 227 deletions

View File

@ -1,11 +1,17 @@
FROM gcc:latest FROM gcc:latest AS build
WORKDIR /app WORKDIR /app
COPY rolls.txt /app/
COPY include/* /app/include/ COPY include/* /app/include/
COPY src/* /app/src/ COPY src/* /app/src/
RUN g++ src/*.cpp -o Bowling RUN g++ src/*.cpp -o Bowling
FROM debian:sid-slim
WORKDIR /app
COPY --from=build /app/Bowling Bowling
COPY rolls.txt /app/
CMD ["./Bowling", "rolls.txt"] CMD ["./Bowling", "rolls.txt"]

View File

@ -2,6 +2,7 @@
To run the project, simply follow these steps: To run the project, simply follow these steps:
1. `git clone https://git.rbwr.dk/owner/Bowling.git` 1. `git clone https://git.rbwr.dk/owner/Bowling.git`
2. `cd Bowling/`
2. `docker build -t bowling .` 2. `docker build -t bowling .`
3. `docker run --rm bowling` 3. `docker run --rm bowling`

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

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
include/FileUtils.h Normal file
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

11
include/Frame.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef FRAME_H
#define FRAME_H
#include <vector>
using namespace std;
struct Frame {
vector<int> Roll;
};
#endif

View File

@ -1,19 +0,0 @@
#ifndef NUMBERHELPER_H
#define NUMBERHELPER_H
#include <string>
#include <vector>
using namespace std;
class NumberHelper {
public:
NumberHelper() = default;
~NumberHelper() = default;
static vector<int> getRolls(string csv);
};
#endif

28
include/NumberUtils.h Normal file
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

View File

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

View File

@ -3,6 +3,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "../include/Frame.h"
using namespace std; using namespace std;
@ -12,7 +13,7 @@ class ScoreCalculator {
~ScoreCalculator() = default; ~ScoreCalculator() = default;
static int getScore(vector<int> rolls); static int getScore(const vector<Frame>& frames);
}; };
#endif #endif

View File

@ -1 +0,0 @@
10, 3, 2, 2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6

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;
}

View File

@ -1,15 +0,0 @@
#include "../include/FileReader.h"
#include <string>
#include <fstream>
string FileReader::getFile(char *path){
ifstream file(path);
string line;
getline(file, line);
file.close();
return line;
}

28
src/FileUtils.cpp Normal file
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;
}

View File

@ -1,17 +0,0 @@
#include "../include/NumberHelper.h"
#include <string>
#include <sstream>
#include <vector>
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, ',')) {
rolls.push_back(stoi(number));
}
return rolls;
}

140
src/NumberUtils.cpp Normal file
View File

@ -0,0 +1,140 @@
#include "../include/NumberUtils.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
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 NumberUtils::validateRolls(const vector<int>& rolls){
int rollCount = rolls.size();
for (int i = 0; i < rollCount; ++i) {
if (rolls[i] > 10 || rolls[i] < 0) {
cerr << "Number: " << rolls[i] << " is invalid.";
return false;
}
}
// A game can not have less than 12 rolls, and more than 21.
if (rollCount < 12 || rollCount > 21) {
cerr << "Incorrect amount of rolls.";
return false;
}
// 10th frame rule.
if (rollCount == 21) {
if (rolls[18] != 10 && rolls[18] + rolls[19] != 10) {
return false;
}
}
return true;
}
vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
vector<Frame> frame;
int roll = 0;
while (roll != rolls.size()) {
// Strike
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]));
break;
}
frame.push_back(createStrikeFrame(10));
roll += 1;
}
// Spare
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]));
break;
}
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2;
}
// Open Frame
else {
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2;
}
}
return frame;
}
Frame NumberUtils::createStrikeFrame(int i){
struct Frame frame = Frame();
vector<int> rolls;
rolls.push_back(i);
frame.Roll = rolls;
return frame;
}
Frame NumberUtils::createFreeFrame(int i, int j){
struct Frame frame = Frame();
vector<int> rolls;
rolls.push_back(i);
rolls.push_back(j);
frame.Roll = rolls;
return frame;
}
Frame NumberUtils::createBonusFrame(int i, int j, int k){
struct Frame frame = Frame();
vector<int> rolls;
rolls.push_back(i);
rolls.push_back(j);
rolls.push_back(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,116 +1,132 @@
#include "../include/PrintFrames.h" #include "../include/PrintFrames.h"
#include <iostream> #include <iostream>
#include <sstream>
void PrintFrames::printHeader(vector<int> rolls){ void PrintFrames::printResult(const vector<Frame>& frames) {
int frame = 0; printHeader();
int i = 1; cout << parseValue(frames);
}
while (true) { void PrintFrames::printHeader() {
cout << "|"; cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl;
}
string PrintFrames::parseValue(const vector<Frame>& frames) {
stringstream ss;
// There can only be 10 frames.
for (int i = 0; i < 10; i++) {
ss << "|";
// Strike // Strike
if (rolls[frame] == 10) { if (frames[i].Roll[0] == 10) {
// If we're on our last frame, and roll a strike, we're given two bonus rolls. if (frames[i].Roll.size() == 3) {
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) { ss << "X, ";
cout << " f" << i << " |" << endl;
break; 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] << " ";
} }
cout << " f" << i << " "; continue;
frame += 1; }
// 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;
} }
// Spare // Spare
else if (rolls[frame] + rolls[frame + 1] == 10) { if (frames[i].Roll[0] + frames[i].Roll[1] == 10) {
// If we're on our last frame, and roll a spare, we're given a bonus roll. if (frames[i].Roll.size() == 3) {
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10) if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 10 && frames[i].Roll[2] != 10) {
{ if (frames[i].Roll[2] == 0) {
cout << " f" << i << " |" << endl; ss << "-, /, -";
break; } 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];
} }
cout << " f" << i << " "; continue;
frame += 2;
} }
// Open Frame ss << frames[i].Roll[0] << ", /";
else { continue;
cout << " f" << i << " ";
frame += 2;
} }
++i; // 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];
}
}
}
// No more frames ss << "|";
if (frame == rolls.size()) { ss << endl;
cout << "|" << endl;
break; return ss.str();
}
}
}
void PrintFrames::printValue(vector<int> rolls){
int frame = 0;
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,45 +1,42 @@
#include "../include/ScoreCalculator.h" #include "../include/ScoreCalculator.h"
#include "../include/Frame.h"
int ScoreCalculator::getScore(vector<int> rolls) { int ScoreCalculator::getScore(const vector<Frame>& frames) {
int score = 0; int score = 0;
int frame = 0;
while (true) { // There can only be 10 frames.
for (int i = 0; i < 10; i++) {
// Strike // Strike
if (rolls[frame] == 10) { if (frames[i].Roll[0] == 10) {
// If we're on our last frame, and roll a strike, we're given two bonus rolls. if (frames[i].Roll.size() == 3) {
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) { score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2];
score += 10 + rolls[frame + 1] + rolls[frame + 2]; continue;
break;
} }
score += 10 + rolls[frame + 1] + rolls[frame + 2]; // If the next frame has 3 or 2 rolls, then take those 2.
frame += 1; if (frames[i + 1].Roll.size() > 1) {
score += frames[i].Roll[0] + frames[i + 1].Roll[0] + frames[i + 1].Roll[1];
continue;
}
// Both next frames, that are not the last, are 10.
score += frames[i].Roll[0] + frames[i + 1].Roll[0] + frames[i + 2].Roll[0];
continue;
} }
// Spare // Spare
else if (rolls[frame] + rolls[frame + 1] == 10) { if (frames[i].Roll[0] + frames[i].Roll[1] == 10) {
// If we're on our last frame, and roll a spare, we're given a bonus roll. if (frames[i].Roll.size() == 3) {
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10) score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2];
{ continue;
score += 10 + rolls[frame + 1];
break;
} }
score += 10 + rolls[frame + 2]; score += 10 + frames[i + 1].Roll[0];
frame += 2; continue;
} }
// Open Frame // Open frame
else { score += frames[i].Roll[0] + frames[i].Roll[1];
score += rolls[frame] + rolls[frame + 1];
frame += 2;
}
// No more frames
if (frame == rolls.size()) {
break;
}
} }
return score; return score;

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,18 +13,23 @@ 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);
PrintFrames::printHeader(rolls); if (!NumberUtils::validateRolls(rolls)) {
PrintFrames::printValue(rolls); return 0;
}
int score = ScoreCalculator::getScore(rolls); vector<Frame> frames = NumberUtils::createFrames(rolls);
PrintFrames::printResult(frames);
int score = ScoreCalculator::getScore(frames);
cout << "Score: " << score << endl; cout << "Score: " << score << endl;
return 0; return 0;

1
test rolls/rolls2.txt Normal file
View File

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

1
test rolls/rolls3.txt Normal file
View File

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

1
test rolls/rolls4.txt Normal file
View File

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

1
test rolls/rolls5.txt Normal file
View File

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