Compare commits
35
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa141a8f22 | ||
|
|
2e69b2e4cf | ||
|
|
531732c524 | ||
|
|
002d8a0804 | ||
|
|
8222f5e0b3 | ||
|
|
b8e2e8e2fd | ||
|
|
1228b7150a | ||
|
|
4a09cbbb33 | ||
|
|
0ebfb2b86d | ||
|
|
76284c3951 | ||
|
|
aa81717fd9 | ||
|
|
7d9da3cc50 | ||
|
|
61b00d40db | ||
|
|
f5e96e7704 | ||
|
|
05ba979f58 | ||
|
|
8fdd4c18fd | ||
|
|
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
|
*.out
|
||||||
*.app
|
*.app
|
||||||
|
|
||||||
|
# Custom
|
||||||
|
bin/*
|
||||||
|
|||||||
+13
-3
@@ -1,7 +1,17 @@
|
|||||||
FROM gcc:latest
|
FROM gcc:latest AS build
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY bin/Bowling /app/
|
COPY include/* /app/include/
|
||||||
|
COPY src/* /app/src/
|
||||||
|
|
||||||
CMD ["./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"]
|
||||||
|
|||||||
@@ -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
|
# 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 FILEUTIL_H
|
||||||
|
#define FILEUTIL_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class FileUtil {
|
||||||
|
public:
|
||||||
|
FileUtil() = default;
|
||||||
|
|
||||||
|
~FileUtil() = default;
|
||||||
|
|
||||||
|
static string getFile(char *path);
|
||||||
|
static bool fileExists(char *path);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef FRAME_H
|
||||||
|
#define FRAME_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct Frame {
|
||||||
|
vector<int> Roll;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#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);
|
||||||
|
static bool isNumber(const std::string& str);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#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 printHeader();
|
||||||
|
static void printValue(string values);
|
||||||
|
static string parseValue(vector<Frame> frames);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef SCORECALCULATOR_H
|
||||||
|
#define SCORECALCULATOR_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include "../include/Frame.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class ScoreCalculator {
|
||||||
|
public:
|
||||||
|
ScoreCalculator() = default;
|
||||||
|
|
||||||
|
~ScoreCalculator() = default;
|
||||||
|
|
||||||
|
static int getScore(vector<Frame> rolls);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 10, 10
|
||||||
+2
-5
@@ -1,10 +1,7 @@
|
|||||||
#include "../Include/FileHelper.h"
|
#include "../include/FileHelper.h"
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
FileHelper::FileHelper() {};
|
bool FileHelper::fileExists(char *path){
|
||||||
FileHelper::~FileHelper() {};
|
|
||||||
|
|
||||||
bool FileHelper::FileExists(char *path){
|
|
||||||
struct stat s;
|
struct stat s;
|
||||||
|
|
||||||
// Check if file exists, and if it isn't a folder.
|
// 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 <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
FileReader::FileReader() {};
|
string FileReader::getFile(char *path){
|
||||||
FileReader::~FileReader() {};
|
ifstream file(path);
|
||||||
|
|
||||||
std::string FileReader::GetFile(char *path){
|
string line;
|
||||||
std::ifstream file(path);
|
|
||||||
|
|
||||||
std::string line;
|
// There is only one line in the file
|
||||||
|
getline(file, line);
|
||||||
std::getline(file, line);
|
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#include "../include/FileUtil.h"
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
bool FileUtil::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 FileUtil::getFile(char *path){
|
||||||
|
ifstream file(path);
|
||||||
|
|
||||||
|
string line;
|
||||||
|
|
||||||
|
// There is only one line in the file
|
||||||
|
getline(file, line);
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
return line;
|
||||||
|
}
|
||||||
+135
-9
@@ -1,19 +1,145 @@
|
|||||||
#include "../Include/NumberHelper.h"
|
#include "../include/NumberHelper.h"
|
||||||
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
NumberHelper::NumberHelper() {};
|
vector<int> NumberHelper::getRolls(string csv){
|
||||||
NumberHelper::~NumberHelper() {};
|
vector<int> rolls;
|
||||||
|
stringstream ss(csv);
|
||||||
|
string number;
|
||||||
|
|
||||||
std::vector<int> NumberHelper::GetNumbers(const std::string csv){
|
// Get number from the CSV file, split by comma, and convert to int.
|
||||||
std::vector<int> rolls;
|
while (getline(ss, number, ',')) {
|
||||||
std::stringstream ss(csv);
|
if (!isNumber(number)) {
|
||||||
std::string number;
|
cerr << number << " is not a number.";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
while (std::getline(ss, number, ',')) {
|
rolls.push_back(stoi(number));
|
||||||
rolls.push_back(std::stoi(number));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return rolls;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<Frame> NumberHelper::createFrames(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() && rolls[roll] + rolls[roll + 1] == 10)
|
||||||
|
{
|
||||||
|
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 NumberHelper::createStrikeFrame(int i){
|
||||||
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
|
vector<int> rolls;
|
||||||
|
|
||||||
|
rolls.push_back(i);
|
||||||
|
|
||||||
|
frame.Roll = rolls;
|
||||||
|
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
Frame NumberHelper::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 NumberHelper::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 NumberHelper::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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
#include "../include/PrintFrames.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
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) {
|
||||||
|
stringstream ss;
|
||||||
|
|
||||||
|
// There can only be 10 frames.
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
ss << "|";
|
||||||
|
// Strike
|
||||||
|
if (frames[i].Roll[0] == 10) {
|
||||||
|
if (frames[i].Roll.size() == 3) {
|
||||||
|
ss << "X, ";
|
||||||
|
|
||||||
|
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] << " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
if (frames[i].Roll[0] + frames[i].Roll[1] == 10) {
|
||||||
|
if (frames[i].Roll.size() == 3) {
|
||||||
|
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[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;
|
||||||
|
}
|
||||||
|
|
||||||
|
ss << frames[i].Roll[0] << ", /";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ss << "|";
|
||||||
|
ss << endl;
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
#include "../include/ScoreCalculator.h"
|
||||||
|
#include "../include/Frame.h"
|
||||||
|
|
||||||
|
int ScoreCalculator::getScore(vector<Frame> frames) {
|
||||||
|
int score = 0;
|
||||||
|
|
||||||
|
// There can only be 10 frames.
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
// Strike
|
||||||
|
if (frames[i].Roll[0] == 10) {
|
||||||
|
if (frames[i].Roll.size() == 3) {
|
||||||
|
score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the next frame has 3 or 2 rolls, then take those 2.
|
||||||
|
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
|
||||||
|
if (frames[i].Roll[0] + frames[i].Roll[1] == 10) {
|
||||||
|
if (frames[i].Roll.size() == 3) {
|
||||||
|
score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
score += 10 + frames[i + 1].Roll[0];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open frame
|
||||||
|
score += frames[i].Roll[0] + frames[i].Roll[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
+23
-11
@@ -1,27 +1,39 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "../Include/FileReader.h"
|
#include "../include/FileUtil.h"
|
||||||
#include "../Include/FileHelper.h"
|
#include "../include/NumberHelper.h"
|
||||||
#include "../Include/NumberHelper.h"
|
#include "../include/ScoreCalculator.h"
|
||||||
|
#include "../include/PrintFrames.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
std::cerr << "Please provide a CSV formatted file.";
|
cerr << "Please provide a CSV formatted file.";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!FileHelper::FileExists(argv[1])) {
|
if (!FileUtil::fileExists(argv[1])) {
|
||||||
std::cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string file = FileReader::GetFile(argv[1]);
|
string file = FileUtil::getFile(argv[1]);
|
||||||
|
vector<int> rolls = NumberHelper::getRolls(file);
|
||||||
|
|
||||||
std::vector<int> numbers = NumberHelper::GetNumbers(file);
|
if (!NumberHelper::validateRolls(rolls)) {
|
||||||
|
return 0;
|
||||||
for(int i = 0; i < numbers.size(); i++) {
|
|
||||||
std::cout << numbers[i] << ' ';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<Frame> frames = NumberHelper::createFrames(rolls);
|
||||||
|
|
||||||
|
string values = PrintFrames::parseValue(frames);
|
||||||
|
|
||||||
|
PrintFrames::printHeader();
|
||||||
|
PrintFrames::printValue(values);
|
||||||
|
|
||||||
|
int score = ScoreCalculator::getScore(frames);
|
||||||
|
cout << "Score: " << score << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user