kz04px / libchess

C++ chess library
MIT License
18 stars 9 forks source link

No method to invert bitboard #2

Closed dave7895 closed 2 years ago

dave7895 commented 2 years ago

I am in the progress of writing a little program, in which I am using your library. This is when I discovered that there doesn't seem to be a method to invert a BitBoard. I thought it to be a quick fix, but it doesn't seem to be working. code:

    [[nodiscard]] /*constexpr*/ Bitboard operator!() const noexcept {
    std::cout << "used operator overload ! for BB\n";
        return !mask_;
    }

Output: rnb--bnr p-p-pkpp p--p---- ---q---- --P----- ----P--- PP-P-PPP R-B-K-NR Castling: KQ EP: 2 Turn: b

attacked squares: 00000000 00000000 00000000 01010000 00010100 11101111 11011101 01110110

not attacked squares: used operator overload ! for BB 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

dave7895 commented 2 years ago

Oh well, just after sending the issue I remembered that bitwise complement is done with ~ in C++.

kz04px commented 2 years ago

Glad everything is okay. If you end up having any more problems or feature requests, feel free to make another issue. I'm not totally convinced I implemented everything you might need, but I'm happy to help if I can.

dave7895 commented 2 years ago

One thing I was surprised about was that there is no method to check if a game is over, by either stalemate or checkmate, but those are pretty easy to emulate with an empty list of legal moves and either the king being in check or not, unless I am not considering some edge case?!

kz04px commented 2 years ago

One thing I was surprised about was that there is no method to check if a game is over, by either stalemate or checkmate, but those are pretty easy to emulate with an empty list of legal moves and either the king being in check or not, unless I am not considering some edge case?!

Seems you're right. If I remember correctly I added the things I needed while writing an engine and probably not much else. Seems I did it as you described here. If you'd like, I could add a gameover() function - or if you want to contribute and add it yourself I'd be happy to merge it.

dave7895 commented 2 years ago

I am happy to provide a PR, will probably be able to make it this weekend If you are totally bored, it would mean a great deal to me if you ever had the issue that the evaluation would switch sign whether the depth of your alphabeta was odd or even and you know how to remedy this. Edit: last sentence out of date, stupidly followed the wikipedia article without completely understanding the pseudo code... Still are welcome to play against my bot on lichess

kz04px commented 2 years ago

Chances are you'll want to use Negamax if you aren't already. It requires that the evaluation is from the side to move's perspective, which is usually done within the evaluation function itself. I guess your issue was to do with this? Glad you fixed it anyway.

dave7895 commented 2 years ago

welp, I just discovered I didn't fix it. If my evaluation returns positive for an advantage for the current player and vice versa, does the alternating nature of Negamax, which I already use, take care of the rest, or not? I am still confused if I have to invert any of the values before returning, and under which conditions Edit: it does seem to work now, but I don't know why