Improve code in general

This commit is contained in:
Loic Guegan 2022-12-27 11:28:13 +01:00
parent f9ac11ad44
commit d32baf9894
6 changed files with 18 additions and 18 deletions

View file

@ -13,9 +13,9 @@ void ChessArbiter::Setup(const std::string &fen) {
positions[this->fen.board] = 1;
}
void ChessArbiter::SetFEN(FEN fen) { SetFEN(FENParser::Serialize(fen)); }
void ChessArbiter::SetFEN(const FEN &fen) { SetFEN(FENParser::Serialize(fen)); }
void ChessArbiter::SetFEN(std::string newfen) {
void ChessArbiter::SetFEN(const std::string &newfen) {
fen = FENParser::Parse(newfen);
board.Clear();

View file

@ -29,8 +29,8 @@ class ChessArbiter {
/// @brief Use to compute occurences of positions
std::unordered_map<std::string, char> positions;
/// @brief FEN methods used internally
void SetFEN(std::string);
void SetFEN(FEN);
void SetFEN(const std::string &newfen);
void SetFEN(const FEN &fen);
std::string SAN, SAN_last;
char capture;
bool was_enpassant;

View file

@ -2,9 +2,9 @@
namespace chessarbiter {
std::string FENParser::normalize_rank(std::string fen_rank) {
std::string FENParser::normalize_rank(const std::string &fen_rank) {
std::string normalized;
for (char &c : fen_rank) {
for (const char &c : fen_rank) {
if (IS_DIGIT(c)) {
for (char i = 0; i < (c - '0'); i++) {
normalized += ' ';
@ -27,14 +27,14 @@ std::string FENParser::normalize_rank(std::string fen_rank) {
return (normalized);
}
char FENParser::NextToken(std::string fen, char loc) {
char FENParser::NextToken(const std::string &fen, char loc) {
while (loc < fen.size() && IS_BLANK(fen[loc])) {
loc++;
}
return (loc);
}
char FENParser::NextRank(std::string fen, char loc) {
char FENParser::NextRank(const std::string &fen, char loc) {
loc++;
while (loc < fen.size() && fen[loc] != '/' && fen[loc] != ' ') {
loc++;
@ -42,11 +42,11 @@ char FENParser::NextRank(std::string fen, char loc) {
return (loc);
}
std::string FENParser::Serialize(FEN fen) {
std::string FENParser::Serialize(const FEN &fen) {
std::string s;
char skip = 0;
char rank = 0;
for (char &c : fen.board) {
for (const char &c : fen.board) {
rank++;
if (c == ' ') {
skip++;
@ -104,7 +104,7 @@ std::string FENParser::Serialize(FEN fen) {
return (s);
}
FEN FENParser::Parse(std::string fen) {
FEN FENParser::Parse(const std::string &fen) {
FEN parsed;
// Parse board

View file

@ -29,15 +29,15 @@ public:
class FENParser {
private:
static std::string normalize_rank(std::string fen_rank);
static char NextToken(std::string fen, char loc);
static char NextRank(std::string fen, char loc);
static std::string normalize_rank(const std::string &fen_rank);
static char NextToken(const std::string &fen, char loc);
static char NextRank(const std::string &fen, char loc);
public:
/// @brief Parse a FEN from a string (can throw InvalidFEN)
static FEN Parse(std::string);
static FEN Parse(const std::string &fen);
/// @brief Generate a fen string from the FEN object
static std::string Serialize(FEN fen);
static std::string Serialize(const FEN &fen);
};
struct InvalidFEN : public std::exception {

View file

@ -1,7 +1,7 @@
#include "Piece.hpp"
namespace chessarbiter {
Piece::Piece(char c, std::string coord)
Piece::Piece(char c, const std::string &coord)
: piece(c), isBlack(!isupper(c)), coord(coord) {}
std::vector<std::string> Piece::GetMoves() {

View file

@ -19,7 +19,7 @@ public:
bool isBlack;
std::string coord;
char piece;
Piece(char c, std::string coord);
Piece(char c, const std::string &coord);
/// @brief Get all possible moves according to the type of piece and its position
std::vector<std::string> GetMoves();
};