Compare commits
19
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c14a29a070 | ||
|
|
3420815c74 | ||
|
|
f076e0e35c | ||
|
|
ceddebcd80 | ||
|
|
8ed07d0334 | ||
|
|
b285704ae1 | ||
|
|
3fd7328f66 | ||
|
|
5b61f0e5d9 | ||
|
|
5f8cabb56a | ||
|
|
67d8fa370d | ||
|
|
5ec8a150cd | ||
|
|
de703651f7 | ||
|
|
d655c852fa | ||
|
|
48a2c065f0 | ||
|
|
959a55d4d0 | ||
|
|
e54b191f7a | ||
|
|
43cd0101f3 | ||
|
|
e52031d3df | ||
|
|
b99b13383f |
@@ -86,3 +86,5 @@ dkms.conf
|
||||
*.out
|
||||
*.app
|
||||
|
||||
# Custom
|
||||
bin/*
|
||||
|
||||
+6
-2
@@ -2,6 +2,10 @@ FROM gcc:latest
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY bin/Bowling /app/
|
||||
COPY rolls.txt /app/
|
||||
COPY include/* /app/include/
|
||||
COPY src/* /app/src/
|
||||
|
||||
CMD ["./Bowling"]
|
||||
RUN g++ src/*.cpp -o Bowling
|
||||
|
||||
CMD ["./Bowling", "rolls.txt"]
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
#ifndef FILEHELPER_H
|
||||
#define FILEHELPER_H
|
||||
|
||||
class FileHelper {
|
||||
public:
|
||||
FileHelper();
|
||||
|
||||
~FileHelper();
|
||||
|
||||
static bool FileExists(char *path);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -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
|
||||
@@ -1,16 +0,0 @@
|
||||
#ifndef NUMBERHELPER_H
|
||||
#define NUMBERHELPER_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class NumberHelper {
|
||||
public:
|
||||
NumberHelper();
|
||||
|
||||
~NumberHelper();
|
||||
|
||||
static std::vector<int> GetNumbers(const std::string csv);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -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.
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef FILEHELPER_H
|
||||
#define FILEHELPER_H
|
||||
|
||||
class FileHelper {
|
||||
public:
|
||||
FileHelper() = default;
|
||||
|
||||
~FileHelper() = default;
|
||||
|
||||
static bool fileExists(char *path);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -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
|
||||
@@ -0,0 +1,19 @@
|
||||
#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
|
||||
@@ -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
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef SCORECALCULATOR_H
|
||||
#define SCORECALCULATOR_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class ScoreCalculator {
|
||||
public:
|
||||
ScoreCalculator() = default;
|
||||
|
||||
~ScoreCalculator() = default;
|
||||
|
||||
static int getScore(vector<int> rolls);
|
||||
};
|
||||
|
||||
#endif
|
||||
+2
-5
@@ -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
@@ -1,16 +1,14 @@
|
||||
#include "../Include/FileReader.h"
|
||||
#include "../include/FileReader.h"
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
FileReader::FileReader() {};
|
||||
FileReader::~FileReader() {};
|
||||
|
||||
std::string FileReader::GetFile(char *path){
|
||||
std::ifstream file(path);
|
||||
string FileReader::getFile(char *path){
|
||||
ifstream file(path);
|
||||
|
||||
std::string line;
|
||||
string line;
|
||||
|
||||
std::getline(file, line);
|
||||
// There is only one line in the file
|
||||
getline(file, line);
|
||||
|
||||
file.close();
|
||||
|
||||
|
||||
+8
-10
@@ -1,18 +1,16 @@
|
||||
#include "../Include/NumberHelper.h"
|
||||
#include "../include/NumberHelper.h"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
NumberHelper::NumberHelper() {};
|
||||
NumberHelper::~NumberHelper() {};
|
||||
|
||||
std::vector<int> NumberHelper::GetNumbers(const std::string csv){
|
||||
std::vector<int> rolls;
|
||||
std::stringstream ss(csv);
|
||||
std::string number;
|
||||
vector<int> NumberHelper::getRolls(string csv){
|
||||
vector<int> rolls;
|
||||
stringstream ss(csv);
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "../include/ScoreCalculator.h"
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
score += 10 + rolls[frame + 1] + rolls[frame + 2];
|
||||
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)
|
||||
{
|
||||
score += 10 + rolls[frame + 1];
|
||||
break;
|
||||
}
|
||||
|
||||
score += 10 + rolls[frame + 2];
|
||||
frame += 2;
|
||||
}
|
||||
|
||||
// Open Frame
|
||||
else {
|
||||
score += rolls[frame] + rolls[frame + 1];
|
||||
frame += 2;
|
||||
}
|
||||
|
||||
// No more frames
|
||||
if (frame == rolls.size()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
+18
-13
@@ -1,27 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "../Include/FileReader.h"
|
||||
#include "../Include/FileHelper.h"
|
||||
#include "../Include/NumberHelper.h"
|
||||
#include "../include/FileReader.h"
|
||||
#include "../include/FileHelper.h"
|
||||
#include "../include/NumberHelper.h"
|
||||
#include "../include/ScoreCalculator.h"
|
||||
#include "../include/PrintFrames.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if(argc < 2) {
|
||||
std::cerr << "Please provide a CSV formatted file.";
|
||||
if (argc < 2) {
|
||||
cerr << "Please provide a CSV formatted file.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(!FileHelper::FileExists(argv[1])) {
|
||||
std::cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
||||
if (!FileHelper::fileExists(argv[1])) {
|
||||
cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string file = FileReader::GetFile(argv[1]);
|
||||
|
||||
std::vector<int> numbers = NumberHelper::GetNumbers(file);
|
||||
string file = FileReader::getFile(argv[1]);
|
||||
vector<int> rolls = NumberHelper::getRolls(file);
|
||||
|
||||
for(int i = 0; i < numbers.size(); i++) {
|
||||
std::cout << numbers[i] << ' ';
|
||||
}
|
||||
PrintFrames::printHeader(rolls);
|
||||
PrintFrames::printValue(rolls);
|
||||
|
||||
int score = ScoreCalculator::getScore(rolls);
|
||||
cout << "Score: " << score << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user