From b64530cb3df6785671ba91f5caf2ec21ea6cce3a Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Thu, 22 Dec 2022 18:37:52 +0100 Subject: [PATCH] [Board] Implement MoveGenerator --- Board.cpp | 18 +++++++++--------- Board.hpp | 24 +++++++++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Board.cpp b/Board.cpp index 11e4698..af39734 100644 --- a/Board.cpp +++ b/Board.cpp @@ -15,18 +15,18 @@ void Board::setPiece(const Square &square, const Piece::Optional &piece) { return; auto index = square.index(); - for (auto &bb : mPieceBBs) { - bb.clear(index); + for (int i = 0; i < BB_NUM; i++) { + mPieceBBs[i].clear(index); } mPieceBBs[toIndex(piece->type())].set(index); mPieceBBs[toIndex(piece->color())].set(index); - mOccupiedBB.set(index); + (*mOccupiedBB).set(index); } Piece::Optional Board::piece(const Square &square) const { BitBoard mask = BitBoard::fromIndex(square.index()); - if (!(mOccupiedBB & mask)) { + if (!(*mOccupiedBB & mask)) { return std::nullopt; } @@ -68,17 +68,17 @@ void Board::makeMove(const Move &move) { BitBoard changeBB = fromBB ^ toBB; // If Piece is captured - if (mOccupiedBB & toBB) { + if (*mOccupiedBB & toBB) { auto capturedPiece = Piece(!mTurn, pieceType(toBB)); mPieceBBs[toIndex(capturedPiece.color())] ^= toBB; mPieceBBs[toIndex(capturedPiece.type())] ^= toBB; - mOccupiedBB ^= fromBB; + *mOccupiedBB ^= fromBB; if (toBB & CastlingRanks) { // Check castling rights handleCastlingRights(capturedPiece, toBB); } } else { - mOccupiedBB ^= changeBB; // update occupied bitboard + *mOccupiedBB ^= changeBB; // update occupied bitboard } auto movedPiece = Piece(mTurn, pieceType(fromBB)); @@ -105,7 +105,7 @@ void Board::makeMove(const Move &move) { mPieceBBs[toIndex(PieceType::Rook)] ^= 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.type())] ^= epBB; - mOccupiedBB ^= epBB; + *mOccupiedBB ^= epBB; } } diff --git a/Board.hpp b/Board.hpp index 0680a41..01715f0 100644 --- a/Board.hpp +++ b/Board.hpp @@ -6,10 +6,12 @@ #include "Move.hpp" #include "CastlingRights.hpp" #include "BitBoard.hpp" +#include "MoveGenerator.hpp" #include #include #include +#include #define BB_NUM 8 // 6 pieces, 2 colors @@ -35,26 +37,30 @@ public: void pseudoLegalMoves(MoveVec &moves) const; void pseudoLegalMovesFrom(const Square &from, MoveVec &moves) const; + static constexpr int toIndex(PieceType t) { + return static_cast(t); + } + static constexpr int toIndex(PieceColor c) { + return static_cast(c); + } + private: - BitBoard mPieceBBs[BB_NUM] = {}; - BitBoard mOccupiedBB = 0; + std::shared_ptr mPieceBBs = std::shared_ptr(new BitBoard[BB_NUM]); +// BitBoard mPieceBBs[BB_NUM] = {}; + std::shared_ptr mOccupiedBB = std::make_shared(0); + + std::shared_ptr mMoveGenerator = std::make_shared(mPieceBBs, mOccupiedBB); PieceColor mTurn = PieceColor::White; CastlingRights mCR = CastlingRights::None; std::optional mEPS; void handleCastlingRights(const Piece &piece, const BitBoard &bb); + constexpr bool hasCastlingRights() const; // 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 inline int toIndex(PieceType t) { - return static_cast(t); - } - static inline int toIndex(PieceColor c) { - return static_cast(c); - } - inline PieceColor pieceColor(const BitBoard &mask) const { auto color = PieceColor::White; if (!(mPieceBBs[static_cast(color)] & mask)) {