From 025ae1eceb2ddfc3f22f06c8f943e28c2f62ad17 Mon Sep 17 00:00:00 2001 From: "rektdie@gmail.com" Date: Sun, 31 Mar 2024 20:39:40 +0200 Subject: [PATCH] added promotion moves(with captures as well) --- source/board.cpp | 38 +++++++++++++++++++++++++++++++--- source/board.h | 1 + source/movegen.cpp | 51 +++++++++++++++++++++++++++++++--------------- 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 26ba5e9..5aa2f82 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -282,6 +282,20 @@ bool Board::InCheck(bool side) { return false; } +void Board::Promote(int square, int pieceType, int color, bool isCapture) { + if (isCapture) { + int targetType = GetPieceType(square); + std::cout << targetType << '\n'; + + pieces[targetType].PopBit(square); + colors[!color].PopBit(square); + } + + occupied.SetBit(square); + pieces[pieceType].SetBit(square); + colors[color].SetBit(square); +} + void Board::DoMove(Move move) { enPassantTarget = a1; // resetting (a1 is impossible) uint16_t moveType = move.moveFlags; @@ -292,13 +306,15 @@ void Board::DoMove(Move move) { int direction = attackerColor ? south : north; - pieces[attackerType].PopBit(move.moveFrom); colors[attackerColor].PopBit(move.moveFrom); occupied.PopBit(move.moveFrom); - pieces[attackerType].SetBit(move.moveTo); - colors[attackerColor].SetBit(move.moveTo); + if (moveType < knightPromotion) { + pieces[attackerType].SetBit(move.moveTo); + colors[attackerColor].SetBit(move.moveTo); + } + if (moveType == quiet) { occupied.SetBit(move.moveTo); } else if (moveType == doublePawnPush) { @@ -332,6 +348,22 @@ void Board::DoMove(Move move) { pieces[Rook].SetBit(rookSquare - 2); colors[attackerColor].SetBit(rookSquare - 2); occupied.SetBit(rookSquare); + } else if (moveType == knightPromotion) { + Promote(move.moveTo, Knight, attackerColor, false); + } else if (moveType == bishopPromotion) { + Promote(move.moveTo, Bishop, attackerColor, false); + } else if (moveType == rookPromotion) { + Promote(move.moveTo, Rook, attackerColor, false); + } else if (moveType == queenPromotion) { + Promote(move.moveTo, Queen, attackerColor, false); + } else if (moveType == knightPromoCapture) { + Promote(move.moveTo, Knight, attackerColor, true); + } else if (moveType == bishopPromoCapture) { + Promote(move.moveTo, Bishop, attackerColor, true); + } else if (moveType == rookPromoCapture) { + Promote(move.moveTo, Rook, attackerColor, true); + } else if (moveType == queenPromoCapture) { + Promote(move.moveTo, Queen, attackerColor, true); } // Removing the right to castle on king movement diff --git a/source/board.h b/source/board.h index 3087427..f512d65 100644 --- a/source/board.h +++ b/source/board.h @@ -40,5 +40,6 @@ public: bool InCheck(bool side); + void Promote(int square, int pieceType, int color, bool isCapture); void DoMove(Move move); }; \ No newline at end of file diff --git a/source/movegen.cpp b/source/movegen.cpp index 26d81b6..57d343a 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -275,6 +275,7 @@ static void GenPawnMoves(Board &board, bool color) { while (pawns.GetBoard()) { int square = pawns.getLS1BIndex(); int direction = color ? south : north; + int lastRank = color ? r_1 : r_8; Bitboard attacks = pawnAttacks[color][square]; @@ -292,28 +293,46 @@ static void GenPawnMoves(Board &board, bool color) { Bitboard firstAhead = Bitboard::GetSquare(square + direction) & ~board.occupied; Bitboard secondAhead = Bitboard::GetSquare(square + 2 * direction) & ~board.occupied; - while (attacks.GetBoard()) { - int targetSquare = attacks.getLS1BIndex(); - - board.AddMove(Move(square, targetSquare, capture)); + if ((attacks & ranks[lastRank]).GetBoard()) { + while (attacks.GetBoard()) { + int targetSquare = attacks.getLS1BIndex(); + + for (int type = knightPromoCapture; type <= queenPromoCapture; type++) { + board.AddMove(Move(square, targetSquare, type)); + } - attacks.PopBit(targetSquare); + attacks.PopBit(targetSquare); + } + } else { + while (attacks.GetBoard()) { + int targetSquare = attacks.getLS1BIndex(); + + board.AddMove(Move(square, targetSquare, capture)); + + attacks.PopBit(targetSquare); + } } if (firstAhead.GetBoard()) { - board.AddMove(Move(square, firstAhead.getLS1BIndex(), quiet)); - - if (color == White) { - if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_2).GetBoard()) { - if ((secondAhead).GetBoard()) { - board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush)); - } + if ((firstAhead & ranks[lastRank]).GetBoard()) { + for (int type = knightPromotion; type <= queenPromotion; type++) { + board.AddMove(Move(square, firstAhead.getLS1BIndex(), type)); } } else { - if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_7).GetBoard()) { - if ((secondAhead).GetBoard()) { - board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush)); - } + board.AddMove(Move(square, firstAhead.getLS1BIndex(), quiet)); + } + } + + if (color == White) { + if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_2).GetBoard()) { + if ((secondAhead).GetBoard()) { + board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush)); + } + } + } else { + if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_7).GetBoard()) { + if ((secondAhead).GetBoard()) { + board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush)); } } }