added promotion moves(with captures as well)

This commit is contained in:
rektdie committed 2024-03-31 20:39:40 +02:00
1 parent 6b5127424e
commit 025ae1eceb
3 files changed
+54 -2

No files matched your search

+33 -1
View File
@@ -282,6 +282,20 @@ bool Board::InCheck(bool side) {
return false; 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) { void Board::DoMove(Move move) {
enPassantTarget = a1; // resetting (a1 is impossible) enPassantTarget = a1; // resetting (a1 is impossible)
uint16_t moveType = move.moveFlags; uint16_t moveType = move.moveFlags;
@@ -292,13 +306,15 @@ void Board::DoMove(Move move) {
int direction = attackerColor ? south : north; int direction = attackerColor ? south : north;
pieces[attackerType].PopBit(move.moveFrom); pieces[attackerType].PopBit(move.moveFrom);
colors[attackerColor].PopBit(move.moveFrom); colors[attackerColor].PopBit(move.moveFrom);
occupied.PopBit(move.moveFrom); occupied.PopBit(move.moveFrom);
if (moveType < knightPromotion) {
pieces[attackerType].SetBit(move.moveTo); pieces[attackerType].SetBit(move.moveTo);
colors[attackerColor].SetBit(move.moveTo); colors[attackerColor].SetBit(move.moveTo);
}
if (moveType == quiet) { if (moveType == quiet) {
occupied.SetBit(move.moveTo); occupied.SetBit(move.moveTo);
} else if (moveType == doublePawnPush) { } else if (moveType == doublePawnPush) {
@@ -332,6 +348,22 @@ void Board::DoMove(Move move) {
pieces[Rook].SetBit(rookSquare - 2); pieces[Rook].SetBit(rookSquare - 2);
colors[attackerColor].SetBit(rookSquare - 2); colors[attackerColor].SetBit(rookSquare - 2);
occupied.SetBit(rookSquare); 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 // Removing the right to castle on king movement
+1
View File
@@ -40,5 +40,6 @@ public:
bool InCheck(bool side); bool InCheck(bool side);
void Promote(int square, int pieceType, int color, bool isCapture);
void DoMove(Move move); void DoMove(Move move);
}; };
+20 -1
View File
@@ -275,6 +275,7 @@ static void GenPawnMoves(Board &board, bool color) {
while (pawns.GetBoard()) { while (pawns.GetBoard()) {
int square = pawns.getLS1BIndex(); int square = pawns.getLS1BIndex();
int direction = color ? south : north; int direction = color ? south : north;
int lastRank = color ? r_1 : r_8;
Bitboard attacks = pawnAttacks[color][square]; Bitboard attacks = pawnAttacks[color][square];
@@ -292,6 +293,17 @@ static void GenPawnMoves(Board &board, bool color) {
Bitboard firstAhead = Bitboard::GetSquare(square + direction) & ~board.occupied; Bitboard firstAhead = Bitboard::GetSquare(square + direction) & ~board.occupied;
Bitboard secondAhead = Bitboard::GetSquare(square + 2 * direction) & ~board.occupied; Bitboard secondAhead = Bitboard::GetSquare(square + 2 * direction) & ~board.occupied;
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);
}
} else {
while (attacks.GetBoard()) { while (attacks.GetBoard()) {
int targetSquare = attacks.getLS1BIndex(); int targetSquare = attacks.getLS1BIndex();
@@ -299,9 +311,17 @@ static void GenPawnMoves(Board &board, bool color) {
attacks.PopBit(targetSquare); attacks.PopBit(targetSquare);
} }
}
if (firstAhead.GetBoard()) { if (firstAhead.GetBoard()) {
if ((firstAhead & ranks[lastRank]).GetBoard()) {
for (int type = knightPromotion; type <= queenPromotion; type++) {
board.AddMove(Move(square, firstAhead.getLS1BIndex(), type));
}
} else {
board.AddMove(Move(square, firstAhead.getLS1BIndex(), quiet)); board.AddMove(Move(square, firstAhead.getLS1BIndex(), quiet));
}
}
if (color == White) { if (color == White) {
if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_2).GetBoard()) { if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_2).GetBoard()) {
@@ -316,7 +336,6 @@ static void GenPawnMoves(Board &board, bool color) {
} }
} }
} }
}
pawns.PopBit(square); pawns.PopBit(square);
} }