[Board] Implement MoveGenerator
This commit is contained in:
18
Board.cpp
18
Board.cpp
@@ -15,18 +15,18 @@ void Board::setPiece(const Square &square, const Piece::Optional &piece) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
auto index = square.index();
|
auto index = square.index();
|
||||||
for (auto &bb : mPieceBBs) {
|
for (int i = 0; i < BB_NUM; i++) {
|
||||||
bb.clear(index);
|
mPieceBBs[i].clear(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
mPieceBBs[toIndex(piece->type())].set(index);
|
mPieceBBs[toIndex(piece->type())].set(index);
|
||||||
mPieceBBs[toIndex(piece->color())].set(index);
|
mPieceBBs[toIndex(piece->color())].set(index);
|
||||||
mOccupiedBB.set(index);
|
(*mOccupiedBB).set(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
Piece::Optional Board::piece(const Square &square) const {
|
Piece::Optional Board::piece(const Square &square) const {
|
||||||
BitBoard mask = BitBoard::fromIndex(square.index());
|
BitBoard mask = BitBoard::fromIndex(square.index());
|
||||||
if (!(mOccupiedBB & mask)) {
|
if (!(*mOccupiedBB & mask)) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,17 +68,17 @@ void Board::makeMove(const Move &move) {
|
|||||||
BitBoard changeBB = fromBB ^ toBB;
|
BitBoard changeBB = fromBB ^ toBB;
|
||||||
|
|
||||||
// If Piece is captured
|
// If Piece is captured
|
||||||
if (mOccupiedBB & toBB) {
|
if (*mOccupiedBB & toBB) {
|
||||||
auto capturedPiece = Piece(!mTurn, pieceType(toBB));
|
auto capturedPiece = Piece(!mTurn, pieceType(toBB));
|
||||||
mPieceBBs[toIndex(capturedPiece.color())] ^= toBB;
|
mPieceBBs[toIndex(capturedPiece.color())] ^= toBB;
|
||||||
mPieceBBs[toIndex(capturedPiece.type())] ^= toBB;
|
mPieceBBs[toIndex(capturedPiece.type())] ^= toBB;
|
||||||
mOccupiedBB ^= fromBB;
|
*mOccupiedBB ^= fromBB;
|
||||||
|
|
||||||
if (toBB & CastlingRanks) { // Check castling rights
|
if (toBB & CastlingRanks) { // Check castling rights
|
||||||
handleCastlingRights(capturedPiece, toBB);
|
handleCastlingRights(capturedPiece, toBB);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mOccupiedBB ^= changeBB; // update occupied bitboard
|
*mOccupiedBB ^= changeBB; // update occupied bitboard
|
||||||
}
|
}
|
||||||
|
|
||||||
auto movedPiece = Piece(mTurn, pieceType(fromBB));
|
auto movedPiece = Piece(mTurn, pieceType(fromBB));
|
||||||
@@ -105,7 +105,7 @@ void Board::makeMove(const Move &move) {
|
|||||||
|
|
||||||
mPieceBBs[toIndex(PieceType::Rook)] ^= rookBB;
|
mPieceBBs[toIndex(PieceType::Rook)] ^= rookBB;
|
||||||
mPieceBBs[toIndex(movedPiece.color())] ^= rookBB;
|
mPieceBBs[toIndex(movedPiece.color())] ^= rookBB;
|
||||||
mOccupiedBB ^= rookBB;
|
*mOccupiedBB ^= rookBB;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +124,7 @@ void Board::handleEnPassant(const Move &move, const Piece &movedPiece) {
|
|||||||
|
|
||||||
mPieceBBs[toIndex(capturedPiece.color())] ^= epBB;
|
mPieceBBs[toIndex(capturedPiece.color())] ^= epBB;
|
||||||
mPieceBBs[toIndex(capturedPiece.type())] ^= epBB;
|
mPieceBBs[toIndex(capturedPiece.type())] ^= epBB;
|
||||||
mOccupiedBB ^= epBB;
|
*mOccupiedBB ^= epBB;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
Board.hpp
24
Board.hpp
@@ -6,10 +6,12 @@
|
|||||||
#include "Move.hpp"
|
#include "Move.hpp"
|
||||||
#include "CastlingRights.hpp"
|
#include "CastlingRights.hpp"
|
||||||
#include "BitBoard.hpp"
|
#include "BitBoard.hpp"
|
||||||
|
#include "MoveGenerator.hpp"
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#define BB_NUM 8 // 6 pieces, 2 colors
|
#define BB_NUM 8 // 6 pieces, 2 colors
|
||||||
|
|
||||||
@@ -35,26 +37,30 @@ public:
|
|||||||
void pseudoLegalMoves(MoveVec &moves) const;
|
void pseudoLegalMoves(MoveVec &moves) const;
|
||||||
void pseudoLegalMovesFrom(const Square &from, MoveVec &moves) const;
|
void pseudoLegalMovesFrom(const Square &from, MoveVec &moves) const;
|
||||||
|
|
||||||
|
static constexpr int toIndex(PieceType t) {
|
||||||
|
return static_cast<int>(t);
|
||||||
|
}
|
||||||
|
static constexpr int toIndex(PieceColor c) {
|
||||||
|
return static_cast<int>(c);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
BitBoard mPieceBBs[BB_NUM] = {};
|
std::shared_ptr<BitBoard[BB_NUM]> mPieceBBs = std::shared_ptr<BitBoard[BB_NUM]>(new BitBoard[BB_NUM]);
|
||||||
BitBoard mOccupiedBB = 0;
|
// BitBoard mPieceBBs[BB_NUM] = {};
|
||||||
|
std::shared_ptr<BitBoard> mOccupiedBB = std::make_shared<BitBoard>(0);
|
||||||
|
|
||||||
|
std::shared_ptr<MoveGenerator> mMoveGenerator = std::make_shared<MoveGenerator>(mPieceBBs, mOccupiedBB);
|
||||||
|
|
||||||
PieceColor mTurn = PieceColor::White;
|
PieceColor mTurn = PieceColor::White;
|
||||||
CastlingRights mCR = CastlingRights::None;
|
CastlingRights mCR = CastlingRights::None;
|
||||||
std::optional<Square> mEPS;
|
std::optional<Square> mEPS;
|
||||||
|
|
||||||
void handleCastlingRights(const Piece &piece, const BitBoard &bb);
|
void handleCastlingRights(const Piece &piece, const BitBoard &bb);
|
||||||
|
constexpr bool hasCastlingRights() const;
|
||||||
// Check if the move is castling without checking the rights or validity.
|
// Check if the move is castling without checking the rights or validity.
|
||||||
static bool isMoveCastling(const BitBoard &from, const BitBoard &to, const Piece &piece);
|
static bool isMoveCastling(const BitBoard &from, const BitBoard &to, const Piece &piece);
|
||||||
|
|
||||||
static inline int toIndex(PieceType t) {
|
|
||||||
return static_cast<int>(t);
|
|
||||||
}
|
|
||||||
static inline int toIndex(PieceColor c) {
|
|
||||||
return static_cast<int>(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline PieceColor pieceColor(const BitBoard &mask) const {
|
inline PieceColor pieceColor(const BitBoard &mask) const {
|
||||||
auto color = PieceColor::White;
|
auto color = PieceColor::White;
|
||||||
if (!(mPieceBBs[static_cast<unsigned>(color)] & mask)) {
|
if (!(mPieceBBs[static_cast<unsigned>(color)] & mask)) {
|
||||||
|
Reference in New Issue
Block a user