10 changed files with 222 additions and 12 deletions
+3 -1
View File
@@ -4,13 +4,15 @@
#include <string> #include <string>
#include <vector> #include <vector>
using namespace std;
class NumberHelper { class NumberHelper {
public: public:
NumberHelper(); NumberHelper();
~NumberHelper(); ~NumberHelper();
static std::vector<int> GetNumbers(const std::string csv); static vector<int> GetNumbers(string csv);
}; };
#endif #endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef PRINTFRAMES_H
#define PRINTFRAMES_H
#include <vector>
using namespace std;
class PrintFrames {
public:
PrintFrames();
~PrintFrames();
static void PrintHeader(vector<int> rolls);
static void PrintValue(vector<int> rolls);
};
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef SCORECALCULATOR_H
#define SCORECALCULATOR_H
#include <string>
#include <vector>
using namespace std;
class ScoreCalculator {
public:
ScoreCalculator();
~ScoreCalculator();
static int GetScore(vector<int> rolls);
};
#endif
Executable
BIN
View File
Binary file not shown.
+3
View File
@@ -1 +1,4 @@
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 1
2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6, 10, 3, 2 2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6, 10, 3, 2
+1
View File
@@ -0,0 +1 @@
10, 3, 2, 2, 3, 5, 4, 9, 1, 2, 5, 3, 2, 4, 2, 3, 3, 4, 6
+1 -1
View File
@@ -6,7 +6,7 @@
NumberHelper::NumberHelper() {}; NumberHelper::NumberHelper() {};
NumberHelper::~NumberHelper() {}; NumberHelper::~NumberHelper() {};
std::vector<int> NumberHelper::GetNumbers(const std::string csv){ std::vector<int> NumberHelper::GetNumbers(std::string csv){
std::vector<int> rolls; std::vector<int> rolls;
std::stringstream ss(csv); std::stringstream ss(csv);
std::string number; std::string number;
+116
View File
@@ -0,0 +1,116 @@
#include "../Include/PrintFrames.h"
#include <iostream>
PrintFrames::PrintFrames() {};
PrintFrames::~PrintFrames() {};
void PrintFrames::PrintHeader(vector<int> rolls){
int frame = 0;
int i = 1;
while (true) {
cout << "|";
// Strike
if (rolls[frame] == 10) {
// 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;
}
cout << " f" << i << " ";
frame += 1;
}
// Spare
else if (rolls[frame] + rolls[frame + 1] == 10) {
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
{
cout << " f" << i << " |" << endl;
break;
}
cout << " f" << i << " ";
frame += 2;
}
// Open Frame
else {
cout << " f" << i << " ";
frame += 2;
}
++i;
if (frame == rolls.size()) {
cout << "|" << endl;
break;
}
}
}
void PrintFrames::PrintValue(vector<int> rolls){
int frame = 0;
while (true) {
cout << "|";
// Strike
if (rolls[frame] == 10) {
// 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;
}
else if (rolls[frame + 1] == 0 && rolls[frame + 2] == 0) {
cout << "X, -, -|" << endl;
}
else if (rolls[frame + 1] == 0) {
cout << "X, -, " << rolls[frame + 2] << "|" << endl;
}
else if (rolls[frame + 2] == 0) {
cout << "X, " << rolls[frame + 1] << ", -|" << endl;
}
else {
cout << "X, " << rolls[frame + 1] << ", " << rolls[frame + 2] << "|" << endl;
}
break;
}
cout << "X ";
frame += 1;
}
// Spare
else if (rolls[frame] + rolls[frame + 1] == 10) {
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
{
cout << rolls[frame] << ", /" << ", " << rolls[frame + 1] << "|" << endl;
break;
}
cout << rolls[frame] << ", /";
frame += 2;
}
// Open Frame
else {
if (rolls[frame] == 0) {
cout << "-" << ", " << rolls[frame + 1];
}
else if (rolls[frame + 1] == 0) {
cout << rolls[frame] << ", -";
}
cout << "" << rolls[frame] << ", " << rolls[frame + 1];
frame += 2;
}
if (frame == rolls.size()) {
cout << " |" << endl;
break;
}
}
}
+47
View File
@@ -0,0 +1,47 @@
#include "../Include/ScoreCalculator.h"
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 10.
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
score += 10 + rolls[frame + 1] + rolls[frame + 2];
break;
}
score += 10 + rolls[frame + 1] + rolls[frame + 2];
frame += 1;
continue;
}
// Spare
if (rolls[frame] + rolls[frame + 1] == 10) {
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
{
score += 10 + rolls[frame + 1];
break;
}
score += 10 + rolls[frame + 2];
frame += 2;
continue;
}
// Open Frame
score += rolls[frame] + rolls[frame + 1];
frame += 2;
if (frame == rolls.size()) {
break;
}
}
return score;
}
+14 -9
View File
@@ -3,25 +3,30 @@
#include "../Include/FileReader.h" #include "../Include/FileReader.h"
#include "../Include/FileHelper.h" #include "../Include/FileHelper.h"
#include "../Include/NumberHelper.h" #include "../Include/NumberHelper.h"
#include "../Include/ScoreCalculator.h"
#include "../Include/PrintFrames.h"
using namespace std;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if(argc < 2) { if (argc < 2) {
std::cerr << "Please provide a CSV formatted file."; cerr << "Please provide a CSV formatted file.";
return 0; return 0;
} }
if(!FileHelper::FileExists(argv[1])) { if (!FileHelper::FileExists(argv[1])) {
std::cerr << "Filepath: " << argv[1] << " doesn't exist."; cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0; return 0;
} }
std::string file = FileReader::GetFile(argv[1]); string file = FileReader::GetFile(argv[1]);
vector<int> rolls = NumberHelper::GetNumbers(file);
std::vector<int> numbers = NumberHelper::GetNumbers(file); PrintFrames::PrintHeader(rolls);
PrintFrames::PrintValue(rolls);
for(int i = 0; i < numbers.size(); i++) { int score = ScoreCalculator::GetScore(rolls);
std::cout << numbers[i] << ' '; cout << "Score: " << score << endl;
}
return 0; return 0;
} }