cleanup #4

Merged
owner merged 2 commits from cleanup into main 2025-03-19 10:48:16 +00:00
11 changed files with 53 additions and 59 deletions

View File

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

View File

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

View File

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

View File

@ -7,12 +7,12 @@ using namespace std;
class PrintFrames {
public:
PrintFrames();
PrintFrames() = default;
~PrintFrames();
~PrintFrames() = default;
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();
ScoreCalculator() = default;
~ScoreCalculator();
~ScoreCalculator() = default;
static int GetScore(vector<int> rolls);
static int getScore(vector<int> rolls);
};
#endif

View File

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

View File

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

View File

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

View File

@ -1,10 +1,7 @@
#include "../include/PrintFrames.h"
#include <iostream>
PrintFrames::PrintFrames() {};
PrintFrames::~PrintFrames() {};
void PrintFrames::PrintHeader(vector<int> rolls){
void PrintFrames::printHeader(vector<int> rolls){
int frame = 0;
int i = 1;
@ -13,7 +10,7 @@ void PrintFrames::PrintHeader(vector<int> rolls){
// Strike
if (rolls[frame] == 10) {
// If we're on our last frame, and roll a 10.
// If we're on our last frame, and roll a strike, we're given two bonus rolls.
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
cout << " f" << i << " |" << endl;
break;
@ -25,6 +22,7 @@ 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;
@ -43,6 +41,7 @@ void PrintFrames::PrintHeader(vector<int> rolls){
++i;
// No more frames
if (frame == rolls.size()) {
cout << "|" << endl;
break;
@ -50,7 +49,7 @@ void PrintFrames::PrintHeader(vector<int> rolls){
}
}
void PrintFrames::PrintValue(vector<int> rolls){
void PrintFrames::printValue(vector<int> rolls){
int frame = 0;
while (true) {
@ -58,7 +57,7 @@ void PrintFrames::PrintValue(vector<int> rolls){
// Strike
if (rolls[frame] == 10) {
// If we're on our last frame, and roll a 10.
// If we're on our last frame, and roll a strike, we're given two bonus rolls.
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
if (rolls[frame + 1] == 10 && rolls[frame + 2] == 10) {
cout << "X, X, X|" << endl;
@ -85,6 +84,7 @@ 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,16 +1,13 @@
#include "../include/ScoreCalculator.h"
ScoreCalculator::ScoreCalculator() {};
ScoreCalculator::~ScoreCalculator() {};
int ScoreCalculator::GetScore(vector<int> rolls) {
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 10.
// If we're on our last frame, and roll a strike, we're given two bonus rolls.
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
score += 10 + rolls[frame + 1] + rolls[frame + 2];
break;
@ -18,11 +15,11 @@ int ScoreCalculator::GetScore(vector<int> rolls) {
score += 10 + rolls[frame + 1] + rolls[frame + 2];
frame += 1;
continue;
}
// Spare
if (rolls[frame] + rolls[frame + 1] == 10) {
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)
{
score += 10 + rolls[frame + 1];
@ -31,13 +28,15 @@ int ScoreCalculator::GetScore(vector<int> rolls) {
score += 10 + rolls[frame + 2];
frame += 2;
continue;
}
// Open Frame
score += rolls[frame] + rolls[frame + 1];
frame += 2;
else {
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::GetNumbers(file);
string file = FileReader::getFile(argv[1]);
vector<int> rolls = NumberHelper::getRolls(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;