Merge pull request 'main' (#10) from main into simplify-printing
Reviewed-on: #10
This commit is contained in:
commit
8222f5e0b3
19
include/FileUtil.h
Normal file
19
include/FileUtil.h
Normal file
@ -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
|
@ -18,9 +18,10 @@ 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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
28
src/FileUtil.cpp
Normal file
28
src/FileUtil.cpp
Normal file
@ -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;
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
#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;
|
||||||
@ -11,6 +12,11 @@ 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,11 +61,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;
|
||||||
}
|
}
|
||||||
@ -70,11 +76,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;
|
||||||
}
|
}
|
||||||
@ -82,7 +88,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;
|
||||||
}
|
}
|
||||||
@ -91,7 +97,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;
|
||||||
@ -103,7 +109,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;
|
||||||
@ -116,7 +122,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;
|
||||||
@ -128,4 +134,12 @@ 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,7 +11,6 @@ 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,7 +1,6 @@
|
|||||||
#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/ScoreCalculator.h"
|
||||||
#include "../include/PrintFrames.h"
|
#include "../include/PrintFrames.h"
|
||||||
@ -14,12 +13,12 @@ int main(int argc, char *argv[]) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!FileHelper::fileExists(argv[1])) {
|
if (!FileUtil::fileExists(argv[1])) {
|
||||||
cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
string file = FileReader::getFile(argv[1]);
|
string file = FileUtil::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