legal or pseudolegal move generator?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

tcusr
Posts: 323
Joined: Tue Aug 31, 2021 10:32 pm
Full name: tcusr

Re: legal or pseudolegal move generator?

Post by tcusr »

R. Tomasi wrote: Fri Sep 10, 2021 9:02 pm
tcusr wrote: Fri Sep 10, 2021 7:59 pm
Mergi wrote: Fri Sep 10, 2021 6:14 pm
R. Tomasi wrote: Fri Sep 10, 2021 1:01 pm Btw, I think you can speed that up a little, assuming you're using magic bitboards: use the king square and the opponent occupancy bitboard to perform a magic lookup, then & mask it with the occupancy bitboard of your own pieces. That will give you any of your pieces that are pinned to your king and have to be excluded from move generation.
You will still run into the problem of 2 or more pieces blocking at the same time, meaning that none of them is actually pinned. So you still need to go through every single attacker individually, cast a ray towards the king, and check that there's only 1 piece blocking. Also the magic lookups are not exactly free either, there's quite a lot of (cache unfriendly) operations needed to get the correct bitboard.

With your method, you get to immediately exclude attackers that are behind each other on the same ray, whereas i still go through all of them, but that should be fairly rare.
what if you generate the opponents rooks/queens attacks with the kogge stone algorithm and & those attacks with the rook attacks from your king?
if there are rooks/queens in this mask it means they are pinned and you can generate the moves for them separately, you wouldn't need "if (blockers & SquareToBB(origin_s))" anymore. you would need to do again for bishops/queens though
Interesting idea. Kogge-Stone isn't completely free, either. I guess you would have to implement it and measure how that fits with the rest of your engine/movegenerator design.
yes kogge-stone towards 1 direction is about ~20 instructions long but at least it's branchless and the usual "for (uint64_ t b = ...; b; b &= b - 1)" it's not an easily predictable branch. measuring is the only answer.

i also thought about using kogge-stone to generate legal king moves. in makeMove() you can calculate all the squares attacked by both sides and then your legalKingMoves is just one statement. return kingAttacks[sq] & ~attackedBy[otherSide] & ~us;
this attackedBy table may also become useful for your evaluation function. i may try this but i have other things to improve for now.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: legal or pseudolegal move generator?

Post by Sven »

The following has been posted a couple of times but forum search is almost unusable, so ...

Only very few types of pseudo-legal moves can actually be illegal: king moves, ep captures, and moves of a pinned piece. Also castling through check falls into this category but this can easily be handled during castling move generation (which can also be omitted when being in check).
So the whole effort of either
1) generating fully legal moves only, or
2) checking legality of generated pseudo-legal moves
can be limited to that small subset of moves. Strategy 2 can even be applied within the move generator itself, producing 100% legal movegen output by doing make-wasKingLeftInCheck-unmake for those few candidates.
Sven Schüle (engine author: Jumbo, KnockOut, Surprise)