Page 1 of 1

Trying to understand check evasion move generation.

Posted: Sun Dec 29, 2019 8:48 am
by JimmyRustles
I'm trying to understand Texel's check evasion move generation so I can implement something similar in my engine.

https://github.com/B4dT0bi/texel/blob/m ... oveGen.cpp

I think I mostly understand it but there are a couple of things I'm not sure about.

Code: Select all

 validTargets |= pos.pieceTypeBB(OtherColor::KING); 
After generating the valid target squares, it adds the square of the opponent's king to the valid targets bitboard. Why does it do this?

It ands the move bitboards for each piece with validTargets so that only the check evasions will be generated. That makes sense.

I don't understand the king move generation though:

Code: Select all


    // King moves
    {
        int sq = pos.getKingSq(wtm);
        U64 m = BitBoard::kingAttacks(sq) & ~pos.colorBB(wtm);
        addMovesByMask(moveList, sq, m);
    }
It doesn't and the king moves bitboard with anything, so that would generate all the king moves, even the ones that put the king in check, right? Shouldn't this be anded with a bitboard of valid squares?

Thanks.

Re: Trying to understand check evasion move generation.

Posted: Sun Dec 29, 2019 12:19 pm
by elcabesa
regarding the first question I don't have an answer, regarding the second one (king moves) remember that Texel move generator create a list of pseudo legal moves, is the search that check that the move is legal before making the move.
Probably it has been measured that this way of generating moves, filters enough moves while doing generation and that filtering all the moves was simply slower

Re: Trying to understand check evasion move generation.

Posted: Mon Dec 30, 2019 3:56 pm
by petero2
JimmyRustles wrote: Sun Dec 29, 2019 8:48 am I'm trying to understand Texel's check evasion move generation so I can implement something similar in my engine.

https://github.com/B4dT0bi/texel/blob/m ... oveGen.cpp

I think I mostly understand it but there are a couple of things I'm not sure about.

Code: Select all

 validTargets |= pos.pieceTypeBB(OtherColor::KING); 
After generating the valid target squares, it adds the square of the opponent's king to the valid targets bitboard. Why does it do this?
This code is actually no longer needed. A long time ago (before this commit) Texel detected mates by performing king captures during search and at that time the code was needed. I simply forgot to remove that code. Thanks for noticing this.
I don't understand the king move generation though:

Code: Select all


    // King moves
    {
        int sq = pos.getKingSq(wtm);
        U64 m = BitBoard::kingAttacks(sq) & ~pos.colorBB(wtm);
        addMovesByMask(moveList, sq, m);
    }
It doesn't and the king moves bitboard with anything, so that would generate all the king moves, even the ones that put the king in check, right? Shouldn't this be anded with a bitboard of valid squares?
The check evasion function can generate moves that are not legal, like other Texel move generation functions. The illegal moves are removed during search. In this case I thought that trying to detect illegal king moves was not worth the effort to implement.

Re: Trying to understand check evasion move generation.

Posted: Tue Dec 31, 2019 8:03 pm
by JimmyRustles
Thanks for the explanations. I assumed it would be a fully legal move generator but it makes sense that it would be pseudo legal. Thanks.

Re: Trying to understand check evasion move generation.

Posted: Tue Dec 31, 2019 8:12 pm
by bob
You CAN do a fully-legal check evasion generator. It is painful to be sure. Moving the king is easy. Capturing the checking piece (if there is only one, ie not a discovered check) is easy. Interpositions is a little harder. Most are easy, just set target squares to be the set of squares between the king and the piece giving check (unless it is a knight or pawn where this is irrelevant.) But the hard part is to also toss in enpassant pawn captures. The pawn moves diagonally to an empty square that might block the checking diagonal. I forgot this one at one point and ran into a couple of cases where I thought I was mating my opponent but was not. Pretty complex bit of code. The advantage is that when in check, you avoid making the (mostly) illegal moves and then getting to the next ply, only to discover you can capture the king which refutes the move at the previous ply. You avoid all of that, so it becomes nothing more than a performance enhancement rather than a chess improvement (other than the gain in time of course).