Trying to understand check evasion move generation.

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
User avatar
JimmyRustles
Posts: 17
Joined: Sun May 05, 2019 9:24 pm
Full name: Steven Griffin

Trying to understand check evasion move generation.

Post by JimmyRustles » Sun Dec 29, 2019 7: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?

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.

elcabesa
Posts: 825
Joined: Sun May 23, 2010 11:32 am
Contact:

Re: Trying to understand check evasion move generation.

Post by elcabesa » Sun Dec 29, 2019 11:19 am

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

petero2
Posts: 594
Joined: Mon Apr 19, 2010 5:07 pm
Location: Sweden
Contact:

Re: Trying to understand check evasion move generation.

Post by petero2 » Mon Dec 30, 2019 2:56 pm

JimmyRustles wrote:
Sun Dec 29, 2019 7: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.

User avatar
JimmyRustles
Posts: 17
Joined: Sun May 05, 2019 9:24 pm
Full name: Steven Griffin

Re: Trying to understand check evasion move generation.

Post by JimmyRustles » Tue Dec 31, 2019 7:03 pm

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.

bob
Posts: 20667
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

Re: Trying to understand check evasion move generation.

Post by bob » Tue Dec 31, 2019 7:12 pm

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).

Post Reply