Compare commits

..

No commits in common. "main" and "format-output" have entirely different histories.

27 changed files with 266 additions and 433 deletions

2
.gitignore vendored
View File

@ -86,5 +86,3 @@ dkms.conf
*.out *.out
*.app *.app
# Custom
bin/*

View File

@ -1,17 +1,7 @@
FROM gcc:latest AS build FROM gcc:latest
WORKDIR /app WORKDIR /app
COPY include/* /app/include/ COPY bin/Bowling /app/
COPY src/* /app/src/
RUN g++ src/*.cpp -o Bowling CMD ["./Bowling"]
FROM debian:sid-slim
WORKDIR /app
COPY --from=build /app/Bowling Bowling
COPY rolls.txt /app/
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();
~FileHelper();
static bool FileExists(char *path);
};
#endif

16
Include/FileReader.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef FILEREADER_H
#define FILEREADER_H
#include <string>
#include <vector>
class FileReader {
public:
FileReader();
~FileReader();
static std::string GetFile(char *path);
};
#endif

18
Include/NumberHelper.h Normal file
View File

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

18
Include/PrintFrames.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef PRINTFRAMES_H
#define PRINTFRAMES_H
#include <vector>
using namespace std;
class PrintFrames {
public:
PrintFrames();
~PrintFrames();
static void PrintHeader(vector<int> rolls);
static void PrintValue(vector<int> rolls);
};
#endif

View File

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

View File

@ -1,9 +1,3 @@
# Bowling # Bowling
To run the project, simply follow these steps: 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.
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.

BIN
bin/Bowling Executable file

Binary file not shown.

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

View File

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

@ -1,24 +0,0 @@
#ifndef PRINTFRAMES_H
#define PRINTFRAMES_H
#include <vector>
#include <string>
#include "../include/Frame.h"
using namespace std;
class PrintFrames {
public:
PrintFrames() = default;
~PrintFrames() = default;
static void printResult(const vector<Frame>& frames);
private:
static void printHeader();
static string parseValue(const vector<Frame>& frames);
};
#endif

View File

@ -1 +1,4 @@
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
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 1
2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6, 10, 3, 2 2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6, 10, 3, 2

1
rolls1.txt Normal file
View File

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

16
src/FileHelper.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "../Include/FileHelper.h"
#include <sys/stat.h>
FileHelper::FileHelper() {};
FileHelper::~FileHelper() {};
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;
}

18
src/FileReader.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "../Include/FileReader.h"
#include <string>
#include <fstream>
FileReader::FileReader() {};
FileReader::~FileReader() {};
std::string FileReader::GetFile(char *path){
std::ifstream file(path);
std::string line;
std::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;
}

19
src/NumberHelper.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "../Include/NumberHelper.h"
#include <string>
#include <sstream>
#include <vector>
NumberHelper::NumberHelper() {};
NumberHelper::~NumberHelper() {};
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));
}
return rolls;
}

View File

@ -1,140 +0,0 @@
#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,132 +1,116 @@
#include "../include/PrintFrames.h" #include "../Include/PrintFrames.h"
#include <iostream> #include <iostream>
#include <sstream>
void PrintFrames::printResult(const vector<Frame>& frames) { PrintFrames::PrintFrames() {};
printHeader(); PrintFrames::~PrintFrames() {};
cout << parseValue(frames);
}
void PrintFrames::printHeader() { void PrintFrames::PrintHeader(vector<int> rolls){
cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl; int frame = 0;
} int i = 1;
string PrintFrames::parseValue(const vector<Frame>& frames) { while (true) {
stringstream ss; cout << "|";
// There can only be 10 frames.
for (int i = 0; i < 10; i++) {
ss << "|";
// Strike // Strike
if (frames[i].Roll[0] == 10) { if (rolls[frame] == 10) {
if (frames[i].Roll.size() == 3) { // If we're on our last frame, and roll a 10.
ss << "X, "; if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
cout << " f" << i << " |" << endl;
if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 10) { break;
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; cout << " f" << i << " ";
} 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
if (frames[i].Roll[0] + frames[i].Roll[1] == 10) { else if (rolls[frame] + rolls[frame + 1] == 10) {
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];
} }
continue; cout << " f" << i << " ";
frame += 2;
} }
ss << frames[i].Roll[0] << ", /"; // Open Frame
continue; else {
cout << " f" << i << " ";
frame += 2;
} }
// Open frame ++i;
if (i == 9) {
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1] << " "; if (frame == rolls.size()) {
} else { cout << "|" << endl;
if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 0) { break;
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];
} }
} }
} }
ss << "|"; void PrintFrames::PrintValue(vector<int> rolls){
ss << endl; int frame = 0;
return ss.str(); while (true) {
cout << "|";
// Strike
if (rolls[frame] == 10) {
// If we're on our last frame, and roll a 10.
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 (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,42 +1,46 @@
#include "../include/ScoreCalculator.h" #include "../Include/ScoreCalculator.h"
#include "../include/Frame.h"
int ScoreCalculator::getScore(const vector<Frame>& frames) { ScoreCalculator::ScoreCalculator() {};
ScoreCalculator::~ScoreCalculator() {};
int ScoreCalculator::GetScore(vector<int> rolls) {
int score = 0; int score = 0;
int frame = 0;
// There can only be 10 frames. while (true) {
for (int i = 0; i < 10; i++) {
// Strike // Strike
if (frames[i].Roll[0] == 10) { if (rolls[frame] == 10) {
if (frames[i].Roll.size() == 3) { // If we're on our last frame, and roll a 10.
score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2]; if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
continue; score += 10 + rolls[frame + 1] + rolls[frame + 2];
break;
} }
// If the next frame has 3 or 2 rolls, then take those 2. score += 10 + rolls[frame + 1] + rolls[frame + 2];
if (frames[i + 1].Roll.size() > 1) { frame += 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; continue;
} }
// Spare // Spare
if (frames[i].Roll[0] + frames[i].Roll[1] == 10) { if (rolls[frame] + rolls[frame + 1] == 10) {
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]; {
score += 10 + rolls[frame + 1];
break;
}
score += 10 + rolls[frame + 2];
frame += 2;
continue; continue;
} }
score += 10 + frames[i + 1].Roll[0]; // Open Frame
continue; score += rolls[frame] + rolls[frame + 1];
} frame += 2;
// Open frame if (frame == rolls.size()) {
score += frames[i].Roll[0] + frames[i].Roll[1]; break;
}
} }
return score; return score;

View File

@ -1,9 +1,10 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "../include/FileUtils.h" #include "../Include/FileReader.h"
#include "../include/NumberUtils.h" #include "../Include/FileHelper.h"
#include "../include/ScoreCalculator.h" #include "../Include/NumberHelper.h"
#include "../include/PrintFrames.h" #include "../Include/ScoreCalculator.h"
#include "../Include/PrintFrames.h"
using namespace std; using namespace std;
@ -13,23 +14,18 @@ int main(int argc, char *argv[]) {
return 0; return 0;
} }
if (!FileUtils::fileExists(argv[1])) { if (!FileHelper::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 = FileReader::GetFile(argv[1]);
vector<int> rolls = NumberUtils::getRolls(file); vector<int> rolls = NumberHelper::GetNumbers(file);
if (!NumberUtils::validateRolls(rolls)) { PrintFrames::PrintHeader(rolls);
return 0; PrintFrames::PrintValue(rolls);
}
vector<Frame> frames = NumberUtils::createFrames(rolls); int score = ScoreCalculator::GetScore(rolls);
PrintFrames::printResult(frames);
int score = ScoreCalculator::getScore(frames);
cout << "Score: " << score << endl; cout << "Score: " << score << endl;
return 0; return 0;

View File

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

View File

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

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