[Move] Fix streaming with promotion
This commit is contained in:
3
Move.cpp
3
Move.cpp
@@ -1,6 +1,7 @@
|
|||||||
#include "Move.hpp"
|
#include "Move.hpp"
|
||||||
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
Move::Move(const Square &from, const Square &to, const std::optional<PieceType> &promotion)
|
Move::Move(const Square &from, const Square &to, const std::optional<PieceType> &promotion)
|
||||||
: mFrom(from), mTo(to), mPromotion(promotion) {
|
: mFrom(from), mTo(to), mPromotion(promotion) {
|
||||||
@@ -41,7 +42,7 @@ std::optional<PieceType> Move::promotion() const {
|
|||||||
std::ostream &operator<<(std::ostream &os, const Move &move) {
|
std::ostream &operator<<(std::ostream &os, const Move &move) {
|
||||||
os << move.from() << move.to();
|
os << move.from() << move.to();
|
||||||
if (move.promotion().has_value()) {
|
if (move.promotion().has_value()) {
|
||||||
os << static_cast<char>(move.promotion().value());
|
os << move.promotion().value();
|
||||||
}
|
}
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
44
Piece.cpp
44
Piece.cpp
@@ -35,38 +35,42 @@ std::optional<PieceType> Piece::pieceTypeFromSymbol(char symbol) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char Piece::toSymbol() const {
|
||||||
|
return toSymbol(mType);
|
||||||
|
}
|
||||||
|
|
||||||
|
char Piece::toSymbol(PieceType type) {
|
||||||
|
switch (type) {
|
||||||
|
case PieceType::Pawn: return 'p';
|
||||||
|
case PieceType::Knight: return 'n';
|
||||||
|
case PieceType::Bishop: return 'b';
|
||||||
|
case PieceType::Rook: return 'r';
|
||||||
|
case PieceType::Queen: return 'q';
|
||||||
|
case PieceType::King: return 'k';
|
||||||
|
}
|
||||||
|
|
||||||
|
return '\0';
|
||||||
|
}
|
||||||
|
|
||||||
bool operator==(const Piece &lhs, const Piece &rhs) {
|
bool operator==(const Piece &lhs, const Piece &rhs) {
|
||||||
return lhs.color() == rhs.color() && lhs.type() == rhs.type();
|
return lhs.color() == rhs.color() && lhs.type() == rhs.type();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const Piece &piece) {
|
std::ostream &operator<<(std::ostream &os, const Piece &piece) {
|
||||||
int typeNum;
|
int typeNum = piece.toSymbol();
|
||||||
|
|
||||||
switch (piece.type()) {
|
if (piece.color() == PieceColor::White) {
|
||||||
case PieceType::Pawn: typeNum = 'P';
|
typeNum = std::toupper(typeNum);
|
||||||
break;
|
|
||||||
case PieceType::Knight: typeNum = 'N';
|
|
||||||
break;
|
|
||||||
case PieceType::Bishop: typeNum = 'B';
|
|
||||||
break;
|
|
||||||
case PieceType::Rook:typeNum = 'R';
|
|
||||||
break;
|
|
||||||
case PieceType::Queen: typeNum = 'Q';
|
|
||||||
break;
|
|
||||||
case PieceType::King: typeNum = 'K';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return os;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (piece.color() == PieceColor::Black) {
|
|
||||||
typeNum = std::tolower(typeNum);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
os << static_cast<char>(typeNum);
|
os << static_cast<char>(typeNum);
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &os, const PieceType &pt) {
|
||||||
|
return os << Piece::toSymbol(pt);
|
||||||
|
}
|
||||||
|
|
||||||
PieceColor operator!(PieceColor color) {
|
PieceColor operator!(PieceColor color) {
|
||||||
return static_cast<PieceColor>((static_cast<int>(color) + 1) % 2);
|
return static_cast<PieceColor>((static_cast<int>(color) + 1) % 2);
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,8 @@ enum class PieceType {
|
|||||||
King
|
King
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::ostream &operator<<(std::ostream &os, const PieceType &pt);
|
||||||
|
|
||||||
class Piece {
|
class Piece {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -27,11 +29,13 @@ public:
|
|||||||
Piece(PieceColor color, PieceType type);
|
Piece(PieceColor color, PieceType type);
|
||||||
|
|
||||||
static Optional fromSymbol(char symbol);
|
static Optional fromSymbol(char symbol);
|
||||||
|
static char toSymbol(PieceType type);
|
||||||
|
|
||||||
static std::optional<PieceType> pieceTypeFromSymbol(char symbol);
|
static std::optional<PieceType> pieceTypeFromSymbol(char symbol);
|
||||||
|
|
||||||
PieceColor color() const;
|
PieceColor color() const;
|
||||||
PieceType type() const;
|
PieceType type() const;
|
||||||
|
char toSymbol() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const PieceColor mColor;
|
const PieceColor mColor;
|
||||||
|
Reference in New Issue
Block a user