90 lines
2.1 KiB
C++
90 lines
2.1 KiB
C++
#include "Move.hpp"
|
|
|
|
#include <ostream>
|
|
#include <iostream>
|
|
|
|
|
|
Move::Move(const Square &from, const Square &to) : mFrom(from), mTo(to) {
|
|
|
|
}
|
|
|
|
Move::Move(const Square &from, const Square &to, const std::optional<PieceType> &promotion, unsigned score)
|
|
: mFrom(from), mTo(to), mPromotion(promotion) {
|
|
mScore -= score;
|
|
}
|
|
|
|
Move::Move(const Square &from, const Square &to, unsigned int score) : mFrom(from), mTo(to) {
|
|
mScore -= score;
|
|
}
|
|
|
|
|
|
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 << move.promotion().value();
|
|
}
|
|
return os;
|
|
}
|
|
|
|
bool operator<(const Move &lhs, const Move &rhs) {
|
|
if (lhs.mScore != rhs.mScore) {
|
|
return lhs.mScore < rhs.mScore;
|
|
}
|
|
|
|
if (lhs.mFrom.index() != rhs.mFrom.index()) {
|
|
return lhs.mFrom.index() < rhs.mFrom.index();
|
|
}
|
|
if (lhs.mTo.index() != rhs.mTo.index()) {
|
|
return lhs.mTo.index() < rhs.mTo.index();
|
|
}
|
|
|
|
return lhs.promotion() < rhs.promotion();
|
|
}
|
|
|
|
bool operator==(const Move &lhs, const Move &rhs) {
|
|
return lhs.score() == rhs.score()
|
|
&& lhs.from() == rhs.from()
|
|
&& lhs.to() == rhs.to()
|
|
&& lhs.promotion() == rhs.promotion();
|
|
}
|
|
|
|
unsigned int Move::score() const {
|
|
return mScore;
|
|
}
|
|
|
|
void Move::setScore(unsigned int score) {
|
|
mScore = score;
|
|
}
|