Compare commits
No commits in common. "8222f5e0b30ab615ffa529cdf2d0b20ce23a8b9a" and "76284c3951146654c55abc9814113acb67507d6e" have entirely different histories.
8222f5e0b3
...
76284c3951
@ -1,19 +0,0 @@
|
|||||||
#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
|
|
@ -18,10 +18,9 @@ class NumberHelper {
|
|||||||
static vector<Frame> createFrames(vector<int> rolls);
|
static vector<Frame> createFrames(vector<int> rolls);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Frame createStrikeFrame(int i);
|
static Frame CreateStrikeFrame(int i);
|
||||||
static Frame createFreeFrame(int i, int j);
|
static Frame CreateFreeFrame(int i, int j);
|
||||||
static Frame createBonusFrame(int i, int j, int k);
|
static Frame CreateBonusFrame(int i, int j, int k);
|
||||||
static bool isNumber(const std::string& str);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
@ -3,7 +3,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cctype>
|
|
||||||
|
|
||||||
vector<int> NumberHelper::getRolls(string csv){
|
vector<int> NumberHelper::getRolls(string csv){
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -12,11 +11,6 @@ vector<int> NumberHelper::getRolls(string csv){
|
|||||||
|
|
||||||
// Get number from the CSV file, split by comma, and convert to int.
|
// Get number from the CSV file, split by comma, and convert to int.
|
||||||
while (getline(ss, number, ',')) {
|
while (getline(ss, number, ',')) {
|
||||||
if (!isNumber(number)) {
|
|
||||||
cerr << number << " is not a number.";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
rolls.push_back(stoi(number));
|
rolls.push_back(stoi(number));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,11 +55,11 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
|
|||||||
{
|
{
|
||||||
// If we're on our last frame, and roll a strike, we're given two bonus rolls.
|
// 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) {
|
if (roll + 3 == rolls.size() && rolls[roll - 3] == 10) {
|
||||||
frame.push_back(createBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
|
frame.push_back(CreateBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
frame.push_back(createStrikeFrame(10));
|
frame.push_back(CreateStrikeFrame(10));
|
||||||
|
|
||||||
roll += 1;
|
roll += 1;
|
||||||
}
|
}
|
||||||
@ -76,11 +70,11 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
|
|||||||
// If we're on our last frame, and roll a spare, we're given a bonus roll.
|
// 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() && rolls[roll] + rolls[roll + 1] == 10)
|
||||||
{
|
{
|
||||||
frame.push_back(createBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
|
frame.push_back(CreateBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
|
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
|
||||||
|
|
||||||
roll += 2;
|
roll += 2;
|
||||||
}
|
}
|
||||||
@ -88,7 +82,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
|
|||||||
// Open Frame
|
// Open Frame
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
|
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
|
||||||
|
|
||||||
roll += 2;
|
roll += 2;
|
||||||
}
|
}
|
||||||
@ -97,7 +91,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame NumberHelper::createStrikeFrame(int i){
|
Frame NumberHelper::CreateStrikeFrame(int i){
|
||||||
struct Frame frame = Frame();
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -109,7 +103,7 @@ Frame NumberHelper::createStrikeFrame(int i){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame NumberHelper::createFreeFrame(int i, int j){
|
Frame NumberHelper::CreateFreeFrame(int i, int j){
|
||||||
struct Frame frame = Frame();
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -122,7 +116,7 @@ Frame NumberHelper::createFreeFrame(int i, int j){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame NumberHelper::createBonusFrame(int i, int j, int k){
|
Frame NumberHelper::CreateBonusFrame(int i, int j, int k){
|
||||||
struct Frame frame = Frame();
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -134,12 +128,4 @@ Frame NumberHelper::createBonusFrame(int i, int j, int k){
|
|||||||
frame.Roll = rolls;
|
frame.Roll = rolls;
|
||||||
|
|
||||||
return frame;
|
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;
|
|
||||||
}
|
}
|
@ -11,6 +11,7 @@ void PrintFrames::printValue(string value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
string PrintFrames::parseValue(vector<Frame> frames) {
|
string PrintFrames::parseValue(vector<Frame> frames) {
|
||||||
|
string value;
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
|
|
||||||
// There can only be 10 frames.
|
// There can only be 10 frames.
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "../include/FileUtil.h"
|
#include "../include/FileReader.h"
|
||||||
|
#include "../include/FileHelper.h"
|
||||||
#include "../include/NumberHelper.h"
|
#include "../include/NumberHelper.h"
|
||||||
#include "../include/ScoreCalculator.h"
|
#include "../include/ScoreCalculator.h"
|
||||||
#include "../include/PrintFrames.h"
|
#include "../include/PrintFrames.h"
|
||||||
@ -13,12 +14,12 @@ int main(int argc, char *argv[]) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!FileUtil::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 = FileUtil::getFile(argv[1]);
|
string file = FileReader::getFile(argv[1]);
|
||||||
vector<int> rolls = NumberHelper::getRolls(file);
|
vector<int> rolls = NumberHelper::getRolls(file);
|
||||||
|
|
||||||
if (!NumberHelper::validateRolls(rolls)) {
|
if (!NumberHelper::validateRolls(rolls)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user