From 119a87f227e0276824bb78fc8a8696b986b43da0 Mon Sep 17 00:00:00 2001 From: "rektdie@gmail.com" Date: Mon, 13 May 2024 18:04:36 +0200 Subject: [PATCH 1/7] cleaned up movegen --- Makefile | 1 - source/board.cpp | 3 - source/main.cpp | 4 +- source/movegen.cpp | 639 ++------------------------------------------- source/movegen.h | 4 +- source/perft.cpp | 4 +- source/search.cpp | 4 +- source/uci.cpp | 2 +- 8 files changed, 24 insertions(+), 637 deletions(-) diff --git a/Makefile b/Makefile index 0412a89..29dab75 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,6 @@ OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS:.cpp=.o))) # Target executable Eleanor$(EXE_EXT): $(OBJS) g++ -o $@ $^ - # Rule to compile source files to object files $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/types.h | $(OBJ_DIR) g++ -c -o $@ $< diff --git a/source/board.cpp b/source/board.cpp index fee5ad7..87a800b 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -183,7 +183,6 @@ void Board::SetByFen(const char* fen) { } occupied = (colors[White] | colors[Black]); - GenAttackMaps(*this); } void Board::PrintBoard() { @@ -411,6 +410,4 @@ void Board::MakeMove(Move move) { if (halfMoves & 2 == 0) fullMoves++; sideToMove = !attackerColor; enPassantTarget = newEpTarget; - - GenAttackMaps(*this); } \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 996e312..2ff7512 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -11,7 +11,9 @@ int main() { Board board; - UCILoop(board); + // UCILoop(board); + + Perft(board, 10); return 0; } \ No newline at end of file diff --git a/source/movegen.cpp b/source/movegen.cpp index 1281913..4758cae 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -285,35 +285,6 @@ Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy) { } } -static Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy) { - Bitboard pushes; - - int direction = color ? south : north; - Bitboard secondRank = color ? ranks[r_7] : ranks[r_2]; - - if (!occupancy.IsSet(square + direction)) { - pushes.SetBit(square + direction); - - if (secondRank.IsSet(square) && !occupancy.IsSet(square + 2 * direction)) { - pushes.SetBit(square + 2 * direction); - } - } - - return pushes; -} - -// Returns bitboard of checkers -static Bitboard Checkers(Board &board, bool color) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - Bitboard checkerBoard = 0ULL; - - for (int type = Pawn; type < King; type++) { - Bitboard targets = getPieceAttacks(kingSquare, type, color, board.occupied.GetBoard()); - checkerBoard |= targets & board.pieces[type] & board.colors[!color]; - } - return checkerBoard; -} - // Returns the direction to target from square static int GetDirection(int square, int target) { int rankDiff = (target / 8) - (square / 8); @@ -346,225 +317,8 @@ static int GetDirection(int square, int target) { return 0; } -// Returns a bitboard of the squares from attacker to target -static Bitboard SliderRayToSquare(int attacker, int target) { - Bitboard ray; - int direction = GetDirection(attacker, target); - - if (direction) { - while (attacker != target) { - ray.SetBit(attacker); - attacker += direction; - } - ray.SetBit(target); - } - - return ray; -} - -static Bitboard GetPinningRay(Board &board, int square, int direction) { - Bitboard ray; - int currentSquare = (board.pieces[King] & board.colors[board.sideToMove]).getLS1BIndex(); - int previousRank = currentSquare / 8; - - currentSquare += direction; - while (currentSquare < 64 && currentSquare >= 0) { - int currentRank = currentSquare / 8; - - int difference = abs(currentRank - previousRank); - - if ((abs(direction) == 7 || abs(direction) == 9) - && difference != 1) { - break; - } else if (abs(direction) == 1 && difference) { - break; - } - previousRank = currentRank; - - ray.SetBit(currentSquare); - if (board.occupied.IsSet(currentSquare)) { - if (currentSquare == square) { - currentSquare += direction; - continue; - } - int attackerType = board.GetPieceType(currentSquare); - int attackerColor = board.GetPieceColor(currentSquare); - - if (attackerColor != board.sideToMove) { - // Pinned by rook or queen - if (abs(direction) == 1 || abs(direction) == 8) { - if (attackerType == Rook || attackerType == Queen) { - return ray; - } - // Pinned by bishop or queen - } else if (direction % 2 != 0) { - if (attackerType == Bishop || attackerType == Queen) { - return ray; - } - } - } - break; - } - currentSquare += direction; - } - - return 0ULL; -} - -static bool IsPinned(Board &board, int square) { - int kingSquare = (board.colors[board.sideToMove] & board.pieces[King]).getLS1BIndex(); - int directionToKing = GetDirection(square, kingSquare); - - if (directionToKing) { - // Checking if there are any pieces between square and kingSquare - if ((SliderRayToSquare(square, kingSquare) - & board.occupied).PopCount() != 2) { - return false; - } - - return GetPinningRay(board, square, -directionToKing).PopCount(); - } - - return false; -} - -static bool IsLegalEnPassant(Board &board, int square) { - Bitboard kingSquare = board.colors[board.sideToMove] & board.pieces[King]; - int pushDirection = board.sideToMove ? south : north; - Bitboard newOccupancy = board.occupied; - newOccupancy.PopBit(square); - newOccupancy.PopBit(board.enPassantTarget - pushDirection); - - // Looking for horizontal checks - int directionToKing = GetDirection(square, kingSquare.getLS1BIndex()); - - // Either east or west - if (abs(directionToKing) == 1) { - int currentSq = kingSquare.getLS1BIndex() - directionToKing; - - Bitboard endingFile = directionToKing > 0 ? files[H] : files[A]; - - while (!endingFile.IsSet(currentSq)) { - if (newOccupancy.IsSet(currentSq)) { - int squareColor = board.GetPieceColor(currentSq); - int squarePiece = board.GetPieceType(currentSq); - - if (squareColor != board.sideToMove) { - if (squarePiece == Rook || squarePiece == Queen) { - return false; - } - } - break; - } - currentSq -= directionToKing; - } - } - - return true; -} - static void GenPawnMoves(Board &board, bool color) { - Bitboard pawns = board.colors[color] & board.pieces[Pawn]; - while (pawns.GetBoard()) { - int square = pawns.getLS1BIndex(); - - Bitboard pushes = getPawnPushes(square, color, board.occupied); - Bitboard captures = pawnAttacks[color][square]; - - if (IsPinned(board, square)) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - int directionToKing = GetDirection(square, kingSquare); - - Bitboard pinningRay = GetPinningRay(board, square, -directionToKing); - pushes &= pinningRay; - captures &= pinningRay; - - // Checking for en passant - if (captures.IsSet(board.enPassantTarget)) { - if (IsLegalEnPassant(board, square)) { - board.AddMove(Move(square, board.enPassantTarget, epCapture)); - } - } - - // Removing own pieces from capture mask - captures &= board.colors[!color]; - - while (pushes.GetBoard()) { - int pushSquare = pushes.getLS1BIndex(); - - if (abs(square - pushSquare) == 16) { - board.AddMove(Move(square, pushSquare, doublePawnPush)); - } else { - board.AddMove(Move(square, pushSquare, quiet)); - } - - pushes.PopBit(pushSquare); - } - - while (captures.GetBoard()) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - pawns.PopBit(square); - continue; - } - - // Checking for en passant - if (captures.IsSet(board.enPassantTarget)) { - // Checking if the move gives discovered check to own king - if (IsLegalEnPassant(board, square)) { - board.AddMove(Move(square, board.enPassantTarget, epCapture)); - } - } - - // Removing own pieces from capture mask - captures &= board.colors[!color]; - - Bitboard lastRank = color ? ranks[r_1] : ranks[r_8]; - - while (pushes.GetBoard()) { - int pushSquare = pushes.getLS1BIndex(); - - // Checking for promotion - if (lastRank.IsSet(pushSquare)) { - board.AddMove(Move(square, pushSquare, knightPromotion)); - board.AddMove(Move(square, pushSquare, bishopPromotion)); - board.AddMove(Move(square, pushSquare, rookPromotion)); - board.AddMove(Move(square, pushSquare, queenPromotion)); - } else { - if (abs(square - pushSquare) == 16) { - board.AddMove(Move(square, pushSquare, doublePawnPush)); - } else { - board.AddMove(Move(square, pushSquare, quiet)); - } - } - - pushes.PopBit(pushSquare); - } - - while (captures.GetBoard()) { - int targetSquare = captures.getLS1BIndex(); - - // Checking for promotion - if (lastRank.IsSet(targetSquare)) { - board.AddMove(Move(square, targetSquare, knightPromoCapture)); - board.AddMove(Move(square, targetSquare, bishopPromoCapture)); - board.AddMove(Move(square, targetSquare, rookPromoCapture)); - board.AddMove(Move(square, targetSquare, queenPromoCapture)); - } else { - board.AddMove(Move(square, targetSquare, capture)); - } - - captures.PopBit(targetSquare); - } - - pawns.PopBit(square); - } } static void GenKnightMoves(Board &board, bool color) { @@ -573,22 +327,9 @@ static void GenKnightMoves(Board &board, bool color) { while (knights.GetBoard()) { int square = knights.getLS1BIndex(); - if (IsPinned(board, square)) { - knights.PopBit(square); - continue; - } - Bitboard moves = knightAttacks[square] & ~board.colors[color]; Bitboard captures = moves & board.colors[!color]; - moves &= ~captures; - - while (moves.GetBoard()) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } + Bitboard quietMoves = moves & ~board.colors[!color]; while (captures.GetBoard()) { int targetSquare = captures.getLS1BIndex(); @@ -598,391 +339,41 @@ static void GenKnightMoves(Board &board, bool color) { captures.PopBit(targetSquare); } + while (quietMoves.GetBoard()) { + int targetSquare = quietMoves.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, quiet)); + + quietMoves.PopBit(targetSquare); + } + knights.PopBit(square); - } + } } static void GenRookMoves(Board &board, bool color) { - Bitboard rooks = board.colors[color] & board.pieces[Rook]; - while (rooks.GetBoard()) { - int square = rooks.getLS1BIndex(); - - Bitboard moves = getRookAttack(square, board.occupied.GetBoard()) & ~board.colors[color]; - Bitboard captures = moves & board.colors[!color]; - moves &= ~captures; - - if (IsPinned(board, square)) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - int directionToKing = GetDirection(square, kingSquare); - - Bitboard pinningRay = GetPinningRay(board, square, -directionToKing); - - moves &= pinningRay; - captures &= pinningRay; - - while (moves.GetBoard()) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures.GetBoard()) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - rooks.PopBit(square); - continue; - } - - - while (moves.GetBoard()) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures.GetBoard()) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - rooks.PopBit(square); - } } static void GenBishopMoves(Board &board, bool color) { - Bitboard bishops = board.colors[color] & board.pieces[Bishop]; - while (bishops.GetBoard()) { - int square = bishops.getLS1BIndex(); - - Bitboard moves = getBishopAttack(square, board.occupied.GetBoard()) & ~board.colors[color]; - Bitboard captures = moves & board.colors[!color]; - moves &= ~captures; - - if (IsPinned(board, square)) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - int directionToKing = GetDirection(square, kingSquare); - - Bitboard pinningRay = GetPinningRay(board, square, -directionToKing); - - moves &= pinningRay; - captures &= pinningRay; - - while (moves.GetBoard()) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures.GetBoard()) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - bishops.PopBit(square); - continue; - } - - - while (moves.GetBoard()) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures.GetBoard()) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - bishops.PopBit(square); - } } static void GenQueenMoves(Board &board, bool color) { - Bitboard queens = board.colors[color] & board.pieces[Queen]; - while (queens.GetBoard()) { - int square = queens.getLS1BIndex(); - - Bitboard moves = getQueenAttack(square, board.occupied.GetBoard()) & ~board.colors[color]; - Bitboard captures = moves & board.colors[!color]; - moves &= ~captures; - - if (IsPinned(board, square)) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - int directionToKing = GetDirection(square, kingSquare); - - Bitboard pinningRay = GetPinningRay(board, square, -directionToKing); - - moves &= pinningRay; - captures &= pinningRay; - - while (moves.GetBoard()) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures.GetBoard()) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - queens.PopBit(square); - continue; - } - - - while (moves.GetBoard()) { - int targetSquare = moves.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, quiet)); - - moves.PopBit(targetSquare); - } - - while (captures.GetBoard()) { - int targetSquare = captures.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); - - captures.PopBit(targetSquare); - } - - queens.PopBit(square); - } } -static void GenKingMoves(Board &board, bool color, bool isInCheck) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - - Bitboard moves = (kingAttacks[kingSquare] & ~board.GetAttackMaps(!color)) & ~board.colors[color]; - Bitboard captures = moves & board.colors[!color]; - moves &= ~board.colors[!color]; - - while (moves.GetBoard()) { - int square = moves.getLS1BIndex(); - - board.AddMove(Move(kingSquare, square, quiet)); - - moves.PopBit(square); - } - - while (captures.GetBoard()) { - int square = captures.getLS1BIndex(); - - Move move(kingSquare, square, capture); - board.AddMove(move); - - captures.PopBit(square); - } - - // Generating castles - if (!isInCheck) { - if (board.castlingRights[color * 2 + 1]) { - U64 QueenSide = color ? 0x1f00000000000000 : 0x1f; - U64 mask = color ? 0x1c00000000000000 : 0x1c; - int targetSquare = color ? c8 : c1; - if ((Bitboard(QueenSide) & board.occupied).PopCount() == 2 - && !(mask & board.GetAttackMaps(!color))) { - board.AddMove(Move(kingSquare, targetSquare, queenCastle)); - } - } - - if (board.castlingRights[color * 2]) { - U64 KingSide = color ? 0xf000000000000000 : 0xf0; - U64 mask = color ? 0x7000000000000000 : 0x70; - int targetSquare = color ? g8 : g1; - if ((Bitboard(KingSide) & board.occupied).PopCount() == 2 - && !(mask & board.GetAttackMaps(!color))) { - board.AddMove(Move(kingSquare, targetSquare, kingCastle)); - } - } - } +static void GenKingMoves(Board &board, bool color) { + } -// Generates moves to get out of check -static void GenCheckEvasions(Board &board, bool color) { - int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); - - // Adding moves that step out of check - GenKingMoves(board, color, true); - - Bitboard captureMask = Checkers(board, color); // Checker locations - Bitboard pushMask; // Where blockers can move to - if (captureMask.PopCount() == 1) { - int checkerSquare = captureMask.getLS1BIndex(); - int checkerType = board.GetPieceType(checkerSquare); - - if (checkerType == Rook || checkerType == Bishop || checkerType == Queen) { - pushMask = SliderRayToSquare(checkerSquare, kingSquare); - pushMask.PopBit(kingSquare); - pushMask.PopBit(checkerSquare); - } - - // looping through own pieces to add captures and blocks - for (int type = Pawn; type < King; type++) { - Bitboard currentPiece = board.colors[color] & board.pieces[type]; - - while (currentPiece.GetBoard()) { - int square = currentPiece.getLS1BIndex(); - - if (IsPinned(board, square)) { - currentPiece.PopBit(square); - continue; - } - - Bitboard captures; - Bitboard pushes; - - if (type == Pawn) { - pushes |= getPawnPushes(square, color, board.occupied) & pushMask; - // Checking for en passant capture - Bitboard attackedSquare = pawnAttacks[color][square] - & Bitboard::GetSquare(board.enPassantTarget); - if (attackedSquare.GetBoard()) { - // Checking if it's legal - if (IsLegalEnPassant(board, square)) { - int direction = color ? south : north; - int targetSquare = captureMask.getLS1BIndex() + direction; - captureMask.SetBit(targetSquare); - if ((attackedSquare & pushMask).GetBoard() - || (attackedSquare & captureMask).GetBoard()) { - board.AddMove(Move(square, board.enPassantTarget, epCapture)); - } - captureMask.PopBit(targetSquare); - } - } - } else { - pushes |= getPieceAttacks(square, type, color, board.occupied.GetBoard()) & pushMask; - } - - captures |= getPieceAttacks(square, type, color, board.occupied.GetBoard()) & captureMask; - - Bitboard lastRank = color ? ranks[r_1] : ranks[r_8]; - - while (captures.GetBoard()) { - int captureSquare = captures.getLS1BIndex(); - - // Checking for promotion - if (type == Pawn) { - if (lastRank.IsSet(captureSquare)) { - board.AddMove(Move(square, captureSquare, knightPromoCapture)); - board.AddMove(Move(square, captureSquare, bishopPromoCapture)); - board.AddMove(Move(square, captureSquare, rookPromoCapture)); - board.AddMove(Move(square, captureSquare, queenPromoCapture)); - } else { - board.AddMove(Move(square, captureSquare, capture)); - } - } else { - board.AddMove(Move(square, captureSquare, capture)); - } - - captures.PopBit(captureSquare); - } - - while (pushes.GetBoard()) { - int pushSquare = pushes.getLS1BIndex(); - - if (type == Pawn) { - // Checking for promotion - if (lastRank.IsSet(pushSquare)) { - board.AddMove(Move(square, pushSquare, knightPromotion)); - board.AddMove(Move(square, pushSquare, bishopPromotion)); - board.AddMove(Move(square, pushSquare, rookPromotion)); - board.AddMove(Move(square, pushSquare, queenPromotion)); - } else { - if (abs(square - pushSquare) == 16) { - board.AddMove(Move(square, pushSquare, doublePawnPush)); - } else { - board.AddMove(Move(square, pushSquare, quiet)); - } - } - } else { - board.AddMove(Move(square, pushSquare, quiet)); - } - - pushes.PopBit(pushSquare); - } - - currentPiece.PopBit(square); - } - } - } else { // We can only step out of double check - return; - } -} - -void GenAttackMaps(Board &board) { - for (int color = White; color <= Black; color++) { - for (int type = Pawn; type <= King; type++) { - board.attackMaps[color][type] = 0ULL; - Bitboard pieces = board.pieces[type] & board.colors[color]; - - while (pieces.GetBoard()) { - int square = pieces.getLS1BIndex(); - if (type == Pawn) { - board.attackMaps[color][type] |= pawnAttacks[color][square].GetBoard(); - } else if (type == Knight) { - board.attackMaps[color][type] |= knightAttacks[square].GetBoard(); - } else if (type == King) { - board.attackMaps[color][type] |= kingAttacks[square].GetBoard(); - } else if (type == Rook) { - board.attackMaps[color][type] |= getRookAttack(square, (board.occupied - & ~(board.colors[!color] & board.pieces[King])).GetBoard()).GetBoard(); - } else if (type == Bishop) { - board.attackMaps[color][type] |= getBishopAttack(square, (board.occupied - & ~(board.colors[!color] & board.pieces[King])).GetBoard()).GetBoard(); - } else if (type == Queen) { - board.attackMaps[color][type] |= getQueenAttack(square, (board.occupied - & ~(board.colors[!color] & board.pieces[King])).GetBoard()).GetBoard(); - } - pieces.PopBit(square); - } - } - } -} - -void GenerateMoves(Board &board, bool side) { +void GenerateMoves(Board &board) { + bool side = board.sideToMove; board.moveList.clear(); - if (board.InCheck(side)) { - GenCheckEvasions(board, side); - return; - } - GenPawnMoves(board, side); GenKnightMoves(board, side); - GenKingMoves(board, side, false); + GenKingMoves(board, side); GenBishopMoves(board, side); GenRookMoves(board, side); GenQueenMoves(board, side); diff --git a/source/movegen.h b/source/movegen.h index 98f3eee..abae6b2 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -208,6 +208,4 @@ Bitboard getQueenAttack(int square, U64 occupancy); // Returns attacks regardless of board (so occupancy is 0ULL) Bitboard getPieceAttacks(int square, int piece, int color); -void GenAttackMaps(Board &board); - -void GenerateMoves(Board &board, bool side); \ No newline at end of file +void GenerateMoves(Board &board); \ No newline at end of file diff --git a/source/perft.cpp b/source/perft.cpp index 4cd6265..cd0ee55 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -2,7 +2,7 @@ #include "perft.h" U64 HelperPerft(Board board, int depth) { - GenerateMoves(board, board.sideToMove); + GenerateMoves(board); if (depth == 1) return board.moveList.size(); if (depth == 0) return 1ULL; @@ -19,7 +19,7 @@ U64 HelperPerft(Board board, int depth) { } void Perft(Board &board, int depth) { - GenerateMoves(board, board.sideToMove); + GenerateMoves(board); U64 totalNodes = 0; diff --git a/source/search.cpp b/source/search.cpp index d4569fc..d78ebbc 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -17,7 +17,7 @@ int Quiescence(Board board, int alpha, int beta) { alpha = staticScore; } - GenerateMoves(board, board.sideToMove); + GenerateMoves(board); for (Move move : board.moveList) { if (move.IsCapture()) { @@ -40,7 +40,7 @@ int Quiescence(Board board, int alpha, int beta) { } int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) { - GenerateMoves(board, board.sideToMove); + GenerateMoves(board); if (board.moveList.size() == 0) { if (board.InCheck(board.sideToMove)) { // checkmate diff --git a/source/uci.cpp b/source/uci.cpp index a1b94f0..c03f2e0 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -7,7 +7,7 @@ #include "search.h" Move ParseMove(Board &board, const char* moveString) { - GenerateMoves(board, board.sideToMove); + GenerateMoves(board); // Source square std::string fromString = ""; -- 2.47.3 From 986b49b50379600a31e32a4eb1813749dffd8a8e Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 14 May 2024 23:14:27 +0200 Subject: [PATCH 2/7] made some optimizations in PopBit and other parts --- Makefile | 2 +- source/bitboards.cpp | 6 ++---- source/bitboards.h | 8 ++------ source/board.cpp | 16 +++++++++------- source/board.h | 5 +++-- source/main.cpp | 2 +- source/movegen.cpp | 12 ++++++------ source/perft.cpp | 18 ++++++++---------- 8 files changed, 32 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 29dab75..883e5c7 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS:.cpp=.o))) # Target executable Eleanor$(EXE_EXT): $(OBJS) - g++ -o $@ $^ + g++ -o $@ $^ # Rule to compile source files to object files $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/types.h | $(OBJ_DIR) g++ -c -o $@ $< diff --git a/source/bitboards.cpp b/source/bitboards.cpp index 10c7539..58f3314 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -36,9 +36,7 @@ void Bitboard::SetBit(int square) { } void Bitboard::PopBit(int square) { - if (Bitboard(m_board).IsSet(square)) { - m_board &= (~Bitboard::GetSquare(square)).GetBoard(); - } + m_board &= ~(1ULL << square); } int Bitboard::PopCount() const { @@ -115,4 +113,4 @@ Bitboard Bitboard::operator>>(int other) { Bitboard Bitboard::operator~() { return ~(m_board); -} \ No newline at end of file +} diff --git a/source/bitboards.h b/source/bitboards.h index 8f0038b..d767a28 100644 --- a/source/bitboards.h +++ b/source/bitboards.h @@ -11,7 +11,7 @@ public: Bitboard(const U64& board) : m_board(board) {} - + U64 GetBoard() const; bool IsSet(int square) const; void SetBit(int square); @@ -22,10 +22,6 @@ public: int getLS1BIndex() const; static Bitboard getOccupancy(int index, Bitboard attackMask); - static Bitboard GetSquare(int square) { - return (1ULL << square); - }; - Bitboard operator&(const Bitboard& other) const; Bitboard operator|(const Bitboard& other) const; Bitboard operator^(const Bitboard& other) const; @@ -38,4 +34,4 @@ public: Bitboard operator<<(int other); Bitboard operator>>(int other); Bitboard operator~(); -}; \ No newline at end of file +}; diff --git a/source/board.cpp b/source/board.cpp index 87a800b..c02edc4 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -1,5 +1,4 @@ #include "board.h" -#include "movegen.h" #include void Board::Init() { @@ -16,6 +15,8 @@ void Board::Reset() { pieces = std::array(); colors = std::array(); + + moveList = std::array(); } void Board::SetByFen(const char* fen) { @@ -79,7 +80,7 @@ void Board::SetByFen(const char* fen) { pieceColor = White; break; case '/': - currentSquare = currentSquare >> 8 + digits; + currentSquare = currentSquare >> (8 + digits); digits = 0; fen++; continue; @@ -236,18 +237,19 @@ void Board::PrintBoard() { } void Board::AddMove(Move move) { - moveList.push_back(move); + moveList[currentMoveIndex] = move; + currentMoveIndex++; } void Board::ResetMoves() { - moveList.clear(); + currentMoveIndex = 0; } void Board::ListMoves() { int count = 1; - for (Move move : moveList) { + for (int i = 0; i < currentMoveIndex; i++) { std::cout << count << ". "; - move.PrintMove(); + moveList[i].PrintMove(); count++; } } @@ -410,4 +412,4 @@ void Board::MakeMove(Move move) { if (halfMoves & 2 == 0) fullMoves++; sideToMove = !attackerColor; enPassantTarget = newEpTarget; -} \ No newline at end of file +} diff --git a/source/board.h b/source/board.h index 12af5c9..f324801 100644 --- a/source/board.h +++ b/source/board.h @@ -17,7 +17,8 @@ public: Bitboard occupied; - std::vector moveList; + std::array moveList; + int currentMoveIndex = 0; bool sideToMove = White; int halfMoves = 0; @@ -46,4 +47,4 @@ public: void Promote(int square, int pieceType, int color, bool isCapture); void MakeMove(Move move); -}; \ No newline at end of file +}; diff --git a/source/main.cpp b/source/main.cpp index 2ff7512..040e453 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -16,4 +16,4 @@ int main() { Perft(board, 10); return 0; -} \ No newline at end of file +} diff --git a/source/movegen.cpp b/source/movegen.cpp index 4758cae..1c2459c 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -18,7 +18,7 @@ static Bitboard notHGfile = notHfile & ~files[G]; Bitboard maskPawnAttacks(int side, int square) { Bitboard attacks; - Bitboard attacker = Bitboard::GetSquare(square); + Bitboard attacker = (1ULL << square); // White if (!side) { @@ -35,7 +35,7 @@ Bitboard maskPawnAttacks(int side, int square) { Bitboard maskKnightAttacks(int square) { Bitboard attacks; - Bitboard attacker = Bitboard::GetSquare(square); + Bitboard attacker = (1ULL << square); if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north); if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north); @@ -52,7 +52,7 @@ Bitboard maskKnightAttacks(int square) { Bitboard maskKingAttacks(int square) { Bitboard attacks; - Bitboard attacker = Bitboard::GetSquare(square); + Bitboard attacker = (1ULL << square); attacks |= (attacker << north); attacks |= (attacker << south); @@ -369,12 +369,12 @@ static void GenKingMoves(Board &board, bool color) { void GenerateMoves(Board &board) { bool side = board.sideToMove; - board.moveList.clear(); + board.ResetMoves(); GenPawnMoves(board, side); - GenKnightMoves(board, side); + GenKnightMoves(board, side); GenKingMoves(board, side); GenBishopMoves(board, side); GenRookMoves(board, side); GenQueenMoves(board, side); -} \ No newline at end of file +} diff --git a/source/perft.cpp b/source/perft.cpp index cd0ee55..3273ccc 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -4,16 +4,15 @@ U64 HelperPerft(Board board, int depth) { GenerateMoves(board); - if (depth == 1) return board.moveList.size(); + if (depth == 1) return board.currentMoveIndex; if (depth == 0) return 1ULL; U64 nodes = 0; - for (int i = 0; i < board.moveList.size(); i++) { + for (int i = 0; i < board.currentMoveIndex; i++) { Board copy = board; - board.MakeMove(board.moveList[i]); - nodes += HelperPerft(board, depth - 1); - board = copy; + copy.MakeMove(board.moveList[i]); + nodes += HelperPerft(copy, depth - 1); } return nodes; } @@ -24,11 +23,10 @@ void Perft(Board &board, int depth) { U64 totalNodes = 0; auto start = std::chrono::steady_clock::now(); - for (int i = 0; i < board.moveList.size(); i++) { + for (int i = 0; i < board.currentMoveIndex; i++) { Board copy = board; - board.MakeMove(board.moveList[i]); - U64 nodeCount = HelperPerft(board, depth - 1); - board = copy; + copy.MakeMove(board.moveList[i]); + U64 nodeCount = HelperPerft(copy, depth - 1); totalNodes += nodeCount; std::cout << squareCoords[board.moveList[i].MoveFrom()] << squareCoords[board.moveList[i].MoveTo()]; @@ -56,4 +54,4 @@ void Perft(Board &board, int depth) { std::cout << "Total nodes: " << totalNodes << '\n'; std::cout << "Time took: " << elapsed_seconds << "s ("; std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n"; -} \ No newline at end of file +} -- 2.47.3 From f6c2b6a881078466c7deb883dfe67aec88035550 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 15 May 2024 00:15:37 +0200 Subject: [PATCH 3/7] reimplemented the GetSquare function + enabled compiler optimization --- Makefile | 4 ++-- source/bitboards.cpp | 6 +++--- source/bitboards.h | 4 ++++ source/movegen.cpp | 6 +++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 883e5c7..ba88710 100644 --- a/Makefile +++ b/Makefile @@ -17,10 +17,10 @@ OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS:.cpp=.o))) # Target executable Eleanor$(EXE_EXT): $(OBJS) - g++ -o $@ $^ + g++ -o $@ $^ -O3 # Rule to compile source files to object files $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/types.h | $(OBJ_DIR) - g++ -c -o $@ $< + g++ -c -o $@ $< -O3 # Create the object directory if it doesn't exist $(OBJ_DIR): diff --git a/source/bitboards.cpp b/source/bitboards.cpp index 58f3314..c61d55a 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -28,15 +28,15 @@ U64 Bitboard::GetBoard() const { } bool Bitboard::IsSet(int square) const { - return (1ULL << square) & m_board; + return GetSquare(square).GetBoard() & m_board; } void Bitboard::SetBit(int square) { - m_board |= (1ULL << square); + m_board |= GetSquare(square).GetBoard(); } void Bitboard::PopBit(int square) { - m_board &= ~(1ULL << square); + m_board &= ~GetSquare(square).GetBoard(); } int Bitboard::PopCount() const { diff --git a/source/bitboards.h b/source/bitboards.h index d767a28..1e64ae5 100644 --- a/source/bitboards.h +++ b/source/bitboards.h @@ -22,6 +22,10 @@ public: int getLS1BIndex() const; static Bitboard getOccupancy(int index, Bitboard attackMask); + static Bitboard GetSquare(int square) { + return (1ULL << square); + }; + Bitboard operator&(const Bitboard& other) const; Bitboard operator|(const Bitboard& other) const; Bitboard operator^(const Bitboard& other) const; diff --git a/source/movegen.cpp b/source/movegen.cpp index 1c2459c..ff2cb8b 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -18,7 +18,7 @@ static Bitboard notHGfile = notHfile & ~files[G]; Bitboard maskPawnAttacks(int side, int square) { Bitboard attacks; - Bitboard attacker = (1ULL << square); + Bitboard attacker = Bitboard::GetSquare(square); // White if (!side) { @@ -35,7 +35,7 @@ Bitboard maskPawnAttacks(int side, int square) { Bitboard maskKnightAttacks(int square) { Bitboard attacks; - Bitboard attacker = (1ULL << square); + Bitboard attacker = Bitboard::GetSquare(square); if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north); if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north); @@ -52,7 +52,7 @@ Bitboard maskKnightAttacks(int square) { Bitboard maskKingAttacks(int square) { Bitboard attacks; - Bitboard attacker = (1ULL << square); + Bitboard attacker = Bitboard::GetSquare(square); attacks |= (attacker << north); attacks |= (attacker << south); -- 2.47.3 From a23918ab8dd0ee1769ab328c4b0cbb760891dc2a Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 15 May 2024 00:32:57 +0200 Subject: [PATCH 4/7] enabled compiler optimizations + sped up search a little bit --- Makefile | 4 ++-- source/main.cpp | 7 ++----- source/search.cpp | 9 +++++---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 0412a89..7a0eca2 100644 --- a/Makefile +++ b/Makefile @@ -17,11 +17,11 @@ OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS:.cpp=.o))) # Target executable Eleanor$(EXE_EXT): $(OBJS) - g++ -o $@ $^ + g++ -o $@ $^ -O3 # Rule to compile source files to object files $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/types.h | $(OBJ_DIR) - g++ -c -o $@ $< + g++ -c -o $@ $< -O3 # Create the object directory if it doesn't exist $(OBJ_DIR): diff --git a/source/main.cpp b/source/main.cpp index 996e312..4505175 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,8 +1,5 @@ -#include -#include "types.h" #include "board.h" #include "movegen.h" -#include "perft.h" #include "uci.h" int main() { @@ -11,7 +8,7 @@ int main() { Board board; - UCILoop(board); + UCILoop(board); return 0; -} \ No newline at end of file +} diff --git a/source/search.cpp b/source/search.cpp index d4569fc..d45339f 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -40,6 +40,11 @@ int Quiescence(Board board, int alpha, int beta) { } int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) { + if (depth == 0) { + return Quiescence(board, -50000, 50000); + //return Evaluate(board); + } + GenerateMoves(board, board.sideToMove); if (board.moveList.size() == 0) { @@ -50,10 +55,6 @@ int NegaMax(Board board, int depth, int alpha, int beta, bool isRoot) { } } - if (depth == 0) { - return Quiescence(board, -50000, 50000); - //return Evaluate(board); - } int max = -50000; -- 2.47.3 From 4d8a7f6a2e1256cd7574560d9f1dc6ff6c380d94 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 15 May 2024 00:54:46 +0200 Subject: [PATCH 5/7] added operator overloading to Bitboard class --- source/bitboards.cpp | 37 ++++++++++++++++++----- source/bitboards.h | 11 ++++++- source/board.cpp | 2 +- source/evaluate.cpp | 2 +- source/movegen.cpp | 70 ++++++++++++++++++++++---------------------- 5 files changed, 77 insertions(+), 45 deletions(-) diff --git a/source/bitboards.cpp b/source/bitboards.cpp index c61d55a..e3a24ec 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -23,20 +23,16 @@ void Bitboard::PrintBoard() const { std::cout << " Board: 0x" << std::hex << m_board << '\n'; } -U64 Bitboard::GetBoard() const { - return m_board; -} - bool Bitboard::IsSet(int square) const { - return GetSquare(square).GetBoard() & m_board; + return GetSquare(square) & m_board; } void Bitboard::SetBit(int square) { - m_board |= GetSquare(square).GetBoard(); + m_board |= GetSquare(square); } void Bitboard::PopBit(int square) { - m_board &= ~GetSquare(square).GetBoard(); + m_board &= ~GetSquare(square); } int Bitboard::PopCount() const { @@ -84,6 +80,19 @@ Bitboard Bitboard::operator*(const Bitboard& other) const { return m_board * other.m_board; } +Bitboard Bitboard::operator&(const uint64_t& other) const { + return m_board & other; +} +Bitboard Bitboard::operator|(const uint64_t& other) const { + return m_board | other; +} +Bitboard Bitboard::operator^(const uint64_t& other) const { + return m_board ^ other; +} +Bitboard Bitboard::operator*(const uint64_t& other) const { + return m_board * other; +} + Bitboard Bitboard::operator&=(const Bitboard& other) { return m_board &= other.m_board; } @@ -94,6 +103,16 @@ Bitboard Bitboard::operator^=(const Bitboard& other) { return m_board ^= other.m_board; } +Bitboard Bitboard::operator&=(const uint64_t& other) { + return m_board &= other; +} +Bitboard Bitboard::operator|=(const uint64_t& other) { + return m_board |= other; +} +Bitboard Bitboard::operator^=(const uint64_t& other) { + return m_board ^= other; +} + Bitboard Bitboard::operator<<(int other) { if (other > 0) { return m_board << other; @@ -114,3 +133,7 @@ Bitboard Bitboard::operator>>(int other) { Bitboard Bitboard::operator~() { return ~(m_board); } + +Bitboard::operator uint64_t() { + return m_board; +} diff --git a/source/bitboards.h b/source/bitboards.h index 1e64ae5..718aa00 100644 --- a/source/bitboards.h +++ b/source/bitboards.h @@ -12,7 +12,6 @@ public: Bitboard(const U64& board) : m_board(board) {} - U64 GetBoard() const; bool IsSet(int square) const; void SetBit(int square); void PopBit(int square); @@ -30,12 +29,22 @@ public: Bitboard operator|(const Bitboard& other) const; Bitboard operator^(const Bitboard& other) const; Bitboard operator*(const Bitboard& other) const; + + Bitboard operator&(const uint64_t& other) const; + Bitboard operator|(const uint64_t& other) const; + Bitboard operator^(const uint64_t& other) const; + Bitboard operator*(const uint64_t& other) const; Bitboard operator&=(const Bitboard& other); Bitboard operator|=(const Bitboard& other); Bitboard operator^=(const Bitboard& other); + Bitboard operator&=(const uint64_t& other); + Bitboard operator|=(const uint64_t& other); + Bitboard operator^=(const uint64_t& other); + Bitboard operator<<(int other); Bitboard operator>>(int other); Bitboard operator~(); + operator uint64_t(); }; diff --git a/source/board.cpp b/source/board.cpp index c02edc4..4d74aa4 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -277,7 +277,7 @@ U64 Board::GetAttackMaps(bool side) { bool Board::InCheck(bool side) { Bitboard myKingSquare = colors[side] & pieces[King]; - return GetAttackMaps(!side) & myKingSquare.GetBoard(); + return GetAttackMaps(!side) & myKingSquare; } void Board::SetPiece(int piece, int square, bool color) { diff --git a/source/evaluate.cpp b/source/evaluate.cpp index a2f63e0..a939849 100644 --- a/source/evaluate.cpp +++ b/source/evaluate.cpp @@ -13,7 +13,7 @@ static int GetPositionalScore(Board &board, int type, int color) { Bitboard pieces = board.pieces[type] & board.colors[color]; - while (pieces.GetBoard()) { + while (pieces) { int square = pieces.getLS1BIndex(); // White diff --git a/source/movegen.cpp b/source/movegen.cpp index ff2cb8b..ed29c9c 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -22,13 +22,13 @@ Bitboard maskPawnAttacks(int side, int square) { // White if (!side) { - if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa); - if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe); + if (((attacker << noEa) & notAfile)) attacks |= (attacker << noEa); + if (((attacker << noWe) & notHfile)) attacks |= (attacker << noWe); } // Black else { - if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa); - if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe); + if (((attacker << soEa) & notAfile)) attacks |= (attacker << soEa); + if (((attacker << soWe) & notHfile)) attacks |= (attacker << soWe); } return attacks; } @@ -37,15 +37,15 @@ Bitboard maskKnightAttacks(int square) { Bitboard attacks; Bitboard attacker = Bitboard::GetSquare(square); - if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north); - if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north); - if (((attacker << noWe + west) & notHGfile).GetBoard()) attacks |= (attacker << noWe + west); - if (((attacker << noEa + east) & notABfile).GetBoard()) attacks |= (attacker << noEa + east); + if (((attacker << noEa + north) & notAfile)) attacks |= (attacker << noEa + north); + if (((attacker << noWe + north) & notHfile)) attacks |= (attacker << noWe + north); + if (((attacker << noWe + west) & notHGfile)) attacks |= (attacker << noWe + west); + if (((attacker << noEa + east) & notABfile)) attacks |= (attacker << noEa + east); - if (((attacker << soWe + south) & notHfile).GetBoard()) attacks |= (attacker << soWe + south); - if (((attacker << soEa + south) & notAfile).GetBoard()) attacks |= (attacker << soEa + south); - if (((attacker << soEa + east) & notABfile).GetBoard()) attacks |= (attacker << soEa + east); - if (((attacker << soWe + west) & notHGfile).GetBoard()) attacks |= (attacker << soWe + west); + if (((attacker << soWe + south) & notHfile)) attacks |= (attacker << soWe + south); + if (((attacker << soEa + south) & notAfile)) attacks |= (attacker << soEa + south); + if (((attacker << soEa + east) & notABfile)) attacks |= (attacker << soEa + east); + if (((attacker << soWe + west) & notHGfile)) attacks |= (attacker << soWe + west); return attacks; } @@ -57,12 +57,12 @@ Bitboard maskKingAttacks(int square) { attacks |= (attacker << north); attacks |= (attacker << south); - if (((attacker << west) & notHfile).GetBoard()) attacks |= (attacker << west); - if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe); - if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa); - if (((attacker << east) & notAfile).GetBoard()) attacks |= (attacker << east); - if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa); - if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe); + if (((attacker << west) & notHfile)) attacks |= (attacker << west); + if (((attacker << soWe) & notHfile)) attacks |= (attacker << soWe); + if (((attacker << soEa) & notAfile)) attacks |= (attacker << soEa); + if (((attacker << east) & notAfile)) attacks |= (attacker << east); + if (((attacker << noEa) & notAfile)) attacks |= (attacker << noEa); + if (((attacker << noWe) & notHfile)) attacks |= (attacker << noWe); return attacks; } @@ -140,25 +140,25 @@ Bitboard bishopAttacksOnTheFly(int square, Bitboard block) { for (int r = tr + 1, f = tf - 1; r <= 7 && f >= 0; r++, f--) { currentSquare = (1ULL << (r * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } // North East for (int r = tr + 1, f = tf + 1; r <= 7 && f <= 7; r++, f++) { currentSquare = (1ULL << (r * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } //South West for (int r = tr - 1, f = tf - 1; r >= 0 && f >= 0; r--, f--) { currentSquare = (1ULL << (r * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } //South East for (int r = tr - 1, f = tf + 1; r >= 0 && f <= 7; r--, f++) { currentSquare = (1ULL << (r * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } return attacks; @@ -179,25 +179,25 @@ Bitboard rookAttacksOnTheFly(int square, Bitboard block) { for (int r = tr + 1; r <= 7; r++) { currentSquare = (1ULL << (r * 8 + tf)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } // South for (int r = tr - 1; r >= 0; r--) { currentSquare = (1ULL << (r * 8 + tf)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } // East for (int f = tf + 1; f <= 7; f++) { currentSquare = (1ULL << (tr * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } // West for (int f = tf - 1; f >= 0; f--) { currentSquare = (1ULL << (tr * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } return attacks; @@ -221,7 +221,7 @@ void initSliderAttacks() { for (int index = 0; index < 512; index++) { Bitboard occupancy = Bitboard::getOccupancy(index, bishopMasks[square]); - int magicIndex = (occupancy.GetBoard() * bishopMagicNumbers[square]) + int magicIndex = (occupancy * bishopMagicNumbers[square]) >> (64 - bishopRelevantBits[square]); bishopAttacks[square][magicIndex] = bishopAttacksOnTheFly(square, occupancy); @@ -230,7 +230,7 @@ void initSliderAttacks() { for (int index = 0; index < 4096; index++) { Bitboard occupancy = Bitboard::getOccupancy(index, rookMasks[square]); - int magicIndex = (occupancy.GetBoard() * rookMagicNumbers[square]) + int magicIndex = (occupancy * rookMagicNumbers[square]) >> (64 - rookRelevantBits[square]); rookAttacks[square][magicIndex] = rookAttacksOnTheFly(square, occupancy); @@ -239,7 +239,7 @@ void initSliderAttacks() { } Bitboard getBishopAttack(int square, U64 occupancy) { - occupancy &= bishopMasks[square].GetBoard(); + occupancy &= bishopMasks[square]; occupancy *= bishopMagicNumbers[square]; occupancy >>= (64 - bishopRelevantBits[square]); @@ -247,7 +247,7 @@ Bitboard getBishopAttack(int square, U64 occupancy) { } Bitboard getRookAttack(int square, U64 occupancy) { - occupancy &= rookMasks[square].GetBoard(); + occupancy &= rookMasks[square]; occupancy *= rookMagicNumbers[square]; occupancy >>= (64 - rookRelevantBits[square]); @@ -258,11 +258,11 @@ Bitboard getQueenAttack(int square, U64 occupancy) { U64 bishopOccupancy = occupancy; U64 rookOccupancy = occupancy; - bishopOccupancy &= bishopMasks[square].GetBoard(); + bishopOccupancy &= bishopMasks[square]; bishopOccupancy *= bishopMagicNumbers[square]; bishopOccupancy >>= (64 - bishopRelevantBits[square]); - rookOccupancy &= rookMasks[square].GetBoard(); + rookOccupancy &= rookMasks[square]; rookOccupancy *= rookMagicNumbers[square]; rookOccupancy >>= (64 - rookRelevantBits[square]); @@ -324,14 +324,14 @@ static void GenPawnMoves(Board &board, bool color) { static void GenKnightMoves(Board &board, bool color) { Bitboard knights = board.colors[color] & board.pieces[Knight]; - while (knights.GetBoard()) { + while (knights) { int square = knights.getLS1BIndex(); Bitboard moves = knightAttacks[square] & ~board.colors[color]; Bitboard captures = moves & board.colors[!color]; Bitboard quietMoves = moves & ~board.colors[!color]; - while (captures.GetBoard()) { + while (captures) { int targetSquare = captures.getLS1BIndex(); board.AddMove(Move(square, targetSquare, capture)); @@ -339,7 +339,7 @@ static void GenKnightMoves(Board &board, bool color) { captures.PopBit(targetSquare); } - while (quietMoves.GetBoard()) { + while (quietMoves) { int targetSquare = quietMoves.getLS1BIndex(); board.AddMove(Move(square, targetSquare, quiet)); -- 2.47.3 From d3a26b29dffa6a4c6f60bac6a28bde6bd7c5a315 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 15 May 2024 14:44:29 +0200 Subject: [PATCH 6/7] now generating all other piece moves --- source/main.cpp | 2 +- source/movegen.cpp | 221 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 192 insertions(+), 31 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 040e453..1f98727 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -13,7 +13,7 @@ int main() { // UCILoop(board); - Perft(board, 10); + Perft(board, 6); return 0; } diff --git a/source/movegen.cpp b/source/movegen.cpp index ed29c9c..0268958 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -285,40 +285,85 @@ Bitboard getPieceAttacks(int square, int piece, int color, U64 occupancy) { } } -// Returns the direction to target from square -static int GetDirection(int square, int target) { - int rankDiff = (target / 8) - (square / 8); - int fileDiff = (target % 8) - (square % 8); +static Bitboard getPawnPushes(int square, bool color, Bitboard &occupancy) { + Bitboard pushes; - // Main directions - if (rankDiff == 0 && fileDiff < 0) { - return west; - } else if (rankDiff == 0 && fileDiff > 0) { - return east; - } else if (rankDiff < 0 && fileDiff == 0) { - return south; - } else if (rankDiff > 0 && fileDiff == 0) { - return north; - } + int direction = color ? south : north; + Bitboard secondRank = color ? ranks[r_7] : ranks[r_2]; - // Diagonals - if (abs(rankDiff) == abs(fileDiff)) { - if (rankDiff < 0 && fileDiff < 0) { - return soWe; - } else if (rankDiff < 0 && fileDiff > 0) { - return soEa; - } else if (rankDiff > 0 && fileDiff < 0) { - return noWe; - } else if (rankDiff > 0 && fileDiff > 0) { - return noEa; - } - } + if (!occupancy.IsSet(square + direction)) { + pushes.SetBit(square + direction); - return 0; -} + if (secondRank.IsSet(square) && !occupancy.IsSet(square + 2 * direction)) { + pushes.SetBit(square + 2 * direction); + } + } + + return pushes; +} static void GenPawnMoves(Board &board, bool color) { + Bitboard pawns = board.pieces[Pawn] & board.colors[color]; + while (pawns) { + int square = pawns.getLS1BIndex(); + Bitboard seventhRank = color ? ranks[r_2] : ranks[r_7]; + + Bitboard pushes = getPawnPushes(square, color, board.occupied); + Bitboard captures = pawnAttacks[color][square] & board.colors[!color]; + + if (seventhRank.IsSet(square)) { + while (pushes) { + int targetSquare = pushes.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, queenPromotion)); + board.AddMove(Move(square, targetSquare, rookPromotion)); + board.AddMove(Move(square, targetSquare, bishopPromotion)); + board.AddMove(Move(square, targetSquare, knightPromotion)); + + pushes.PopBit(targetSquare); + } + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, queenPromoCapture)); + board.AddMove(Move(square, targetSquare, rookPromoCapture)); + board.AddMove(Move(square, targetSquare, bishopPromoCapture)); + board.AddMove(Move(square, targetSquare, knightPromoCapture)); + + captures.PopBit(targetSquare); + } + } else { + while (pushes) { + int targetSquare = pushes.getLS1BIndex(); + + // Checking for double push + if (abs(targetSquare - square) == 16) { + board.AddMove(Move(square, targetSquare, doublePawnPush)); + } else { + board.AddMove(Move(square, targetSquare, quiet)); + } + + pushes.PopBit(targetSquare); + } + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + // Checking for en passant + if (pawnAttacks[color][square].IsSet(board.enPassantTarget)) { + board.AddMove(Move(square, board.enPassantTarget, epCapture)); + } + } + + pawns.PopBit(square); + } } static void GenKnightMoves(Board &board, bool color) { @@ -348,23 +393,139 @@ static void GenKnightMoves(Board &board, bool color) { } knights.PopBit(square); - } + } } static void GenRookMoves(Board &board, bool color) { + Bitboard rooks = board.pieces[Rook] & board.colors[color]; + while (rooks) { + int square = rooks.getLS1BIndex(); + + Bitboard moves = getRookAttack(square, board.occupied) & ~board.colors[color]; + Bitboard captures = moves & board.colors[!color]; + Bitboard quietMoves = moves & ~board.colors[!color]; + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + while (quietMoves) { + int targetSquare = quietMoves.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, quiet)); + + quietMoves.PopBit(targetSquare); + } + + rooks.PopBit(square); + } } static void GenBishopMoves(Board &board, bool color) { + Bitboard bishops = board.pieces[Bishop] & board.colors[color]; + while (bishops) { + int square = bishops.getLS1BIndex(); + + Bitboard moves = getBishopAttack(square, board.occupied) & ~board.colors[color]; + Bitboard captures = moves & board.colors[!color]; + Bitboard quietMoves = moves & ~board.colors[!color]; + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + while (quietMoves) { + int targetSquare = quietMoves.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, quiet)); + + quietMoves.PopBit(targetSquare); + } + + bishops.PopBit(square); + } } static void GenQueenMoves(Board &board, bool color) { + Bitboard queens = board.pieces[Queen] & board.colors[color]; + while (queens) { + int square = queens.getLS1BIndex(); + + Bitboard moves = getQueenAttack(square, board.occupied) & ~board.colors[color]; + Bitboard captures = moves & board.colors[!color]; + Bitboard quietMoves = moves & ~board.colors[!color]; + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + while (quietMoves) { + int targetSquare = quietMoves.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, quiet)); + + quietMoves.PopBit(targetSquare); + } + + queens.PopBit(square); + } } static void GenKingMoves(Board &board, bool color) { - + int kingSquare = (board.pieces[King] & board.colors[color]).getLS1BIndex(); + + Bitboard moves = kingAttacks[kingSquare] & ~board.colors[color]; + Bitboard captures = moves & board.colors[!color]; + Bitboard quietMoves = moves & ~board.colors[!color]; + + while (captures) { + int targetSquare = captures.getLS1BIndex(); + + board.AddMove(Move(kingSquare, targetSquare, capture)); + + captures.PopBit(targetSquare); + } + + while (quietMoves) { + int targetSquare = quietMoves.getLS1BIndex(); + + board.AddMove(Move(kingSquare, targetSquare, quiet)); + + quietMoves.PopBit(targetSquare); + } + + // Checking for king side castle + if (board.castlingRights[color * 2]) { + Bitboard mask = color ? 0xe000000000000000 : 0xe0; + + if ((board.occupied & mask).PopCount() == 1) { + board.AddMove(Move(kingSquare, kingSquare + 2, kingCastle)); + } + } + + // Checking for quuen side castle + if (board.castlingRights[color * 2 + 1]) { + Bitboard mask = color ? 0xf00000000000000 : 0xf; + + if ((board.occupied & mask).PopCount() == 1) { + board.AddMove(Move(kingSquare, kingSquare - 2, queenCastle)); + } + } } void GenerateMoves(Board &board) { -- 2.47.3 From 4c217fa4781850ad049e8147374c40c06266357c Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 16 May 2024 01:35:16 +0200 Subject: [PATCH 7/7] storing castling rights in 8 bits instead of 32 --- source/board.cpp | 38 ++++++++++++++------------------------ source/board.h | 5 +---- source/movegen.cpp | 6 +++--- source/types.h | 9 ++++++++- 4 files changed, 26 insertions(+), 32 deletions(-) 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 +}; -- 2.47.3