From a1fdbee6ba1d4da1543d7182587be6ee9bfc2dd1 Mon Sep 17 00:00:00 2001 From: Inconspicuous Semicolon Date: Fri, 28 Jul 2023 23:35:30 +0100 Subject: [PATCH] Create Function GetPieceAttacks This function allows the length of code to be reduced, by allowing all piece attacks to be queried in the same loop without having to check which function needs to be checked. For example a loop to check all piece attacks will now look like: ``` for (int square = 0; square < 64; ++square) { for (int piece = 0; piece < 6; ++piece) { contention[square] += GetPieceAttacks((PieceType)piece, new Square(square), board, isWhiteFriendly); contention[square] -= GetPieceAttacks((PieceType)piece, new Square(square), board, !isWhiteFriendly); } } ``` Instead of: ``` for (int square = 0; square < 64; ++square) { for (int piece = 0; piece < 6; ++piece) { switch ((PieceType)piece) { PieceType.Pawn => ..., PieceType.Knight => ..., ..., ..., } } } ``` This better suits the limits of the challenge by reducing the amount of lexemes required to fetch the same information. --- Chess-Challenge/src/API/BitboardHelper.cs | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Chess-Challenge/src/API/BitboardHelper.cs b/Chess-Challenge/src/API/BitboardHelper.cs index 019478d..5a07a8e 100644 --- a/Chess-Challenge/src/API/BitboardHelper.cs +++ b/Chess-Challenge/src/API/BitboardHelper.cs @@ -51,6 +51,46 @@ namespace ChessChallenge.API return BitBoardUtility.PopCount(bitboard); } + + + /// + /// Returns a bitboard where each bit that is set to 1 represents a square that the given + /// piece type is able to attack. These attacks are calculated from the given square, + /// and take the given board state into account (so queen, rook, and bishop attacks will be blocked by pieces that are in the way). + /// + public static ulong GetPieceAttacks(PieceType pieceType, Square square, Board board, bool isWhite) + { + return pieceType switch + { + PieceType.Pawn => GetPawnAttacks(square, isWhite), + PieceType.Knight => GetKnightAttacks(square), + PieceType.Bishop => GetBishopAttacks(square, board.AllPiecesBitboard), + PieceType.Rook => GetRookAttacks(square, board.AllPiecesBitboard), + PieceType.Queen => GetQueenAttacks(square, board.AllPiecesBitboard), + PieceType.King => GetKingAttacks(square), + _ => 0 + }; + } + + /// + /// Returns a bitboard where each bit that is set to 1 represents a square that the given + /// piece type is able to attack. These attacks are calculated from the given square, + /// and take the given board state into account (so queen, rook, and bishop attacks will be blocked by pieces that are in the way). + /// + public static ulong GetPieceAttacks(PieceType pieceType, Square square, ulong blockers, bool isWhite) + { + return pieceType switch + { + PieceType.Pawn => GetPawnAttacks(square, isWhite), + PieceType.Knight => GetKnightAttacks(square), + PieceType.Bishop => GetBishopAttacks(square, blockers), + PieceType.Rook => GetRookAttacks(square, blockers), + PieceType.Queen => GetQueenAttacks(square, blockers), + PieceType.King => GetKingAttacks(square), + _ => 0 + }; + } + /// /// Returns a bitboard where each bit that is set to 1 represents a square that the given /// sliding piece type is able to attack. These attacks are calculated from the given square,