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

Does anyone know how to find which squares/how many each piece is attacking? #215

Open KingKobra7899 opened 1 year ago

KingKobra7899 commented 1 year ago

I made an eval function in c++ which uses this in addition to material and im trying to implement the same thing for this

NiekvD commented 1 year ago

you can filter the legal moves where the starting square is the square of the piece

DiogoSL commented 1 year ago

I want to get all squares a piece is seeing. Not just the squares it can go to. How can I achieve this? Do I need to write a function for every piece that calculates the vision given a square?

SebLague commented 1 year ago

Hi, the BitboardHelper class has some functions for getting a bitboard of all attacked squares by a given piece. See GetSliderAttacks/GetKnightAttacks/GetKingAttacks/GetPawnAttacks. You can then use the GetNumberOfSetBits function for example to count how many squares are set in the bitboard.

Lobsterme9 commented 1 year ago

Hi, the BitboardHelper class has some functions for getting a bitboard of all attacked squares by a given piece. See GetSliderAttacks/GetKnightAttacks/GetKingAttacks/GetPawnAttacks. You can then use the GetNumberOfSetBits function for example to count how many squares are set in the bitboard.

is there a way to get the number of pieces attacking a particular square?

ateready commented 1 year ago

is there a way to get the number of pieces attacking a particular square?

Nope, SquareIsAttackedByOpponent is a boolean so can't give a number, and otherwise only the IsCapture boolean for moves or the get attacks functions for bitboards can find attacks.

You could create one yourself by iterating over each piece using the bitboard attack functions, or find every square that is attacked using SquareIsAttackedByOpponent and find out how many pieces are attacking it in reverse.

SKAE5 commented 1 year ago

Hi, the BitboardHelper class has some functions for getting a bitboard of all attacked squares by a given piece. See GetSliderAttacks/GetKnightAttacks/GetKingAttacks/GetPawnAttacks. You can then use the GetNumberOfSetBits function for example to count how many squares are set in the bitboard.

Hi, do the GetAttacks functions only consider opponent pieces being attacked? If I use, say, the GetKnightAttacks with a white knight as input, does this return only black pieces in squares under the knight attack, or also white pieces?

jonigrin commented 1 year ago

Hi, do the GetAttacks functions only consider opponent pieces being attacked? If I use, say, the GetKnightAttacks with a white knight as input, does this return only black pieces in squares under the knight attack, or also white pieces? no, it's not working based on the board, it's just working on bitboard so the knight attacks will just return all 8 squares but, you could bitwise AND it with the bitboard of the opponent pieces so you get a bitboard of oppenent pieces that are attacked by your knight

SKAE5 commented 1 year ago

Hi, do the GetAttacks functions only consider opponent pieces being attacked? If I use, say, the GetKnightAttacks with a white knight as input, does this return only black pieces in squares under the knight attack, or also white pieces? no, it's not working based on the board, it's just working on bitboard so the knight attacks will just return all 8 squares but, you could bitwise AND it with the bitboard of the opponent pieces so you get a bitboard of oppenent pieces that are attacked by your knight

Thank you