diff --git a/source/uci.cpp b/source/uci.cpp index eb7762e..b1ff1cd 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -10,80 +10,7 @@ #include #include "tt.h" #include "perft.h" - -Move ParseMove(Board &board, const char* moveString) { - GenerateMoves(board, board.sideToMove); - - // Source square - std::string fromString = ""; - fromString += moveString[0]; - fromString += moveString[1]; - - auto it = std::find(squareCoords.begin(), squareCoords.end(), fromString); - - int from = std::distance(squareCoords.begin(), it); - - // Target square - std::string toString = ""; - toString += moveString[2]; - toString += moveString[3]; - - it = std::find(squareCoords.begin(), squareCoords.end(), toString); - - int to = std::distance(squareCoords.begin(), it); - std::string promotionString = ""; - - // Finding the move from available moves - for (Move move : board.moveList) { - if (move.MoveFrom() == from && move.MoveTo() == to) { - // Checking for promotion - if (std::string(moveString).length() == 5) { - int promotePiece = 0; - - // Checking for capture promotion - if (board.GetPieceColor(from) != board.GetPieceColor(to)) { - switch (moveString[4]) - { - case 'r': - promotePiece = rookPromoCapture; - break; - case 'q': - promotePiece = queenPromoCapture; - break; - case 'b': - promotePiece = bishopPromoCapture; - break; - case 'n': - promotePiece = knightPromoCapture; - break; - } - } else { - switch (moveString[4]) - { - case 'r': - promotePiece = rookPromotion; - break; - case 'q': - promotePiece = queenPromotion; - break; - case 'b': - promotePiece = bishopPromotion; - break; - case 'n': - promotePiece = knightPromotion; - break; - } - } - - return Move(from, to, promotePiece); - } - - return move; - } - } - - return Move(); -} +#include "utils.h" void ParsePosition(Board &board, const char* command) { command += 9; // Skip "position" @@ -121,7 +48,7 @@ void ParsePosition(Board &board, const char* command) { } if (!moveString.empty()) { - board.MakeMove(ParseMove(board, moveString.c_str())); + board.MakeMove(parseMove(board, moveString)); } } } diff --git a/source/uci.h b/source/uci.h index a355491..9f3e383 100644 --- a/source/uci.h +++ b/source/uci.h @@ -13,9 +13,6 @@ public: SearchParams(){} }; -// Returns Move object from a string -Move ParseMove(Board &board, const char* moveString); - // Initializes the position with given parameters void ParsePosition(Board &board, const char* command); diff --git a/source/utils.cpp b/source/utils.cpp new file mode 100644 index 0000000..3911661 --- /dev/null +++ b/source/utils.cpp @@ -0,0 +1,68 @@ +#include "utils.h" + +std::vector split(std::string_view str, char delim) { + std::vector results; + + std::istringstream stream(std::string{str}); + + for (std::string token{}; std::getline(stream, token, delim);) { + if (token.empty()) continue; + + results.push_back(token); + } + + return results; +} + +int parseSquare(std::string_view str) { + constexpr std::string_view files = "abcdefgh"; + + int file = files.find(str[0]); + int rank = str[1] - '0' - 1; + + return file + rank * 8; +} + +Move parseMove(Board &board, std::string_view str) { + int from = parseSquare(str.substr(0, 2)); + int to = parseSquare(str.substr(2, 2)); + + int flag = 0; + + // Promotion + if (str.length() > 4) { + if (board.GetPieceColor(from) != board.GetPieceColor(to)) { + switch(str[4]) { + case 'r': + flag = rookPromoCapture; + break; + case 'q': + flag = queenPromoCapture; + break; + case 'b': + flag = bishopPromoCapture; + break; + case 'n': + flag = knightPromoCapture; + break; + } + } else { + switch(str[4]) { + case 'r': + flag = rookPromotion; + break; + case 'q': + flag = queenPromotion; + break; + case 'b': + flag = bishopPromotion; + break; + case 'n': + flag = knightPromotion; + break; + } + } + } + + return Move(from, to, flag); +} diff --git a/source/utils.h b/source/utils.h index 6e5ffe0..6c23585 100644 --- a/source/utils.h +++ b/source/utils.h @@ -3,26 +3,11 @@ #include #include #include +#include "move.h" +#include "board.h" -std::vector split(std::string_view str, char delim) { - std::vector results; +std::vector split(std::string_view str, char delim); - std::istringstream stream(std::string{str}); +int parseSquare(std::string_view str); - for (std::string token{}; std::getline(stream, token, delim);) { - if (token.empty()) continue; - - results.push_back(token); - } - - return results; -} - -int parseSquare(std::string_view str) { - constexpr std::string_view files = "abcdefgh"; - - int file = files.find(str[0]); - int rank = str[1] - '0'; - - return file + rank * 8; -} +Move parseMove(Board &board, std::string_view str);