Merge pull request 'main' (#8) from main into validate-input
Reviewed-on: #8
This commit is contained in:
commit
4a09cbbb33
11
include/Frame.h
Normal file
11
include/Frame.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef FRAME_H
|
||||||
|
#define FRAME_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct Frame {
|
||||||
|
vector<int> Roll;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "../include/Frame.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -14,6 +15,12 @@ class NumberHelper {
|
|||||||
|
|
||||||
static vector<int> getRolls(string csv);
|
static vector<int> getRolls(string csv);
|
||||||
static bool validateRolls(vector<int> rolls);
|
static bool validateRolls(vector<int> rolls);
|
||||||
|
static vector<Frame> createFrames(vector<int> rolls);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static Frame CreateStrikeFrame(int i);
|
||||||
|
static Frame CreateFreeFrame(int i, int j);
|
||||||
|
static Frame CreateBonusFrame(int i, int j, int k);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
#define PRINTFRAMES_H
|
#define PRINTFRAMES_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include "../include/Frame.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -11,8 +13,9 @@ class PrintFrames {
|
|||||||
|
|
||||||
~PrintFrames() = default;
|
~PrintFrames() = default;
|
||||||
|
|
||||||
static void printHeader(vector<int> rolls);
|
static void printHeader();
|
||||||
static void printValue(vector<int> rolls);
|
static void printValue(string values);
|
||||||
|
static string parseValue(vector<Frame> frames);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "../include/Frame.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ class ScoreCalculator {
|
|||||||
|
|
||||||
~ScoreCalculator() = default;
|
~ScoreCalculator() = default;
|
||||||
|
|
||||||
static int getScore(vector<int> rolls);
|
static int getScore(vector<Frame> rolls);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
1
rolls4.txt
Normal file
1
rolls4.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5
|
1
rolls5.txt
Normal file
1
rolls5.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 10, 10
|
@ -41,4 +41,91 @@ bool NumberHelper::validateRolls(vector<int> rolls){
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<Frame> NumberHelper::createFrames(vector<int> rolls){
|
||||||
|
vector<Frame> frame;
|
||||||
|
|
||||||
|
int roll = 0;
|
||||||
|
|
||||||
|
while (roll != rolls.size())
|
||||||
|
{
|
||||||
|
// Strike
|
||||||
|
if (rolls[roll] == 10)
|
||||||
|
{
|
||||||
|
// 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) {
|
||||||
|
frame.push_back(CreateBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
frame.push_back(CreateStrikeFrame(10));
|
||||||
|
|
||||||
|
roll += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spare
|
||||||
|
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 (roll + 3 == rolls.size() && rolls[roll] + rolls[roll + 1] == 10)
|
||||||
|
{
|
||||||
|
frame.push_back(CreateBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
|
||||||
|
|
||||||
|
roll += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open Frame
|
||||||
|
else
|
||||||
|
{
|
||||||
|
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
|
||||||
|
|
||||||
|
roll += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
Frame NumberHelper::CreateStrikeFrame(int i){
|
||||||
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
|
vector<int> rolls;
|
||||||
|
|
||||||
|
rolls.push_back(i);
|
||||||
|
|
||||||
|
frame.Roll = rolls;
|
||||||
|
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
Frame NumberHelper::CreateFreeFrame(int i, int j){
|
||||||
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
|
vector<int> rolls;
|
||||||
|
|
||||||
|
rolls.push_back(i);
|
||||||
|
rolls.push_back(j);
|
||||||
|
|
||||||
|
frame.Roll = rolls;
|
||||||
|
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
Frame NumberHelper::CreateBonusFrame(int i, int j, int k){
|
||||||
|
struct Frame frame = Frame();
|
||||||
|
|
||||||
|
vector<int> rolls;
|
||||||
|
|
||||||
|
rolls.push_back(i);
|
||||||
|
rolls.push_back(j);
|
||||||
|
rolls.push_back(k);
|
||||||
|
|
||||||
|
frame.Roll = rolls;
|
||||||
|
|
||||||
|
return frame;
|
||||||
}
|
}
|
@ -1,116 +1,139 @@
|
|||||||
#include "../include/PrintFrames.h"
|
#include "../include/PrintFrames.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
void PrintFrames::printHeader(vector<int> rolls){
|
void PrintFrames::printHeader() {
|
||||||
int frame = 0;
|
cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl;
|
||||||
int i = 1;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
cout << "|";
|
|
||||||
|
|
||||||
// Strike
|
|
||||||
if (rolls[frame] == 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << " f" << i << " ";
|
|
||||||
frame += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << " f" << i << " ";
|
|
||||||
frame += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open Frame
|
|
||||||
else {
|
|
||||||
cout << " f" << i << " ";
|
|
||||||
frame += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
++i;
|
|
||||||
|
|
||||||
// No more frames
|
|
||||||
if (frame == rolls.size()) {
|
|
||||||
cout << "|" << endl;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintFrames::printValue(vector<int> rolls){
|
void PrintFrames::printValue(string value) {
|
||||||
int frame = 0;
|
cout << value;
|
||||||
|
}
|
||||||
|
|
||||||
|
string PrintFrames::parseValue(vector<Frame> frames) {
|
||||||
|
string value;
|
||||||
|
stringstream ss;
|
||||||
|
|
||||||
while (true) {
|
// There can only be 10 frames.
|
||||||
cout << "|";
|
for (int i = 0; i < 10; i++) {
|
||||||
|
ss << "|";
|
||||||
// Strike
|
// Strike
|
||||||
if (rolls[frame] == 10) {
|
if (frames[i].Roll[0] == 10) {
|
||||||
// If we're on our last frame, and roll a strike, we're given two bonus rolls.
|
if (frames[i].Roll.size() == 3) {
|
||||||
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
|
ss << "X, ";
|
||||||
if (rolls[frame + 1] == 10 && rolls[frame + 2] == 10) {
|
|
||||||
cout << "X, X, X|" << endl;
|
if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 10) {
|
||||||
|
ss << "X, X";
|
||||||
}
|
}
|
||||||
else if (rolls[frame + 1] == 0 && rolls[frame + 2] == 0) {
|
else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 10) {
|
||||||
cout << "X, -, -|" << endl;
|
ss << frames[i].Roll[1] << ", X ";
|
||||||
}
|
}
|
||||||
else if (rolls[frame + 1] == 0) {
|
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) {
|
||||||
cout << "X, -, " << rolls[frame + 2] << "|" << endl;
|
ss << "X, " << frames[i].Roll[2] << " ";
|
||||||
}
|
|
||||||
else if (rolls[frame + 2] == 0) {
|
|
||||||
cout << "X, " << rolls[frame + 1] << ", -|" << endl;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
cout << "X, " << rolls[frame + 1] << ", " << rolls[frame + 2] << "|" << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 0) {
|
||||||
|
ss << frames[i].Roll[1] << ", - ";
|
||||||
|
}
|
||||||
|
else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] == 10) {
|
||||||
|
ss << "-, " << frames[i].Roll[2] << " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 0) {
|
||||||
|
ss << frames[i].Roll[1] << ", - ";
|
||||||
|
}
|
||||||
|
else if (frames[i].Roll[1] == 0 && frames[i].Roll[2] != 10) {
|
||||||
|
ss << "-, " << frames[i].Roll[2] << " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "X ";
|
// If the next frame has 3 or 2 rolls, then take those 2.
|
||||||
frame += 1;
|
if (frames[i + 1].Roll.size() > 1) {
|
||||||
|
if (frames[i + 1].Roll[0] == 10 && frames[i + 1].Roll[1] != 10) {
|
||||||
|
ss << "X, " << frames[i + 1].Roll[0] << " ";
|
||||||
|
}
|
||||||
|
else if (frames[i + 1].Roll[0] != 10 && frames[i + 1].Roll[1] == 10) {
|
||||||
|
ss << frames[i + 1].Roll[1] << ", X";
|
||||||
|
} else {
|
||||||
|
if (frames[i + 1].Roll[0] + frames[i + 1].Roll[1] == 10) {
|
||||||
|
ss << "X, " << frames[i + 1].Roll[0] << ", /";
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (frames[i + 1].Roll[0] == 0 && frames[i + 1].Roll[1] != 0) {
|
||||||
|
ss << "X, " << "-, " << frames[i + 1].Roll[1];
|
||||||
|
}
|
||||||
|
else if (frames[i + 1].Roll[0] != 0 && frames[i + 1].Roll[1] == 0) {
|
||||||
|
ss << "X, " << frames[i + 1].Roll[0] << ", -";
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
ss << "X, ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Both next frames, that are not the last, are 10.
|
||||||
|
ss << "X ";
|
||||||
|
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spare
|
// Spare
|
||||||
else if (rolls[frame] + rolls[frame + 1] == 10) {
|
if (frames[i].Roll[0] + frames[i].Roll[1] == 10) {
|
||||||
// If we're on our last frame, and roll a spare, we're given a bonus roll.
|
if (frames[i].Roll.size() == 3) {
|
||||||
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
|
if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 10 && frames[i].Roll[2] != 10) {
|
||||||
{
|
if (frames[i].Roll[2] == 0) {
|
||||||
cout << rolls[frame] << ", /" << ", " << rolls[frame + 1] << "|" << endl;
|
ss << "-, /, -";
|
||||||
break;
|
} else {
|
||||||
|
ss << "-, " << frames[i].Roll[1] << frames[i].Roll[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) {
|
||||||
|
if (frames[i].Roll[2] == 0) {
|
||||||
|
ss << "-, /, -";
|
||||||
|
} else {
|
||||||
|
ss << "-, /, " << frames[i].Roll[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 10 && frames[i].Roll[2] == 10) {
|
||||||
|
ss << "-, /, X";
|
||||||
|
} else {
|
||||||
|
ss << frames[i].Roll[0] << ", / ";
|
||||||
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << rolls[frame] << ", /";
|
ss << frames[i].Roll[0] << ", /";
|
||||||
frame += 2;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open Frame
|
// Open frame
|
||||||
else {
|
if (i == 9) {
|
||||||
if (rolls[frame] == 0) {
|
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1] << " ";
|
||||||
cout << "-" << ", " << rolls[frame + 1];
|
} else {
|
||||||
|
if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 0) {
|
||||||
|
ss << "-, -";
|
||||||
}
|
}
|
||||||
else if (rolls[frame + 1] == 0) {
|
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 0) {
|
||||||
cout << rolls[frame] << ", -";
|
ss << "-, " << frames[i].Roll[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "" << rolls[frame] << ", " << rolls[frame + 1];
|
else if (frames[i].Roll[0] != 0 && frames[i].Roll[1] == 0) {
|
||||||
frame += 2;
|
ss << frames[i].Roll[0] << ", -";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (frame == rolls.size()) {
|
else {
|
||||||
cout << " |" << endl;
|
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1];
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ss << "|";
|
||||||
|
ss << endl;
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
}
|
}
|
@ -1,45 +1,42 @@
|
|||||||
#include "../include/ScoreCalculator.h"
|
#include "../include/ScoreCalculator.h"
|
||||||
|
#include "../include/Frame.h"
|
||||||
|
|
||||||
int ScoreCalculator::getScore(vector<int> rolls) {
|
int ScoreCalculator::getScore(vector<Frame> frames) {
|
||||||
int score = 0;
|
int score = 0;
|
||||||
int frame = 0;
|
|
||||||
|
|
||||||
while (true) {
|
// There can only be 10 frames.
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
// Strike
|
// Strike
|
||||||
if (rolls[frame] == 10) {
|
if (frames[i].Roll[0] == 10) {
|
||||||
// If we're on our last frame, and roll a strike, we're given two bonus rolls.
|
if (frames[i].Roll.size() == 3) {
|
||||||
if (frame + 3 == rolls.size() && rolls[frame - 3] == 10) {
|
score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2];
|
||||||
score += 10 + rolls[frame + 1] + rolls[frame + 2];
|
continue;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
score += 10 + rolls[frame + 1] + rolls[frame + 2];
|
// If the next frame has 3 or 2 rolls, then take those 2.
|
||||||
frame += 1;
|
if (frames[i + 1].Roll.size() > 1) {
|
||||||
|
score += frames[i].Roll[0] + frames[i + 1].Roll[0] + frames[i + 1].Roll[1];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Both next frames, that are not the last, are 10.
|
||||||
|
score += frames[i].Roll[0] + frames[i + 1].Roll[0] + frames[i + 2].Roll[0];
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spare
|
// Spare
|
||||||
else if (rolls[frame] + rolls[frame + 1] == 10) {
|
if (frames[i].Roll[0] + frames[i].Roll[1] == 10) {
|
||||||
// If we're on our last frame, and roll a spare, we're given a bonus roll.
|
if (frames[i].Roll.size() == 3) {
|
||||||
if (frame + 3 == rolls.size() && rolls[frame] + rolls[frame + 1] == 10)
|
score += frames[i].Roll[0] + frames[i].Roll[1] + frames[i].Roll[2];
|
||||||
{
|
continue;
|
||||||
score += 10 + rolls[frame + 1];
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
score += 10 + rolls[frame + 2];
|
score += 10 + frames[i + 1].Roll[0];
|
||||||
frame += 2;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open Frame
|
// Open frame
|
||||||
else {
|
score += frames[i].Roll[0] + frames[i].Roll[1];
|
||||||
score += rolls[frame] + rolls[frame + 1];
|
|
||||||
frame += 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No more frames
|
|
||||||
if (frame == rolls.size()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return score;
|
return score;
|
||||||
|
10
src/main.cpp
10
src/main.cpp
@ -26,10 +26,14 @@ int main(int argc, char *argv[]) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintFrames::printHeader(rolls);
|
vector<Frame> frames = NumberHelper::createFrames(rolls);
|
||||||
PrintFrames::printValue(rolls);
|
|
||||||
|
|
||||||
int score = ScoreCalculator::getScore(rolls);
|
string values = PrintFrames::parseValue(frames);
|
||||||
|
|
||||||
|
PrintFrames::printHeader();
|
||||||
|
PrintFrames::printValue(values);
|
||||||
|
|
||||||
|
int score = ScoreCalculator::getScore(frames);
|
||||||
cout << "Score: " << score << endl;
|
cout << "Score: " << score << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user