Files
cpl_cpp-project/Move.cpp

60 lines
1.4 KiB
C++

#include "Move.hpp"
#include <ostream>
Move::Move(const Square &from, const Square &to, const std::optional<PieceType> &promotion)
: mFrom(from), mTo(to), mPromotion(promotion) {
}
Move::Optional Move::fromUci(const std::string &uci) {
if (uci.length() < 4 || uci.length() > 5)
return std::nullopt;
std::optional<PieceType> 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 {
return mFrom;
}
Square Move::to() const {
return mTo;
}
std::optional<PieceType> Move::promotion() const {
return mPromotion;
}
std::ostream &operator<<(std::ostream &os, const Move &move) {
os << move.from() << move.to();
if (move.promotion().has_value()) {
os << static_cast<char>(move.promotion().value());
}
return os;
}
bool operator<(const Move &lhs, const Move &rhs) {
(void) lhs;
(void) rhs;
return false;
}
bool operator==(const Move &lhs, const Move &rhs) {
return lhs.from() == rhs.from()
&& lhs.to() == rhs.to()
&& lhs.promotion() == rhs.promotion();
}