diff --git a/source/board.cpp b/source/board.cpp index 4d74aa4..a81308f 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -6,7 +6,7 @@ void Board::Init() { } void Board::Reset() { - castlingRights = { false, false, false, false }; + castlingRights = 0; enPassantTarget = -1; halfMoves = 0; fullMoves = 0; @@ -124,16 +124,16 @@ void Board::SetByFen(const char* fen) { } break; case 'K': - castlingRights[White] = true; + castlingRights |= whiteKingRight; break; case 'Q': - castlingRights[White + 1] = true; + castlingRights |= whiteQueenRight; break; case 'k': - castlingRights[Black * 2] = true; + castlingRights |= blackKingRight; break; case 'q': - castlingRights[Black * 2 + 1] = true; + castlingRights |= blackQueenRight; break; default: if (*fen != '-') { @@ -229,10 +229,10 @@ void Board::PrintBoard() { } std::cout << " Castling rights: "; - if (castlingRights[0]) std::cout << "K"; else std::cout << "-"; - if (castlingRights[1]) std::cout << "Q"; else std::cout << "-"; - if (castlingRights[2]) std::cout << "k"; else std::cout << "-"; - if (castlingRights[3]) std::cout << "q"; else std::cout << "-"; + if (castlingRights & whiteKingRight) std::cout << "K"; else std::cout << "-"; + if (castlingRights & whiteQueenRight) std::cout << "Q"; else std::cout << "-"; + if (castlingRights & blackKingRight) std::cout << "k"; else std::cout << "-"; + if (castlingRights & blackQueenRight) std::cout << "q"; else std::cout << "-"; std::cout << '\n'; } @@ -266,18 +266,8 @@ int Board::GetPieceColor(int square) { return (colors[Black].IsSet(square)); } -U64 Board::GetAttackMaps(bool side) { - U64 combined = 0ULL; - for (int type = Pawn; type <= King; type++) { - combined |= attackMaps[side][type]; - } - return combined; -} - bool Board::InCheck(bool side) { - Bitboard myKingSquare = colors[side] & pieces[King]; - return GetAttackMaps(!side) & myKingSquare; } void Board::SetPiece(int piece, int square, bool color) { @@ -299,13 +289,13 @@ static void UpdateCastlingRights(Board &board, int square, int type, int color) int kingSideRook = color ? h8 : h1; if (square == queenSideRook) { - board.castlingRights[color * 2 + 1] = false; + board.castlingRights &= color ? blackQueenRight : whiteQueenRight; } else if (square == kingSideRook) { - board.castlingRights[color * 2] = false; + board.castlingRights &= color ? blackKingRight : whiteKingRight; } } else if (type == King) { - board.castlingRights[color * 2 + 1] = false; - board.castlingRights[color * 2] = false; + board.castlingRights &= color ? blackQueenRight : whiteQueenRight; + board.castlingRights &= color ? blackKingRight : whiteKingRight; } } @@ -409,7 +399,7 @@ void Board::MakeMove(Move move) { UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor); halfMoves++; - if (halfMoves & 2 == 0) fullMoves++; + if (halfMoves % 2 == 0) fullMoves++; sideToMove = !attackerColor; enPassantTarget = newEpTarget; } diff --git a/source/board.h b/source/board.h index f324801..5af5cbd 100644 --- a/source/board.h +++ b/source/board.h @@ -3,7 +3,6 @@ #include "bitboards.h" #include "types.h" #include "move.h" -#include class Board { public: @@ -13,7 +12,6 @@ public: std::array pieces; std::array colors; - U64 attackMaps[2][64]; Bitboard occupied; @@ -24,7 +22,7 @@ public: int halfMoves = 0; int fullMoves = 0; - std::array castlingRights = { false, false, false, false }; + uint8_t castlingRights = 0; int enPassantTarget; void Init(); @@ -39,7 +37,6 @@ public: int GetPieceType(int square); int GetPieceColor(int square); - U64 GetAttackMaps(bool side); bool InCheck(bool side); void SetPiece(int piece, int square, bool color); diff --git a/source/movegen.cpp b/source/movegen.cpp index 0268958..b8e8fc3 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -510,7 +510,7 @@ static void GenKingMoves(Board &board, bool color) { } // Checking for king side castle - if (board.castlingRights[color * 2]) { + if (board.castlingRights & (whiteKingRight | blackKingRight)) { Bitboard mask = color ? 0xe000000000000000 : 0xe0; if ((board.occupied & mask).PopCount() == 1) { @@ -518,8 +518,8 @@ static void GenKingMoves(Board &board, bool color) { } } - // Checking for quuen side castle - if (board.castlingRights[color * 2 + 1]) { + // Checking for queen side castle + if (board.castlingRights & (whiteQueenRight | blackQueenRight)) { Bitboard mask = color ? 0xf00000000000000 : 0xf; if ((board.occupied & mask).PopCount() == 1) { diff --git a/source/types.h b/source/types.h index 7b1cd65..809a86d 100644 --- a/source/types.h +++ b/source/types.h @@ -20,6 +20,13 @@ enum PIECE { King }; +enum CASTLING_RIGHTS { + whiteKingRight = 1, + whiteQueenRight = 2, + blackKingRight = 4, + blackQueenRight = 8 +}; + constexpr std::array PIECE_LETTERS = { 'P', 'p', 'N', 'n', @@ -87,4 +94,4 @@ enum DIRECTION { noEa = north + east, soWe = south + west, soEa = south + east -}; \ No newline at end of file +};