Compare commits
19 Commits
simplify-s
...
main
Author | SHA1 | Date | |
---|---|---|---|
dbfba6a7dd | |||
2778a490a7 | |||
2ec206b5bb | |||
35cd28b6be | |||
efc1b264b8 | |||
1e51718b04 | |||
aa141a8f22 | |||
2e69b2e4cf | |||
531732c524 | |||
002d8a0804 | |||
8222f5e0b3 | |||
b8e2e8e2fd | |||
1228b7150a | |||
4a09cbbb33 | |||
0ebfb2b86d | |||
76284c3951 | |||
aa81717fd9 | |||
7d9da3cc50 | |||
61b00d40db |
16
Dockerfile
16
Dockerfile
@ -1,11 +1,17 @@
|
|||||||
FROM gcc:latest
|
FROM gcc:latest AS build
|
||||||
|
|
||||||
RUN g++ src/*.cpp -o Bowling
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY include/* /app/include/
|
||||||
|
COPY src/* /app/src/
|
||||||
|
|
||||||
|
RUN g++ src/*.cpp -o Bowling
|
||||||
|
|
||||||
|
FROM debian:sid-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY --from=build /app/Bowling Bowling
|
||||||
COPY rolls.txt /app/
|
COPY rolls.txt /app/
|
||||||
|
|
||||||
#RUN g++ src/*.cpp -o Bowling
|
|
||||||
|
|
||||||
CMD ["./Bowling", "rolls.txt"]
|
CMD ["./Bowling", "rolls.txt"]
|
||||||
|
@ -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
|
|
19
include/FileUtils.h
Normal file
19
include/FileUtils.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef FILEUTILS_H
|
||||||
|
#define FILEUTILS_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class FileUtils {
|
||||||
|
public:
|
||||||
|
FileUtils() = default;
|
||||||
|
|
||||||
|
~FileUtils() = default;
|
||||||
|
|
||||||
|
static string getFile(char *path);
|
||||||
|
static bool fileExists(char *path);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -1,27 +0,0 @@
|
|||||||
#ifndef NUMBERHELPER_H
|
|
||||||
#define NUMBERHELPER_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include "../include/Frame.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class NumberHelper {
|
|
||||||
public:
|
|
||||||
NumberHelper() = default;
|
|
||||||
|
|
||||||
~NumberHelper() = default;
|
|
||||||
|
|
||||||
static vector<int> getRolls(string csv);
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
28
include/NumberUtils.h
Normal file
28
include/NumberUtils.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#ifndef NUMBERUTILS_H
|
||||||
|
#define NUMBERUTILS_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include "../include/Frame.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class NumberUtils {
|
||||||
|
public:
|
||||||
|
NumberUtils() = default;
|
||||||
|
|
||||||
|
~NumberUtils() = default;
|
||||||
|
|
||||||
|
static vector<int> getRolls(const string& csv);
|
||||||
|
static bool validateRolls(const vector<int>& rolls);
|
||||||
|
static vector<Frame> createFrames(const 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);
|
||||||
|
static bool isNumber(const std::string& str);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -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,12 @@ class PrintFrames {
|
|||||||
|
|
||||||
~PrintFrames() = default;
|
~PrintFrames() = default;
|
||||||
|
|
||||||
static void printHeader(vector<int> rolls);
|
|
||||||
static void printValue(vector<int> rolls);
|
static void printResult(const vector<Frame>& frames);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void printHeader();
|
||||||
|
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
|
@ -1 +0,0 @@
|
|||||||
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5
|
|
@ -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;
|
|
||||||
}
|
|
28
src/FileUtils.cpp
Normal file
28
src/FileUtils.cpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include "../include/FileUtils.h"
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
bool FileUtils::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;
|
||||||
|
}
|
||||||
|
|
||||||
|
string FileUtils::getFile(char *path){
|
||||||
|
ifstream file(path);
|
||||||
|
|
||||||
|
string line;
|
||||||
|
|
||||||
|
// There is only one line in the file
|
||||||
|
getline(file, line);
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
return line;
|
||||||
|
}
|
@ -1,27 +1,33 @@
|
|||||||
#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>
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
// Get number from the CSV file, split by comma, and convert to int.
|
// Get number from the CSV file, split by comma, and convert to int.
|
||||||
while (getline(ss, number, ',')) {
|
while (getline(ss, number, ',')) {
|
||||||
|
if (!isNumber(number)) {
|
||||||
|
cerr << number << " is not a number.";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
rolls.push_back(stoi(number));
|
rolls.push_back(stoi(number));
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -43,46 +49,41 @@ 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]));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
frame.push_back(CreateStrikeFrame(10));
|
frame.push_back(createStrikeFrame(10));
|
||||||
|
|
||||||
roll += 1;
|
roll += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
frame.push_back(CreateFreeFrame(rolls[roll], rolls[roll + 1]));
|
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
|
||||||
|
|
||||||
roll += 2;
|
roll += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
@ -91,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;
|
||||||
@ -103,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;
|
||||||
@ -116,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;
|
||||||
@ -129,3 +130,11 @@ Frame NumberHelper::CreateBonusFrame(int i, int j, int k){
|
|||||||
|
|
||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool NumberUtils::isNumber(const std::string& str){
|
||||||
|
for (size_t i = 0; i < str.size(); ++i) {
|
||||||
|
if (!isdigit(str[i]) && !isspace(str[i])) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
@ -1,116 +1,132 @@
|
|||||||
#include "../include/PrintFrames.h"
|
#include "../include/PrintFrames.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
void PrintFrames::printHeader(vector<int> rolls){
|
void PrintFrames::printResult(const vector<Frame>& frames) {
|
||||||
int frame = 0;
|
printHeader();
|
||||||
int i = 1;
|
cout << parseValue(frames);
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
void PrintFrames::printHeader() {
|
||||||
cout << "|";
|
cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
string PrintFrames::parseValue(const vector<Frame>& frames) {
|
||||||
|
stringstream ss;
|
||||||
|
|
||||||
|
// There can only be 10 frames.
|
||||||
|
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, ";
|
||||||
cout << " f" << i << " |" << endl;
|
|
||||||
break;
|
if (frames[i].Roll[1] == 10 && frames[i].Roll[2] == 10) {
|
||||||
|
ss << "X, X";
|
||||||
|
}
|
||||||
|
else if (frames[i].Roll[1] != 10 && frames[i].Roll[2] == 10) {
|
||||||
|
ss << frames[i].Roll[1] << ", X ";
|
||||||
|
}
|
||||||
|
else if (frames[i].Roll[1] == 10 && frames[i].Roll[2] != 10) {
|
||||||
|
ss << "X, " << 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] << " ";
|
||||||
|
}
|
||||||
|
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 << " f" << i << " ";
|
// 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 << " f" << i << " |" << 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] << ", /, " << frames[i].Roll[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << " f" << i << " ";
|
ss << frames[i].Roll[0] << ", /";
|
||||||
frame += 2;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open Frame
|
// Open frame
|
||||||
else {
|
if (i == 9) {
|
||||||
cout << " f" << i << " ";
|
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1] << " ";
|
||||||
frame += 2;
|
} else {
|
||||||
}
|
if (frames[i].Roll[0] == 0 && frames[i].Roll[1] == 0) {
|
||||||
|
ss << "-, -";
|
||||||
++i;
|
}
|
||||||
|
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 0) {
|
||||||
// No more frames
|
ss << "-, " << frames[i].Roll[1];
|
||||||
if (frame == rolls.size()) {
|
}
|
||||||
cout << "|" << endl;
|
else if (frames[i].Roll[0] != 0 && frames[i].Roll[1] == 0) {
|
||||||
break;
|
ss << frames[i].Roll[0] << ", -";
|
||||||
}
|
} else {
|
||||||
}
|
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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;
|
|
||||||
}
|
|
||||||
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 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;
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ss << "|";
|
||||||
|
ss << endl;
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
}
|
}
|
@ -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.
|
||||||
@ -19,7 +19,7 @@ int ScoreCalculator::getScore(vector<Frame> frames) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// [10] + [x] + [y]
|
// 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];
|
score += frames[i].Roll[0] + frames[i + 1].Roll[0] + frames[i + 2].Roll[0];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
18
src/main.cpp
18
src/main.cpp
@ -1,8 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "../include/FileReader.h"
|
#include "../include/FileUtils.h"
|
||||||
#include "../include/FileHelper.h"
|
#include "../include/NumberUtils.h"
|
||||||
#include "../include/NumberHelper.h"
|
|
||||||
#include "../include/ScoreCalculator.h"
|
#include "../include/ScoreCalculator.h"
|
||||||
#include "../include/PrintFrames.h"
|
#include "../include/PrintFrames.h"
|
||||||
|
|
||||||
@ -14,22 +13,21 @@ int main(int argc, char *argv[]) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!FileHelper::fileExists(argv[1])) {
|
if (!FileUtils::fileExists(argv[1])) {
|
||||||
cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
cerr << "Filepath: " << argv[1] << " doesn't exist.";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
string file = FileReader::getFile(argv[1]);
|
string file = FileUtils::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;
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintFrames::printHeader(rolls);
|
vector<Frame> frames = NumberUtils::createFrames(rolls);
|
||||||
PrintFrames::printValue(rolls);
|
|
||||||
|
|
||||||
vector<Frame> frames = NumberHelper::createFrames(rolls);
|
PrintFrames::printResult(frames);
|
||||||
|
|
||||||
int score = ScoreCalculator::getScore(frames);
|
int score = ScoreCalculator::getScore(frames);
|
||||||
cout << "Score: " << score << endl;
|
cout << "Score: " << score << endl;
|
||||||
|
1
test rolls/rolls4.txt
Normal file
1
test rolls/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
test rolls/rolls5.txt
Normal file
1
test rolls/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
|
Loading…
Reference in New Issue
Block a user