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.
This commit is contained in:
Inconspicuous Semicolon 2023-07-28 23:35:30 +01:00
parent 5aa54e4e51
commit a1fdbee6ba

View file

@ -51,6 +51,46 @@ namespace ChessChallenge.API
return BitBoardUtility.PopCount(bitboard); return BitBoardUtility.PopCount(bitboard);
} }
/// <summary>
/// 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).
/// </summary>
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
};
}
/// <summary>
/// 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).
/// </summary>
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
};
}
/// <summary> /// <summary>
/// Returns a bitboard where each bit that is set to 1 represents a square that the given /// 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, /// sliding piece type is able to attack. These attacks are calculated from the given square,