Compare commits

..

No commits in common. "b285704ae19830b813158e4a20bdb54db4367b46" and "5f8cabb56ad14918bded72cbf058a44113abf1c4" have entirely different histories.

11 changed files with 59 additions and 53 deletions

View File

@ -3,11 +3,11 @@
class FileHelper {
public:
FileHelper() = default;
FileHelper();
~FileHelper() = default;
~FileHelper();
static bool fileExists(char *path);
static bool FileExists(char *path);
};
#endif

View File

@ -4,15 +4,13 @@
#include <string>
#include <vector>
using namespace std;
class FileReader {
public:
FileReader() = default;
FileReader();
~FileReader() = default;
~FileReader();
static string getFile(char *path);
static std::string GetFile(char *path);
};
#endif

View File

@ -8,12 +8,11 @@ using namespace std;
class NumberHelper {
public:
NumberHelper() = default;
NumberHelper();
~NumberHelper() = default;
~NumberHelper();
static vector<int> getRolls(string csv);
static vector<int> GetNumbers(string csv);
};
#endif

View File

@ -7,12 +7,12 @@ using namespace std;
class PrintFrames {
public:
PrintFrames() = default;
PrintFrames();
~PrintFrames() = default;
~PrintFrames();
static void printHeader(vector<int> rolls);
static void printValue(vector<int> rolls);
static void PrintHeader(vector<int> rolls);
static void PrintValue(vector<int> rolls);
};
#endif

View File

@ -8,11 +8,11 @@ using namespace std;
class ScoreCalculator {
public:
ScoreCalculator() = default;
ScoreCalculator();
~ScoreCalculator() = default;
~ScoreCalculator();
static int getScore(vector<int> rolls);
static int GetScore(vector<int> rolls);
};
#endif

View File

@ -1,7 +1,10 @@
#include "../include/FileHelper.h"
#include <sys/stat.h>
bool FileHelper::fileExists(char *path){
FileHelper::FileHelper() {};
FileHelper::~FileHelper() {};
bool FileHelper::FileExists(char *path){
struct stat s;
// Check if file exists, and if it isn't a folder.

View File

@ -2,12 +2,15 @@
#include <string>
#include <fstream>
string FileReader::getFile(char *path){
ifstream file(path);
FileReader::FileReader() {};
FileReader::~FileReader() {};
std::string FileReader::GetFile(char *path){
std::ifstream file(path);
string line;
std::string line;
getline(file, line);
std::getline(file, line);
file.close();

View File

@ -3,14 +3,16 @@
#include <sstream>
#include <vector>
vector<int> NumberHelper::getRolls(string csv){
vector<int> rolls;
stringstream ss(csv);
string number;
NumberHelper::NumberHelper() {};
NumberHelper::~NumberHelper() {};
std::vector<int> NumberHelper::GetNumbers(std::string csv){
std::vector<int> rolls;
std::stringstream ss(csv);
std::string number;
// Get number from the CSV file, split by comma, and convert to int.
while (getline(ss, number, ',')) {
rolls.push_back(stoi(number));
while (std::getline(ss, number, ',')) {
rolls.push_back(std::stoi(number));
}
return rolls;

View File

@ -1,7 +1,10 @@
#include "../include/PrintFrames.h"
#include <iostream>
void PrintFrames::printHeader(vector<int> rolls){
PrintFrames::PrintFrames() {};
PrintFrames::~PrintFrames() {};
void PrintFrames::PrintHeader(vector<int> rolls){
int frame = 0;
int i = 1;
@ -10,7 +13,7 @@ void PrintFrames::printHeader(vector<int> rolls){
// Strike
if (rolls[frame] == 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 10.
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
cout << " f" << i << " |" << endl;
break;
@ -22,7 +25,6 @@ void PrintFrames::printHeader(vector<int> rolls){
// Spare
else if (rolls[frame] + rolls[frame + 1] == 10) {
// If we're on our last frame, and roll a spare, we're given a bonus roll.
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
{
cout << " f" << i << " |" << endl;
@ -41,7 +43,6 @@ void PrintFrames::printHeader(vector<int> rolls){
++i;
// No more frames
if (frame == rolls.size()) {
cout << "|" << endl;
break;
@ -49,7 +50,7 @@ void PrintFrames::printHeader(vector<int> rolls){
}
}
void PrintFrames::printValue(vector<int> rolls){
void PrintFrames::PrintValue(vector<int> rolls){
int frame = 0;
while (true) {
@ -57,7 +58,7 @@ void PrintFrames::printValue(vector<int> rolls){
// Strike
if (rolls[frame] == 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 10.
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
if (rolls[frame + 1] == 10 && rolls[frame + 2] == 10) {
cout << "X, X, X|" << endl;
@ -84,7 +85,6 @@ void PrintFrames::printValue(vector<int> rolls){
// Spare
else if (rolls[frame] + rolls[frame + 1] == 10) {
// If we're on our last frame, and roll a spare, we're given a bonus roll.
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
{
cout << rolls[frame] << ", /" << ", " << rolls[frame + 1] << "|" << endl;

View File

@ -1,13 +1,16 @@
#include "../include/ScoreCalculator.h"
int ScoreCalculator::getScore(vector<int> rolls) {
ScoreCalculator::ScoreCalculator() {};
ScoreCalculator::~ScoreCalculator() {};
int ScoreCalculator::GetScore(vector<int> rolls) {
int score = 0;
int frame = 0;
while (true) {
// Strike
if (rolls[frame] == 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 10.
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
score += 10 + rolls[frame + 1] + rolls[frame + 2];
break;
@ -15,11 +18,11 @@ int ScoreCalculator::getScore(vector<int> rolls) {
score += 10 + rolls[frame + 1] + rolls[frame + 2];
frame += 1;
continue;
}
// Spare
else if (rolls[frame] + rolls[frame + 1] == 10) {
// If we're on our last frame, and roll a spare, we're given a bonus roll.
if (rolls[frame] + rolls[frame + 1] == 10) {
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
{
score += 10 + rolls[frame + 1];
@ -28,15 +31,13 @@ int ScoreCalculator::getScore(vector<int> rolls) {
score += 10 + rolls[frame + 2];
frame += 2;
continue;
}
// Open Frame
else {
score += rolls[frame] + rolls[frame + 1];
frame += 2;
}
score += rolls[frame] + rolls[frame + 1];
frame += 2;
// No more frames
if (frame == rolls.size()) {
break;
}

View File

@ -14,18 +14,18 @@ int main(int argc, char *argv[]) {
return 0;
}
if (!FileHelper::fileExists(argv[1])) {
if (!FileHelper::FileExists(argv[1])) {
cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
string file = FileReader::getFile(argv[1]);
vector<int> rolls = NumberHelper::getRolls(file);
string file = FileReader::GetFile(argv[1]);
vector<int> rolls = NumberHelper::GetNumbers(file);
PrintFrames::printHeader(rolls);
PrintFrames::printValue(rolls);
PrintFrames::PrintHeader(rolls);
PrintFrames::PrintValue(rolls);
int score = ScoreCalculator::getScore(rolls);
int score = ScoreCalculator::GetScore(rolls);
cout << "Score: " << score << endl;
return 0;