SebLague / Chess-Challenge

Create your own tiny chess bot!
https://www.youtube.com/watch?v=Ne40a5LkK6A
MIT License
1.78k stars 1.06k forks source link

Is there any way to get our Piece's attacking squares or get a square's attacking pieces? #316

Open Kaanavciexe opened 1 year ago

Kaanavciexe commented 1 year ago

I want to get our Piece's attacking squares or get a square's attacking pieces. For example my knight on f3 and i want to take its attacking squares for example e5, g5... IS there any way to do this?

mcthouacbb commented 1 year ago

If you're willing to use bitboards, you can use ChessChallenge.Chess.Bits.KnightAttacks[knightSquare] to get the bitboard of all the knight's attacks

EikeSchwass commented 1 year ago

You could also count the legal moves that start at the knights position. Don't know if that's exactly what your asking for

TrevorMcCubbin commented 1 year ago

I don't think you're allowed to use this namespace: ChessChallenge.Chess

ScottProgrammer commented 1 year ago

you can use the ChessChallenge.API.GetKnightAttacks to get the knights bitboard ChessChallenge.API is defiantly allowed where I also don't think ChessChallenge.Chess is allowed

if you want to see if it is attacking something it is a bit clunky in this situation as you need to test it against a square to see if it is attacking that square, I'm not sure how you can get it to list all squares in the bitboard (without looping all squares).

I'm am using it to check for attacks on specific pieces like this ulong Attacks = board.GetAllPieceLists(); OR ulong Attacks = p_board.GetPieceList(PieceType, IsWhite); to get specific pieces then loop through the pieces and use ChessChallenge.API.BitboardHelper.SquareIsSet(Attacks, piece.Square)

hope this helps, if anyone has a better way please post it here