diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2022-12-27 09:34:33 +0100 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2022-12-27 09:34:33 +0100 |
| commit | aa5aac203becba63416d8bc8079ef095b6fa4c09 (patch) | |
| tree | 03ffe296bbf9d69c39c638792ab70e5f00c9c51f /src | |
| parent | bdb2963bc38893fd8a8009d8d5bfea3c34f1646d (diff) | |
Cleaning code
Diffstat (limited to 'src')
| -rw-r--r-- | src/ChessArbiter.cpp | 28 | ||||
| -rw-r--r-- | src/ChessArbiter.hpp | 16 |
2 files changed, 20 insertions, 24 deletions
diff --git a/src/ChessArbiter.cpp b/src/ChessArbiter.cpp index 0064234..9207e80 100644 --- a/src/ChessArbiter.cpp +++ b/src/ChessArbiter.cpp @@ -7,7 +7,7 @@ ChessArbiter::ChessArbiter() Setup("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); } -void ChessArbiter::Setup(std::string fen) { +void ChessArbiter::Setup(const std::string fen) { positions.clear(); SetFEN(fen); positions[this->fen.board] = 1; @@ -39,7 +39,7 @@ bool ChessArbiter::IsCheck(bool isBlack) { return (IsAttacked(kingloc, !isBlack)); } -bool ChessArbiter::Play(std::string move, char promote) { +bool ChessArbiter::Play(const std::string move, const char promote) { std::vector<std::string> moves = ListLegalMoves(fen.player); if (find(moves.begin(), moves.end(), move) != moves.end()) { Piece moved = board.GetPieceAt(move.substr(0, 2)); // This call never fail @@ -189,7 +189,7 @@ bool ChessArbiter::Play(std::string move, char promote) { bool ChessArbiter::WasEnPassant() { return (was_enpassant); } -bool ChessArbiter::IsAttacked(std::string square, bool by) { +bool ChessArbiter::IsAttacked(const std::string square, const bool by) { std::vector<std::string> moves = board.ListPossibleMoves(by); for (std::string &m : moves) { std::string src = m.substr(0, 2); @@ -209,7 +209,7 @@ bool ChessArbiter::IsAttacked(std::string square, bool by) { return (false); } -bool ChessArbiter::IsCastlePossible(bool isBlack, bool isLong) { +bool ChessArbiter::IsCastlePossible(const bool isBlack, const bool isLong) { if (isBlack && isLong && fen.black_castle_long) { if (board.IsEmpty("d8") && board.IsEmpty("c8") && board.IsEmpty("b8")) { @@ -273,7 +273,7 @@ int ChessArbiter::GetMaterialScore() { return (whiteScore - blackScore); } -std::string ChessArbiter::GetCaptures(bool isBlack) { +std::string ChessArbiter::GetCaptures(const bool isBlack) { std::string captures; // Pawn char p = 'P'; @@ -320,7 +320,7 @@ std::string ChessArbiter::GetCaptures(bool isBlack) { return (captures); } -std::vector<std::string> ChessArbiter::ListLegalMoves(bool isBlack) { +std::vector<std::string> ChessArbiter::ListLegalMoves(const bool isBlack) { std::vector<std::string> moves; for (std::string &move : board.ListPossibleMoves(isBlack)) { std::string src = move.substr(0, 2); @@ -425,7 +425,7 @@ bool ChessArbiter::IsCheckMate() { std::string ChessArbiter::GetSAN() { return (SAN); } char ChessArbiter::GetCapture() { return (capture); } -std::string ChessArbiter::ParseSAN(std::string SANMove) { +std::string ChessArbiter::ParseSAN(const std::string SANMove) { std::string src, dst; char piece = ' '; char hint = ' '; @@ -530,15 +530,11 @@ std::string ChessArbiter::ParseSAN(std::string SANMove) { return (src + dst); } -char ChessArbiter::ParseSANPromotion(std::string SANMove){ - for(short i=0;i<SANMove.length();i++){ - if(SANMove[i]=='='){ - if((i+1)<SANMove.length()){ - char p=SANMove[i+1]; // Must be upper - if(p=='Q' || p=='R' || p=='B' || p=='N'){ - return SANMove[i+1]; - } - } +char ChessArbiter::ParseSANPromotion(const std::string SANMove){ + if(SANMove.length()>=4 && SANMove[0] - 'a' < 8 && SANMove[2]=='='){ + char p=SANMove[3]; // Must be upper + if(p=='Q' || p=='R' || p=='B' || p=='N'){ + return p; } } return 'Q'; diff --git a/src/ChessArbiter.hpp b/src/ChessArbiter.hpp index d63ab06..1379db2 100644 --- a/src/ChessArbiter.hpp +++ b/src/ChessArbiter.hpp @@ -37,16 +37,16 @@ class ChessArbiter { public: ChessArbiter(); - void Setup(std::string); + void Setup(const std::string); std::string GetFEN(); /// @brief Check which player is going to play bool IsBlackTurn(); /// @brief Check if a side is in check bool IsCheck(bool); /// @brief Play a move (return false if it's illegal) - bool Play(std::string, char promote='Q'); + bool Play(const std::string, const char promote='Q'); /// @brief Check if a square is attacked by a particular player - bool IsAttacked(std::string, bool); + bool IsAttacked(const std::string, const bool); /// @brief Get the serialized board std::string GetBoard(); /// @brief Get current position evaluation according to player's material @@ -54,14 +54,14 @@ public: /// @brief Check if position is legal to be played bool IsPlayable(); /// @brief Get pieces captures by a player - std::string GetCaptures(bool); + std::string GetCaptures(const bool); char GetCapture(); /// @brief Get the english SAN format of the last move std::string GetSAN(); /// @brief List all the legal moves of a player - std::vector<std::string> ListLegalMoves(bool); + std::vector<std::string> ListLegalMoves(const bool); /// @brief Check if a specific castle is possible by a player - bool IsCastlePossible(bool, bool); + bool IsCastlePossible(const bool, const bool); bool IsCheckMate(); /// @brief Draws check bool IsDrawByFiftyMoveRule(); @@ -69,7 +69,7 @@ public: bool IsDrawByRepetitions(); bool IsDraw(); bool WasEnPassant(); - std::string ParseSAN(std::string SANMove); - char ParseSANPromotion(std::string SANMove); + std::string ParseSAN(const std::string SANMove); + char ParseSANPromotion(const std::string SANMove); }; } // namespace chessarbiter |
