From d32baf9894ce67850c83b6e4a281bd1cfd921a93 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Tue, 27 Dec 2022 11:28:13 +0100 Subject: [PATCH] Improve code in general --- src/ChessArbiter.cpp | 4 ++-- src/ChessArbiter.hpp | 4 ++-- src/Fen.cpp | 14 +++++++------- src/Fen.hpp | 10 +++++----- src/Piece.cpp | 2 +- src/Piece.hpp | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/ChessArbiter.cpp b/src/ChessArbiter.cpp index 29b17c7..dece4b9 100644 --- a/src/ChessArbiter.cpp +++ b/src/ChessArbiter.cpp @@ -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(); diff --git a/src/ChessArbiter.hpp b/src/ChessArbiter.hpp index b874493..c29dd32 100644 --- a/src/ChessArbiter.hpp +++ b/src/ChessArbiter.hpp @@ -29,8 +29,8 @@ class ChessArbiter { /// @brief Use to compute occurences of positions std::unordered_map 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; diff --git a/src/Fen.cpp b/src/Fen.cpp index f3acfe5..54dddfd 100644 --- a/src/Fen.cpp +++ b/src/Fen.cpp @@ -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 diff --git a/src/Fen.hpp b/src/Fen.hpp index c7720b5..bcb93de 100644 --- a/src/Fen.hpp +++ b/src/Fen.hpp @@ -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 { diff --git a/src/Piece.cpp b/src/Piece.cpp index 8ace179..2a32a9d 100644 --- a/src/Piece.cpp +++ b/src/Piece.cpp @@ -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 Piece::GetMoves() { diff --git a/src/Piece.hpp b/src/Piece.hpp index c1f19d6..7469ee2 100644 --- a/src/Piece.hpp +++ b/src/Piece.hpp @@ -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 GetMoves(); };