Made code style more consistent, use const reference in parameters, and removed a redundant check

This commit is contained in:
Rasmus Rasmussen 2025-03-27 18:05:20 +01:00
parent 1e51718b04
commit efc1b264b8
12 changed files with 37 additions and 109 deletions

View File

@ -1,13 +0,0 @@
#ifndef FILEHELPER_H
#define FILEHELPER_H
class FileHelper {
public:
FileHelper() = default;
~FileHelper() = default;
static bool fileExists(char *path);
};
#endif

View File

@ -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

View File

@ -1,5 +1,5 @@
#ifndef NUMBERHELPER_H
#define NUMBERHELPER_H
#ifndef NUMBERUTILS_H
#define NUMBERUTILS_H
#include <string>
#include <vector>
@ -7,15 +7,15 @@
using namespace std;
class NumberHelper {
class NumberUtils {
public:
NumberHelper() = default;
NumberUtils() = default;
~NumberHelper() = default;
~NumberUtils() = default;
static vector<int> getRolls(string csv);
static bool validateRolls(vector<int> rolls);
static vector<Frame> createFrames(vector<int> rolls);
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);

View File

@ -14,8 +14,8 @@ class PrintFrames {
~PrintFrames() = default;
static void printHeader();
static void printValue(string values);
static string parseValue(vector<Frame> frames);
static void printValue(const string& value);
static string parseValue(const vector<Frame>& frames);
};
#endif

View File

@ -13,7 +13,7 @@ class ScoreCalculator {
~ScoreCalculator() = default;
static int getScore(vector<Frame> rolls);
static int getScore(const vector<Frame>& frames);
};
#endif

View File

@ -1,7 +1,7 @@
# Makefile for hello.c
CC = g++
CFLAGS = -O2
CFLAGS = -O2 -Wanalyzer-malloc-leak
SRC_DIR = src
BUILD_DIR = build

View File

@ -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;
}

View File

@ -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;
}

View File

@ -1,11 +1,11 @@
#include "../include/NumberHelper.h"
#include "../include/NumberUtils.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
vector<int> NumberHelper::getRolls(string csv){
vector<int> NumberUtils::getRolls(const string& csv){
vector<int> rolls;
stringstream ss(csv);
string number;
@ -23,11 +23,11 @@ vector<int> NumberHelper::getRolls(string csv){
return rolls;
}
bool NumberHelper::validateRolls(vector<int> rolls){
bool NumberUtils::validateRolls(const vector<int>& rolls){
int rollCount = rolls.size();
for(int i = 0; i < rollCount; ++i) {
if(rolls[i] > 10 || rolls[i] < 0) {
for (int i = 0; i < rollCount; ++i) {
if (rolls[i] > 10 || rolls[i] < 0) {
cerr << "Number: " << rolls[i] << " is invalid.";
return false;
}
@ -49,16 +49,14 @@ bool NumberHelper::validateRolls(vector<int> rolls){
return true;
}
vector<Frame> NumberHelper::createFrames(vector<int> rolls){
vector<Frame> NumberUtils::createFrames(const vector<int>& rolls){
vector<Frame> frame;
int roll = 0;
while (roll != rolls.size())
{
while (roll != rolls.size()) {
// 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 (roll + 3 == rolls.size() && rolls[roll - 3] == 10) {
frame.push_back(createBonusFrame(rolls[roll], rolls[roll + 1], rolls[roll + 2]));
@ -71,11 +69,9 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
}
// 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 (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]));
break;
}
@ -86,8 +82,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
}
// Open Frame
else
{
else {
frame.push_back(createFreeFrame(rolls[roll], rolls[roll + 1]));
roll += 2;
@ -97,7 +92,7 @@ vector<Frame> NumberHelper::createFrames(vector<int> rolls){
return frame;
}
Frame NumberHelper::createStrikeFrame(int i){
Frame NumberUtils::createStrikeFrame(int i){
struct Frame frame = Frame();
vector<int> rolls;
@ -109,7 +104,7 @@ Frame NumberHelper::createStrikeFrame(int i){
return frame;
}
Frame NumberHelper::createFreeFrame(int i, int j){
Frame NumberUtils::createFreeFrame(int i, int j){
struct Frame frame = Frame();
vector<int> rolls;
@ -122,7 +117,7 @@ Frame NumberHelper::createFreeFrame(int i, int j){
return frame;
}
Frame NumberHelper::createBonusFrame(int i, int j, int k){
Frame NumberUtils::createBonusFrame(int i, int j, int k){
struct Frame frame = Frame();
vector<int> rolls;
@ -136,7 +131,7 @@ Frame NumberHelper::createBonusFrame(int i, int j, int k){
return frame;
}
bool NumberHelper::isNumber(const std::string& str){
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;
}

View File

@ -6,11 +6,11 @@ void PrintFrames::printHeader() {
cout << "| f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 |" << endl;
}
void PrintFrames::printValue(string value) {
void PrintFrames::printValue(const string& value) {
cout << value;
}
string PrintFrames::parseValue(vector<Frame> frames) {
string PrintFrames::parseValue(const vector<Frame>& frames) {
stringstream ss;
// There can only be 10 frames.
@ -30,14 +30,12 @@ string PrintFrames::parseValue(vector<Frame> frames) {
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] << ", - ";
}
@ -59,15 +57,12 @@ string PrintFrames::parseValue(vector<Frame> frames) {
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 {
} else {
ss << "X, ";
}
}
@ -103,6 +98,7 @@ string PrintFrames::parseValue(vector<Frame> frames) {
} else {
ss << frames[i].Roll[0] << ", /, " << frames[i].Roll[2];
}
continue;
}
@ -120,12 +116,9 @@ string PrintFrames::parseValue(vector<Frame> frames) {
else if (frames[i].Roll[0] == 0 && frames[i].Roll[1] != 0) {
ss << "-, " << frames[i].Roll[1];
}
else if (frames[i].Roll[0] != 0 && frames[i].Roll[1] == 0) {
ss << frames[i].Roll[0] << ", -";
}
else {
} else {
ss << frames[i].Roll[0] << ", " << frames[i].Roll[1];
}
}

View File

@ -1,7 +1,7 @@
#include "../include/ScoreCalculator.h"
#include "../include/Frame.h"
int ScoreCalculator::getScore(vector<Frame> frames) {
int ScoreCalculator::getScore(const vector<Frame>& frames) {
int score = 0;
// There can only be 10 frames.

View File

@ -1,7 +1,7 @@
#include <iostream>
#include <string>
#include "../include/FileUtil.h"
#include "../include/NumberHelper.h"
#include "../include/NumberUtils.h"
#include "../include/ScoreCalculator.h"
#include "../include/PrintFrames.h"
@ -19,13 +19,13 @@ int main(int argc, char *argv[]) {
}
string file = FileUtil::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;
}
vector<Frame> frames = NumberHelper::createFrames(rolls);
vector<Frame> frames = NumberUtils::createFrames(rolls);
string values = PrintFrames::parseValue(frames);