Compare commits

...

2 Commits

Author SHA1 Message Date
0084bbf994 [Board] Implement castlingRights and enPassantSquare 2022-12-21 10:42:28 +01:00
123e473a53 [Piece] Add fromValue 2022-12-21 10:41:46 +01:00
4 changed files with 19 additions and 13 deletions

View File

@@ -24,7 +24,7 @@ Piece::Optional Board::piece(const Square &square) const {
BitBoard mask = indexToBitBoard(square.index()); BitBoard mask = indexToBitBoard(square.index());
for (const auto &kPieceBb : mPieceBBs) { for (const auto &kPieceBb : mPieceBBs) {
if (kPieceBb & mask) { if (kPieceBb & mask) {
return Piece(indexToColor(i), indexToType(i)); return Piece::fromValue(i);
} }
i++; i++;
@@ -41,19 +41,19 @@ PieceColor Board::turn() const {
} }
void Board::setCastlingRights(CastlingRights cr) { void Board::setCastlingRights(CastlingRights cr) {
(void) cr; mCr = cr;
} }
CastlingRights Board::castlingRights() const { CastlingRights Board::castlingRights() const {
return CastlingRights::None; return mCr;
} }
void Board::setEnPassantSquare(const Square::Optional &square) { void Board::setEnPassantSquare(const Square::Optional &square) {
(void) square; mEPS = square->index();
} }
Square::Optional Board::enPassantSquare() const { Square::Optional Board::enPassantSquare() const {
return std::nullopt; return Square::fromIndex(mEPS);
} }
void Board::makeMove(const Move &move) { void Board::makeMove(const Move &move) {

View File

@@ -40,6 +40,8 @@ private:
BitBoard mPieceBBs[BB_NUM] = {}; BitBoard mPieceBBs[BB_NUM] = {};
PieceColor mTurn = PieceColor::White; PieceColor mTurn = PieceColor::White;
CastlingRights mCr;
unsigned mEPS = 64;
static inline void clearIndex(BitBoard &b, unsigned i) { static inline void clearIndex(BitBoard &b, unsigned i) {
b &= ~(1ULL << i); b &= ~(1ULL << i);
@@ -52,14 +54,6 @@ private:
static inline BitBoard indexToBitBoard(unsigned i) { static inline BitBoard indexToBitBoard(unsigned i) {
return (1ULL << i); return (1ULL << i);
} }
static inline PieceColor indexToColor(unsigned i) {
return static_cast<PieceColor>(i % 2);
}
static inline PieceType indexToType(unsigned i) {
return static_cast<PieceType>(i - (i % 2));
}
}; };
std::ostream &operator<<(std::ostream &os, const Board &board); std::ostream &operator<<(std::ostream &os, const Board &board);

View File

@@ -42,6 +42,9 @@ std::optional<PieceType> Piece::pieceTypeFromSymbol(char symbol) {
default: return std::nullopt; default: return std::nullopt;
} }
} }
Piece::Optional Piece::fromValue(unsigned int value) {
return Piece(valToColor(value), valToType(value));
}
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();

View File

@@ -27,6 +27,7 @@ public:
Piece(PieceColor color, PieceType type); Piece(PieceColor color, PieceType type);
static Optional fromSymbol(char symbol); static Optional fromSymbol(char symbol);
static Optional fromValue(unsigned value);
static std::optional<PieceType> pieceTypeFromSymbol(char symbol); static std::optional<PieceType> pieceTypeFromSymbol(char symbol);
@@ -38,6 +39,14 @@ public:
private: private:
const PieceColor mColor; const PieceColor mColor;
const PieceType mType; const PieceType mType;
static inline PieceColor valToColor(unsigned v) {
return static_cast<PieceColor>(v % 2);
}
static inline PieceType valToType(unsigned v) {
return static_cast<PieceType>(v - (v % 2));
}
}; };
bool operator==(const Piece &lhs, const Piece &rhs); bool operator==(const Piece &lhs, const Piece &rhs);