Compare commits

..

6 Commits

17 changed files with 55 additions and 126 deletions

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

View File

@ -1,16 +1,16 @@
#ifndef FILEUTIL_H
#define FILEUTIL_H
#ifndef FILEUTILS_H
#define FILEUTILS_H
#include <string>
#include <vector>
using namespace std;
class FileUtil {
class FileUtils {
public:
FileUtil() = default;
FileUtils() = default;
~FileUtil() = default;
~FileUtils() = default;
static string getFile(char *path);
static bool fileExists(char *path);

View File

@ -1,5 +1,5 @@
#ifndef NUMBERHELPER_H
#define NUMBERHELPER_H
#ifndef NUMBERUTILS_H
#define NUMBERUTILS_H
#include <string>
#include <vector>
@ -7,15 +7,15 @@
using namespace std;
class NumberHelper {
class NumberUtils {
public:
NumberHelper() = default;
NumberUtils() = default;
~NumberHelper() = default;
~NumberUtils() = default;
static vector<int> getRolls(string csv);
static bool validateRolls(vector<int> rolls);
static vector<Frame> createFrames(vector<int> rolls);
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);

View File

@ -13,9 +13,12 @@ class PrintFrames {
~PrintFrames() = default;
static void printResult(const vector<Frame>& frames);
private:
static void printHeader();
static void printValue(string values);
static string parseValue(vector<Frame> frames);
static string parseValue(const vector<Frame>& frames);
};
#endif

View File

@ -13,7 +13,7 @@ class ScoreCalculator {
~ScoreCalculator() = default;
static int getScore(vector<Frame> rolls);
static int getScore(const vector<Frame>& frames);
};
#endif

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,16 +0,0 @@
#include "../include/FileReader.h"
#include <string>
#include <fstream>
string FileReader::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,9 +1,9 @@
#include "../include/FileUtil.h"
#include "../include/FileUtils.h"
#include <string>
#include <fstream>
#include <sys/stat.h>
bool FileUtil::fileExists(char *path){
bool FileUtils::fileExists(char *path){
struct stat s;
// Check if file exists, and if it isn't a folder.
@ -14,7 +14,7 @@ bool FileUtil::fileExists(char *path){
return false;
}
string FileUtil::getFile(char *path){
string FileUtils::getFile(char *path){
ifstream file(path);
string line;

View File

@ -1,11 +1,11 @@
#include "../include/NumberHelper.h"
#include "../include/NumberUtils.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
vector<int> NumberHelper::getRolls(string csv){
vector<int> NumberUtils::getRolls(const string& csv){
vector<int> rolls;
stringstream ss(csv);
string number;
@ -23,7 +23,7 @@ vector<int> NumberHelper::getRolls(string csv){
return rolls;
}
bool NumberHelper::validateRolls(vector<int> rolls){
bool NumberUtils::validateRolls(const vector<int>& rolls){
int rollCount = rolls.size();
for (int i = 0; i < rollCount; ++i) {
@ -49,16 +49,14 @@ bool NumberHelper::validateRolls(vector<int> rolls){
return true;
}
vector<Frame> NumberHelper::createFrames(vector<int> rolls){
vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
vector<Frame> frame;
int roll = 0;
while (roll != rolls.size())
{
while (roll != rolls.size()) {
// Strike
if (rolls[roll] == 10)
{
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]));
@ -71,11 +69,9 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
}
// Spare
else if (rolls[roll] + rolls[roll + 1] == 10)
{
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)
{
if (roll + 3 == rolls.size()) {
frame.push_back(createBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
break;
}
@ -86,8 +82,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
}
// Open Frame
else
{
else {
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2;
@ -97,7 +92,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
return frame;
}
Frame NumberHelper::createStrikeFrame(int i){
Frame NumberUtils::createStrikeFrame(int i){
struct Frame frame = Frame();
vector<int> rolls;
@ -109,7 +104,7 @@ Frame NumberHelper::createStrikeFrame(int i){
return frame;
}
Frame NumberHelper::createFreeFrame(int i, int j){
Frame NumberUtils::createFreeFrame(int i, int j){
struct Frame frame = Frame();
vector<int> rolls;
@ -122,7 +117,7 @@ Frame NumberHelper::createFreeFrame(int i, int j){
return frame;
}
Frame NumberHelper::createBonusFrame(int i, int j, int k){
Frame NumberUtils::createBonusFrame(int i, int j, int k){
struct Frame frame = Frame();
vector<int> rolls;
@ -136,7 +131,7 @@ Frame NumberHelper::createBonusFrame(int i, int j, int k){
return frame;
}
bool NumberHelper::isNumber(const std::string& str){
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;
}

View File

@ -2,15 +2,16 @@
#include <iostream>
#include <sstream>
void PrintFrames::printResult(const vector<Frame>& frames) {
printHeader();
cout << parseValue(frames);
}
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) {
string PrintFrames::parseValue(const vector<Frame>& frames) {
stringstream ss;
// There can only be 10 frames.
@ -30,14 +31,12 @@ string PrintFrames::parseValue(vector<Frame> frames) {
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] << ", - ";
}
@ -59,15 +58,12 @@ string PrintFrames::parseValue(vector<Frame> frames) {
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 {
} else {
ss << "X, ";
}
}
@ -103,6 +99,7 @@ string PrintFrames::parseValue(vector<Frame> frames) {
} else {
ss << frames[i].Roll[0] << ", /, " << frames[i].Roll[2];
}
continue;
}
@ -120,12 +117,9 @@ string PrintFrames::parseValue(vector<Frame> frames) {
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 {
} else {
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1];
}
}

View File

@ -1,7 +1,7 @@
#include "../include/ScoreCalculator.h"
#include "../include/Frame.h"
int ScoreCalculator::getScore(vector<Frame> frames) {
int ScoreCalculator::getScore(const vector<Frame>& frames) {
int score = 0;
// There can only be 10 frames.

View File

@ -1,7 +1,7 @@
#include <iostream>
#include <string>
#include "../include/FileUtil.h"
#include "../include/NumberHelper.h"
#include "../include/FileUtils.h"
#include "../include/NumberUtils.h"
#include "../include/ScoreCalculator.h"
#include "../include/PrintFrames.h"
@ -13,24 +13,21 @@ int main(int argc, char *argv[]) {
return 0;
}
if (!FileUtil::fileExists(argv[1])) {
if (!FileUtils::fileExists(argv[1])) {
cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
string file = FileUtil::getFile(argv[1]);
vector<int> rolls = NumberHelper::getRolls(file);
string file = FileUtils::getFile(argv[1]);
vector<int> rolls = NumberUtils::getRolls(file);
if (!NumberHelper::validateRolls(rolls)) {
if (!NumberUtils::validateRolls(rolls)) {
return 0;
}
vector<Frame> frames = NumberHelper::createFrames(rolls);
vector<Frame> frames = NumberUtils::createFrames(rolls);
string values = PrintFrames::parseValue(frames);
PrintFrames::printHeader();
PrintFrames::printValue(values);
PrintFrames::printResult(frames);
int score = ScoreCalculator::getScore(frames);
cout << "Score: " << score << endl;