fixed InCheck and its helper functions

This commit is contained in:
rektdie committed 2024-04-10 09:21:44 +02:00
1 parent 494cd13f7e
commit b4dc7125f9
2 files changed
+40 -38

No files matched your search

+2 -1
View File
@@ -9,7 +9,8 @@ int main() {
initSliderAttacks(); initSliderAttacks();
Board board; Board board;
Perft(board, 4); board.PrintBoard();
Perft(board, 3);
return 0; return 0;
} }
+38 -37
View File
@@ -318,17 +318,17 @@ static Bitboard Checkers(Board &board, bool color) {
static int GetDirection(int square, int target) { static int GetDirection(int square, int target) {
int difference = target - square; int difference = target - square;
if (difference - north == 0) { if (difference % north == 0 && difference > 0) {
return north; return north;
} else if (difference - south == 0) { } else if (difference % south == 0 && difference < 0) {
return south; return south;
} else if (difference - noWe == 0) { } else if (difference % noWe == 0 && difference > 0) {
return noWe; return noWe;
} else if (difference - noEa == 0) { } else if (difference % noEa == 0 && difference > 0) {
return noEa; return noEa;
} else if (difference - soWe == 0) { } else if (difference % soWe == 0 && difference < 0) {
return soWe; return soWe;
} else if (difference - soEa == 0) { } else if (difference % soEa == 0 && difference < 0) {
return soEa; return soEa;
} else if (ranks[square / 8] == ranks[target / 8] && target < square) { } else if (ranks[square / 8] == ranks[target / 8] && target < square) {
return west; return west;
@@ -340,60 +340,66 @@ static int GetDirection(int square, int target) {
} }
// Returns a bitboard of the squares from attacker to target // Returns a bitboard of the squares from attacker to target
static Bitboard SliderRaysToSquare(int attacker, int target) { static Bitboard SliderRayToSquare(int attacker, int target) {
Bitboard ray; Bitboard ray;
int direction = GetDirection(attacker, target); int direction = GetDirection(attacker, target);
attacker += direction;
if (direction) { if (direction) {
while (attacker != target) { while (attacker != target) {
ray.SetBit(attacker); ray.SetBit(attacker);
attacker += direction; attacker += direction;
} }
ray.SetBit(target);
} }
return ray; return ray;
} }
static Bitboard GetPinningRay(Board &board, int square, int direction) { static Bitboard GetPinningRay(Board &board, int square, int direction) {
Bitboard ray; Bitboard ray;
int previousRank = square / 8;
if (direction) { square += direction;
int previousRank = (square - direction) / 7;
while (square < 64 && square >= 0) { while (square < 64 && square >= 0) {
int currentRank = square / 7; int currentRank = square / 8;
if (abs(currentRank - previousRank) > 1) break; if (abs(currentRank - previousRank) > 1) break;
previousRank = currentRank; previousRank = currentRank;
ray.SetBit(square); ray.SetBit(square);
if (board.occupied.IsSet(square)) { if (board.occupied.IsSet(square)) {
int attackerColor = board.GetPieceColor(square);
int attackerType = board.GetPieceType(square); int attackerType = board.GetPieceType(square);
int attackerColor = board.GetPieceColor(square);
if (attackerColor == !board.sideToMove && (attackerType == Rook if (attackerColor != board.sideToMove) {
|| attackerType == Bishop || attackerType == Queen)) { if (attackerType == Rook|| attackerType == Bishop
|| attackerType == Queen) {
return ray; return ray;
} }
return 0ULL; }
break;
} }
square += direction; square += direction;
} }
}
return 0ULL; return 0ULL;
} }
static bool IsPinned(Board &board, int square, int color) { static bool IsPinned(Board &board, int square) {
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); int kingSquare = (board.colors[board.sideToMove] & board.pieces[King]).getLS1BIndex();
int directionToKing = GetDirection(square, kingSquare); int directionToKing = GetDirection(square, kingSquare);
if ((SliderRaysToSquare(square, kingSquare) & board.occupied).GetBoard()) { if (directionToKing) {
// Checking if there are any pieces between square and kingSquare
if ((SliderRayToSquare(square, kingSquare)
& board.occupied).PopCount() != 2) {
return false; return false;
} }
int currentSquare = square - directionToKing; return GetPinningRay(board, square, -directionToKing).PopCount();
return GetPinningRay(board, currentSquare, -directionToKing).GetBoard(); }
return false;
} }
static bool IsLegalEnPassant(Board &board, int square) { static bool IsLegalEnPassant(Board &board, int square) {
@@ -452,7 +458,7 @@ static void GenPawnMoves(Board &board, bool color) {
Bitboard pushes = getPawnPushes(square, color, board.occupied); Bitboard pushes = getPawnPushes(square, color, board.occupied);
Bitboard captures = pawnAttacks[color][square]; Bitboard captures = pawnAttacks[color][square];
if (IsPinned(board, square, color)) { if (IsPinned(board, square)) {
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
int directionToKing = GetDirection(square, kingSquare); int directionToKing = GetDirection(square, kingSquare);
@@ -546,7 +552,7 @@ static void GenKnightMoves(Board &board, bool color) {
while (knights.GetBoard()) { while (knights.GetBoard()) {
int square = knights.getLS1BIndex(); int square = knights.getLS1BIndex();
if (IsPinned(board, square, color)) { if (IsPinned(board, square)) {
knights.PopBit(square); knights.PopBit(square);
continue; continue;
} }
@@ -585,7 +591,7 @@ static void GenRookMoves(Board &board, bool color) {
Bitboard captures = moves & board.colors[!color]; Bitboard captures = moves & board.colors[!color];
moves &= ~captures; moves &= ~captures;
if (IsPinned(board, square, color)) { if (IsPinned(board, square)) {
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
int directionToKing = GetDirection(square, kingSquare); int directionToKing = GetDirection(square, kingSquare);
@@ -645,7 +651,7 @@ static void GenBishopMoves(Board &board, bool color) {
Bitboard captures = moves & board.colors[!color]; Bitboard captures = moves & board.colors[!color];
moves &= ~captures; moves &= ~captures;
if (IsPinned(board, square, color)) { if (IsPinned(board, square)) {
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
int directionToKing = GetDirection(square, kingSquare); int directionToKing = GetDirection(square, kingSquare);
@@ -705,7 +711,7 @@ static void GenQueenMoves(Board &board, bool color) {
Bitboard captures = moves & board.colors[!color]; Bitboard captures = moves & board.colors[!color];
moves &= ~captures; moves &= ~captures;
if (IsPinned(board, square, color)) { if (IsPinned(board, square)) {
int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex(); int kingSquare = (board.colors[color] & board.pieces[King]).getLS1BIndex();
int directionToKing = GetDirection(square, kingSquare); int directionToKing = GetDirection(square, kingSquare);
@@ -817,7 +823,7 @@ static void GenCheckEvasions(Board &board, bool color) {
int checkerType = board.GetPieceType(checkerSquare); int checkerType = board.GetPieceType(checkerSquare);
if (checkerType == Rook || checkerType == Bishop || checkerType == Queen) { if (checkerType == Rook || checkerType == Bishop || checkerType == Queen) {
pushMask = SliderRaysToSquare(checkerSquare, kingSquare); pushMask = SliderRayToSquare(checkerSquare, kingSquare);
} }
// looping through own pieces to add captures and blocks // looping through own pieces to add captures and blocks
@@ -827,7 +833,7 @@ static void GenCheckEvasions(Board &board, bool color) {
while (currentPiece.GetBoard()) { while (currentPiece.GetBoard()) {
int square = currentPiece.getLS1BIndex(); int square = currentPiece.getLS1BIndex();
if (IsPinned(board, square, color)) { if (IsPinned(board, square)) {
currentPiece.PopBit(square); currentPiece.PopBit(square);
continue; continue;
} }
@@ -911,14 +917,9 @@ void GenerateMoves(Board &board, bool side) {
} }
GenPawnMoves(board, side); GenPawnMoves(board, side);
GenKnightMoves(board, side); GenKnightMoves(board, side);
GenRookMoves(board, side);
GenBishopMoves(board, side);
GenQueenMoves(board, side);
GenKingMoves(board, side, false); GenKingMoves(board, side, false);
GenBishopMoves(board, side);
GenRookMoves(board, side);
GenQueenMoves(board, side);
} }