From 864b46be9e33f949b40d878b0de97cc3f1fcefbf Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Sun, 18 Dec 2022 21:01:48 +0100 Subject: [PATCH] [Move] Implement Move::fromUci --- Move.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Move.cpp b/Move.cpp index 226cb3f..75dec86 100644 --- a/Move.cpp +++ b/Move.cpp @@ -7,8 +7,23 @@ Move::Move(const Square &from, const Square &to, const std::optional } Move::Optional Move::fromUci(const std::string &uci) { - (void) uci; - return std::nullopt; + if (uci.length() < 4 || uci.length() > 5) + return std::nullopt; + + std::optional promotion = std::nullopt; + if (uci.length() == 5) { + promotion = Piece::pieceTypeFromSymbol(uci[4]); + if (!promotion.has_value()) { + return std::nullopt; + } + } + + auto from = Square::fromName(uci.substr(0, 2)); + auto to = Square::fromName(uci.substr(2, 2)); + if (!from.has_value() || !to.has_value()) + return std::nullopt; + + return Move(from.value(), to.value(), promotion); } Square Move::from() const {