Made code style more consistent, use const reference in parameters, and removed a redundant check
This commit is contained in:
parent
1e51718b04
commit
efc1b264b8
@ -1,13 +0,0 @@
|
|||||||
#ifndef FILEHELPER_H
|
|
||||||
#define FILEHELPER_H
|
|
||||||
|
|
||||||
class FileHelper {
|
|
||||||
public:
|
|
||||||
FileHelper() = default;
|
|
||||||
|
|
||||||
~FileHelper() = default;
|
|
||||||
|
|
||||||
static bool fileExists(char *path);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -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
|
|
@ -1,5 +1,5 @@
|
|||||||
#ifndef NUMBERHELPER_H
|
#ifndef NUMBERUTILS_H
|
||||||
#define NUMBERHELPER_H
|
#define NUMBERUTILS_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -7,15 +7,15 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class NumberHelper {
|
class NumberUtils {
|
||||||
public:
|
public:
|
||||||
NumberHelper() = default;
|
NumberUtils() = default;
|
||||||
|
|
||||||
~NumberHelper() = default;
|
~NumberUtils() = default;
|
||||||
|
|
||||||
static vector<int> getRolls(string csv);
|
static vector<int> getRolls(const string& csv);
|
||||||
static bool validateRolls(vector<int> rolls);
|
static bool validateRolls(const vector<int>& rolls);
|
||||||
static vector<Frame> createFrames(vector<int> rolls);
|
static vector<Frame> createFrames(const vector<int>& rolls);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Frame createStrikeFrame(int i);
|
static Frame createStrikeFrame(int i);
|
@ -14,8 +14,8 @@ class PrintFrames {
|
|||||||
~PrintFrames() = default;
|
~PrintFrames() = default;
|
||||||
|
|
||||||
static void printHeader();
|
static void printHeader();
|
||||||
static void printValue(string values);
|
static void printValue(const string& value);
|
||||||
static string parseValue(vector<Frame> frames);
|
static string parseValue(const vector<Frame>& frames);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -13,7 +13,7 @@ class ScoreCalculator {
|
|||||||
|
|
||||||
~ScoreCalculator() = default;
|
~ScoreCalculator() = default;
|
||||||
|
|
||||||
static int getScore(vector<Frame> rolls);
|
static int getScore(const vector<Frame>& frames);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
2
makefile
2
makefile
@ -1,7 +1,7 @@
|
|||||||
# Makefile for hello.c
|
# Makefile for hello.c
|
||||||
|
|
||||||
CC = g++
|
CC = g++
|
||||||
CFLAGS = -O2
|
CFLAGS = -O2 -Wanalyzer-malloc-leak
|
||||||
|
|
||||||
SRC_DIR = src
|
SRC_DIR = src
|
||||||
BUILD_DIR = build
|
BUILD_DIR = build
|
||||||
|
@ -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;
|
|
||||||
}
|
|
@ -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;
|
|
||||||
}
|
|
@ -1,11 +1,11 @@
|
|||||||
#include "../include/NumberHelper.h"
|
#include "../include/NumberUtils.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
|
||||||
vector<int> NumberHelper::getRolls(string csv){
|
vector<int> NumberUtils::getRolls(const string& csv){
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
stringstream ss(csv);
|
stringstream ss(csv);
|
||||||
string number;
|
string number;
|
||||||
@ -23,11 +23,11 @@ vector<int> NumberHelper::getRolls(string csv){
|
|||||||
return rolls;
|
return rolls;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NumberHelper::validateRolls(vector<int> rolls){
|
bool NumberUtils::validateRolls(const vector<int>& rolls){
|
||||||
int rollCount = rolls.size();
|
int rollCount = rolls.size();
|
||||||
|
|
||||||
for(int i = 0; i < rollCount; ++i) {
|
for (int i = 0; i < rollCount; ++i) {
|
||||||
if(rolls[i] > 10 || rolls[i] < 0) {
|
if (rolls[i] > 10 || rolls[i] < 0) {
|
||||||
cerr << "Number: " << rolls[i] << " is invalid.";
|
cerr << "Number: " << rolls[i] << " is invalid.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -49,16 +49,14 @@ bool NumberHelper::validateRolls(vector<int> rolls){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<Frame> NumberHelper::createFrames(vector<int> rolls){
|
vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
|
||||||
vector<Frame> frame;
|
vector<Frame> frame;
|
||||||
|
|
||||||
int roll = 0;
|
int roll = 0;
|
||||||
|
|
||||||
while (roll != rolls.size())
|
while (roll != rolls.size()) {
|
||||||
{
|
|
||||||
// Strike
|
// 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 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]));
|
||||||
@ -71,11 +69,9 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Spare
|
// 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 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]));
|
frame.push_back(createBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -86,8 +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 +92,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame NumberHelper::createStrikeFrame(int i){
|
Frame NumberUtils::createStrikeFrame(int i){
|
||||||
struct Frame frame = Frame();
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -109,7 +104,7 @@ Frame NumberHelper::createStrikeFrame(int i){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame NumberHelper::createFreeFrame(int i, int j){
|
Frame NumberUtils::createFreeFrame(int i, int j){
|
||||||
struct Frame frame = Frame();
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -122,7 +117,7 @@ Frame NumberHelper::createFreeFrame(int i, int j){
|
|||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
Frame NumberHelper::createBonusFrame(int i, int j, int k){
|
Frame NumberUtils::createBonusFrame(int i, int j, int k){
|
||||||
struct Frame frame = Frame();
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
vector<int> rolls;
|
vector<int> rolls;
|
||||||
@ -136,7 +131,7 @@ Frame NumberHelper::createBonusFrame(int i, int j, int k){
|
|||||||
return frame;
|
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) {
|
for (size_t i = 0; i < str.size(); ++i) {
|
||||||
if (!isdigit(str[i]) && !isspace(str[i])) return false;
|
if (!isdigit(str[i]) && !isspace(str[i])) return false;
|
||||||
}
|
}
|
@ -6,11 +6,11 @@ void PrintFrames::printHeader() {
|
|||||||
cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl;
|
cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintFrames::printValue(string value) {
|
void PrintFrames::printValue(const string& value) {
|
||||||
cout << value;
|
cout << value;
|
||||||
}
|
}
|
||||||
|
|
||||||
string PrintFrames::parseValue(vector<Frame> frames) {
|
string PrintFrames::parseValue(const vector<Frame>& frames) {
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
|
|
||||||
// There can only be 10 frames.
|
// There can only be 10 frames.
|
||||||
@ -30,14 +30,12 @@ string PrintFrames::parseValue(vector<Frame> frames) {
|
|||||||
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) {
|
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) {
|
||||||
ss << "X, " << frames[i].Roll[2] << " ";
|
ss << "X, " << frames[i].Roll[2] << " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 0) {
|
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 0) {
|
||||||
ss << frames[i].Roll[1] << ", - ";
|
ss << frames[i].Roll[1] << ", - ";
|
||||||
}
|
}
|
||||||
else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] == 10) {
|
else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] == 10) {
|
||||||
ss << "-, " << frames[i].Roll[2] << " ";
|
ss << "-, " << frames[i].Roll[2] << " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 0) {
|
else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 0) {
|
||||||
ss << frames[i].Roll[1] << ", - ";
|
ss << frames[i].Roll[1] << ", - ";
|
||||||
}
|
}
|
||||||
@ -59,15 +57,12 @@ string PrintFrames::parseValue(vector<Frame> frames) {
|
|||||||
if (frames[i + 1].Roll[0] + frames[i + 1].Roll[1] == 10) {
|
if (frames[i + 1].Roll[0] + frames[i + 1].Roll[1] == 10) {
|
||||||
ss << "X, " << frames[i + 1].Roll[0] << ", /";
|
ss << "X, " << frames[i + 1].Roll[0] << ", /";
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (frames[i + 1].Roll[0] == 0 && frames[i + 1].Roll[1] != 0) {
|
else if (frames[i + 1].Roll[0] == 0 && frames[i + 1].Roll[1] != 0) {
|
||||||
ss << "X, " << "-, " << frames[i + 1].Roll[1];
|
ss << "X, " << "-, " << frames[i + 1].Roll[1];
|
||||||
}
|
}
|
||||||
else if (frames[i + 1].Roll[0] != 0 && frames[i + 1].Roll[1] == 0) {
|
else if (frames[i + 1].Roll[0] != 0 && frames[i + 1].Roll[1] == 0) {
|
||||||
ss << "X, " << frames[i + 1].Roll[0] << ", -";
|
ss << "X, " << frames[i + 1].Roll[0] << ", -";
|
||||||
}
|
} else {
|
||||||
|
|
||||||
else {
|
|
||||||
ss << "X, ";
|
ss << "X, ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,6 +98,7 @@ string PrintFrames::parseValue(vector<Frame> frames) {
|
|||||||
} else {
|
} else {
|
||||||
ss << frames[i].Roll[0] << ", /, " << frames[i].Roll[2];
|
ss << frames[i].Roll[0] << ", /, " << frames[i].Roll[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,12 +116,9 @@ string PrintFrames::parseValue(vector<Frame> frames) {
|
|||||||
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 0) {
|
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 0) {
|
||||||
ss << "-, " << frames[i].Roll[1];
|
ss << "-, " << frames[i].Roll[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (frames[i].Roll[0] != 0 && frames[i].Roll[1] == 0) {
|
else if (frames[i].Roll[0] != 0 && frames[i].Roll[1] == 0) {
|
||||||
ss << frames[i].Roll[0] << ", -";
|
ss << frames[i].Roll[0] << ", -";
|
||||||
}
|
} else {
|
||||||
|
|
||||||
else {
|
|
||||||
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1];
|
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "../include/ScoreCalculator.h"
|
#include "../include/ScoreCalculator.h"
|
||||||
#include "../include/Frame.h"
|
#include "../include/Frame.h"
|
||||||
|
|
||||||
int ScoreCalculator::getScore(vector<Frame> frames) {
|
int ScoreCalculator::getScore(const vector<Frame>& frames) {
|
||||||
int score = 0;
|
int score = 0;
|
||||||
|
|
||||||
// There can only be 10 frames.
|
// There can only be 10 frames.
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "../include/FileUtil.h"
|
#include "../include/FileUtil.h"
|
||||||
#include "../include/NumberHelper.h"
|
#include "../include/NumberUtils.h"
|
||||||
#include "../include/ScoreCalculator.h"
|
#include "../include/ScoreCalculator.h"
|
||||||
#include "../include/PrintFrames.h"
|
#include "../include/PrintFrames.h"
|
||||||
|
|
||||||
@ -19,13 +19,13 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
string file = FileUtil::getFile(argv[1]);
|
string file = FileUtil::getFile(argv[1]);
|
||||||
vector<int> rolls = NumberHelper::getRolls(file);
|
vector<int> rolls = NumberUtils::getRolls(file);
|
||||||
|
|
||||||
if (!NumberHelper::validateRolls(rolls)) {
|
if (!NumberUtils::validateRolls(rolls)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<Frame> frames = NumberHelper::createFrames(rolls);
|
vector<Frame> frames = NumberUtils::createFrames(rolls);
|
||||||
|
|
||||||
string values = PrintFrames::parseValue(frames);
|
string values = PrintFrames::parseValue(frames);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user