Disservin / chess-library

C++ chess library
https://disservin.github.io/chess-library/
MIT License
71 stars 22 forks source link

Attackers method in Board #24

Closed Orbital-Web closed 1 year ago

Orbital-Web commented 1 year ago

Hi, this is my first commit on a public repo so I apologize in advance if I made a terrible mistake somewhere (either in my code or in this general process).

Anyways, I made two main changes.

One is that I added a attackers() method inside of the Board class to get a bitboard for every piece of a certain color attacking a certain square. I added occupancy as a parameter to help find xray attackers, which is mostly useful for implementing Static Exchange Evaluations in engines (hence why I made this PR). You can basically modify the occupancy you input to virtually remove certain pieces, such as the first rook in a double rook battery, allowing you to find more attackers without having to play out any move.

The other change is a very minor one; I just replaced the isAttacked code inside game end detection with inCheck for readability (and less repetition, I guess).

Disservin commented 1 year ago

Cool, thanks! Could you also document this in the docs? docs/board-object.md and change the description to this?

    /// @brief Returns a bitboard with the origin squares of the attacking pieces set. 
    /// @param color Attacker Color
    /// @param square Attacked Square
    /// @param occupied
    /// @return 
Orbital-Web commented 1 year ago

Yup, those changes are pushed now.

Disservin commented 1 year ago

Hi, thanks again ^^

Just wanted to let you know that I moved the attackers function into the attacks namespace and moved that namespace (attacks) out of the movegen namespace. So previous movegen::attacks:: are now just attacks:: and board.attackers() is attacks::attackers(board, ...)

Orbital-Web commented 1 year ago

Hi, I noticed a pretty important issue. After commit 96b5fae (where the namespaces have been moved around), any cpp files that includes the chess library no longer works. This can be tested by creating a cpp file with the content

#include <chess.hpp>

int main() {
    std::cout << "Hello World!\n";
    return 0;
}

The program compiles, but does not print Hello World as intended. This was just a demonstration to show that the cpp file does not work, but any code inside main will not run. I have no idea why this happens though.

Another thing, I noticed in the code I wrote that the attackers method should be defined as a const method to avoid passing 'const chess::Board' as 'this' argument discards qualifiers [-fpermissive], though I guess this won't be a problem if attackers is being moved into attacks namespace taking board as an input.

Disservin commented 1 year ago

Thanks for pointing this out, idk how I missed this. Should be fixed now.