Author SHA1 Message Date
owner 05ba979f58 Testing roll combination validation 2025-03-26 12:44:11 +01:00
owner 8fdd4c18fd Validate if roll is higher than 10, and lower than 0 2025-03-26 12:05:45 +01:00
owner c14a29a070 Add comment 2025-03-19 11:58:53 +01:00
owner 3420815c74 Update README.md 2025-03-19 10:56:40 +00:00
owner f076e0e35c Remove unused text file 2025-03-19 11:51:10 +01:00
owner ceddebcd80 Merge pull request 'Remove unused folder' (#5) from cleanup into main
Reviewed-on: #5
2025-03-19 10:50:03 +00:00
owner 8ed07d0334 Remove unused folder 2025-03-19 11:49:05 +01:00
owner b285704ae1 Merge pull request 'cleanup' (#4) from cleanup into main
Reviewed-on: #4
2025-03-19 10:48:15 +00:00
owner 3fd7328f66 Add comments 2025-03-19 11:46:19 +01:00
owner 5b61f0e5d9 Follow c++ naming convention 2025-03-19 11:21:04 +01:00
owner 5f8cabb56a Update README 2025-03-19 10:30:20 +01:00
owner 67d8fa370d Fix include folder name, update dockerfile and compile the executable inside the docker container 2025-03-19 10:26:16 +01:00
owner 5ec8a150cd Change gitignore to remove bin folder 2025-03-18 20:54:33 +01:00
owner de703651f7 Remove executable 2025-03-18 20:53:04 +01:00
owner d655c852fa Merge pull request 'format-output' (#3) from format-output into main
Reviewed-on: #3
2025-03-18 19:25:20 +00:00
owner 48a2c065f0 Changed the code formatting a bit 2025-03-18 20:25:00 +01:00
owner 959a55d4d0 Print formated output with the frames and rolls now works 2025-03-18 20:00:59 +01:00
owner e54b191f7a Merge pull request 'calculate-score' (#2) from calculate-score into main
Reviewed-on: #2
2025-03-18 15:44:10 +00:00
21 changed files with 274 additions and 102 deletions
+2
View File
@@ -86,3 +86,5 @@ dkms.conf
*.out
*.app
# Custom
bin/*
+6 -2
View File
@@ -1,7 +1,11 @@
FROM gcc:latest
RUN g++ src/*.cpp -o Bowling
WORKDIR /app
COPY bin/Bowling /app/
COPY rolls.txt /app/
CMD ["./Bowling"]
#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();
~FileHelper();
static bool FileExists(char *path);
};
#endif
-16
View File
@@ -1,16 +0,0 @@
#ifndef FILEREADER_H
#define FILEREADER_H
#include <string>
#include <vector>
class FileReader {
public:
FileReader();
~FileReader();
static std::string GetFile(char *path);
};
#endif
-18
View File
@@ -1,18 +0,0 @@
#ifndef NUMBERHELPER_H
#define NUMBERHELPER_H
#include <string>
#include <vector>
using namespace std;
class NumberHelper {
public:
NumberHelper();
~NumberHelper();
static vector<int> GetNumbers(string csv);
};
#endif
+7 -1
View File
@@ -1,3 +1,9 @@
# Bowling
If you want to try out the project, please use git clone with the HTTPS link to the repo, and not the SSH link, as only the owner can pull and push via SSH.
To run the project, simply follow these steps:
1. `git clone https://git.rbwr.dk/owner/Bowling.git`
2. `cd Bowling/`
2. `docker build -t bowling .`
3. `docker run --rm bowling`
To change bowling score, simply edit the `rolls.txt` file, and build the docker image again.
+13
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
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
+20
View File
@@ -0,0 +1,20 @@
#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);
static bool validateRolls(vector<int> rolls);
};
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef PRINTFRAMES_H
#define PRINTFRAMES_H
#include <vector>
using namespace std;
class PrintFrames {
public:
PrintFrames() = default;
~PrintFrames() = default;
static void printHeader(vector<int> rolls);
static void printValue(vector<int> rolls);
};
#endif
@@ -8,11 +8,11 @@ using namespace std;
class ScoreCalculator {
public:
ScoreCalculator();
ScoreCalculator() = default;
~ScoreCalculator();
~ScoreCalculator() = default;
static int GetScore(vector<int> rolls);
static int getScore(vector<int> rolls);
};
#endif
-2
View File
@@ -1,3 +1 @@
2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6, 10, 3, 2
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
-1
View File
@@ -1 +0,0 @@
10, 3, 2, 2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6
+1
View File
@@ -0,0 +1 @@
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
+1
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
+2 -5
View File
@@ -1,10 +1,7 @@
#include "../Include/FileHelper.h"
#include "../include/FileHelper.h"
#include <sys/stat.h>
FileHelper::FileHelper() {};
FileHelper::~FileHelper() {};
bool FileHelper::FileExists(char *path){
bool FileHelper::fileExists(char *path){
struct stat s;
// Check if file exists, and if it isn't a folder.
+6 -8
View File
@@ -1,16 +1,14 @@
#include "../Include/FileReader.h"
#include "../include/FileReader.h"
#include <string>
#include <fstream>
FileReader::FileReader() {};
FileReader::~FileReader() {};
string FileReader::getFile(char *path){
ifstream file(path);
std::string FileReader::GetFile(char *path){
std::ifstream file(path);
string line;
std::string line;
std::getline(file, line);
// There is only one line in the file
getline(file, line);
file.close();
+35 -10
View File
@@ -1,19 +1,44 @@
#include "../Include/NumberHelper.h"
#include "../include/NumberHelper.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
NumberHelper::NumberHelper() {};
NumberHelper::~NumberHelper() {};
vector<int> NumberHelper::getRolls(string csv){
vector<int> rolls;
stringstream ss(csv);
string number;
std::vector<int> NumberHelper::GetNumbers(std::string csv){
std::vector<int> rolls;
std::stringstream ss(csv);
std::string number;
while (std::getline(ss, number, ',')) {
rolls.push_back(std::stoi(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;
}
bool NumberHelper::validateRolls(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;
}
+116
View File
@@ -0,0 +1,116 @@
#include "../include/PrintFrames.h"
#include <iostream>
void PrintFrames::printHeader(vector<int> rolls){
int frame = 0;
int i = 1;
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) {
cout << " f" << i << " |" << endl;
break;
}
cout << " f" << i << " ";
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 << " f" << i << " |" << endl;
break;
}
cout << " f" << i << " ";
frame += 2;
}
// 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;
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;
}
}
}
+9 -10
View File
@@ -1,16 +1,13 @@
#include "../Include/ScoreCalculator.h"
#include "../include/ScoreCalculator.h"
ScoreCalculator::ScoreCalculator() {};
ScoreCalculator::~ScoreCalculator() {};
int ScoreCalculator::GetScore(vector<int> rolls) {
// Calculate score
int ScoreCalculator::getScore(vector<int> rolls) {
int score = 0;
int frame = 0;
while (true) {
// Strike
if (rolls[frame] == 10) {
// If we're on our last frame, and roll a 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) {
score += 10 + rolls[frame + 1] + rolls[frame + 2];
break;
@@ -18,11 +15,11 @@ int ScoreCalculator::GetScore(vector<int> rolls) {
score += 10 + rolls[frame + 1] + rolls[frame + 2];
frame += 1;
continue;
}
// Spare
if (rolls[frame] + rolls[frame + 1] == 10) {
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)
{
score += 10 + rolls[frame + 1];
@@ -31,13 +28,15 @@ int ScoreCalculator::GetScore(vector<int> rolls) {
score += 10 + rolls[frame + 2];
frame += 2;
continue;
}
// Open Frame
else {
score += rolls[frame] + rolls[frame + 1];
frame += 2;
}
// No more frames
if (frame == rolls.size()) {
break;
}
+15 -11
View File
@@ -1,9 +1,10 @@
#include <iostream>
#include <string>
#include "../Include/FileReader.h"
#include "../Include/FileHelper.h"
#include "../Include/NumberHelper.h"
#include "../Include/ScoreCalculator.h"
#include "../include/FileReader.h"
#include "../include/FileHelper.h"
#include "../include/NumberHelper.h"
#include "../include/ScoreCalculator.h"
#include "../include/PrintFrames.h"
using namespace std;
@@ -13,20 +14,23 @@ int main(int argc, char *argv[]) {
return 0;
}
if (!FileHelper::FileExists(argv[1])) {
if (!FileHelper::fileExists(argv[1])) {
cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
string file = FileReader::GetFile(argv[1]);
string file = FileReader::getFile(argv[1]);
vector<int> rolls = NumberHelper::getRolls(file);
vector<int> rolls = NumberHelper::GetNumbers(file);
if (!NumberHelper::validateRolls(rolls)) {
return 0;
}
int score = ScoreCalculator::GetScore(rolls);
PrintFrames::printHeader(rolls);
PrintFrames::printValue(rolls);
cout << "Score: " << score << std::endl;
int score = ScoreCalculator::getScore(rolls);
cout << "Score: " << score << endl;
return 0;
}