Legality move test

Code, algorithms, languages, construction...
Post Reply
tetra
Posts: 17
Joined: Sun Jul 06, 2014 1:14 am
Real Name: Ricardo

Legality move test

Post by tetra » Sun Jul 06, 2014 1:47 am

Hello!

I'm trying to understand something about legality test in move generation but so far no luck..

I've implemented a move generation with bitboards and now I want to know which of the generated moves are legal.. On my first approach I just make the move and I see if that move resulted in discovered check. This approach is easily implemented, but I found out that is a much faster way to do this. The idea is to calculate a bitboard of absolute pins before move generation to later discard obvious illegal moves. I have implemented this and now on the move generation we need to check for 3 cases: King in single check, King in double check and King not in check. What I can't understand is this:

"Not in Check:
-The moving piece is not absolutely pinned on its move direction" → from chessprogramming wikispaces.

So I have a bitboard of absolute pins, a bitboard of moves for each piece (that are generated with magic bitboards, shift etc..) and I want to know how I check if the moving piece is not absolutely pinned on its move direction (forget the implementation to see if is none, single or double check, I'am focus on not in check). For the move direction I've implemented a "in between" function needed to the absolute pins. Could this help here?

Can someone explain "The moving piece is not absolutely pinned on its move direction" better? for example with pseudo-code?

hyatt
Posts: 1242
Joined: Thu Jun 10, 2010 2:13 am
Real Name: Bob Hyatt (Robert M. Hyatt)
Location: University of Alabama at Birmingham
Contact:

Re: Legality move test

Post by hyatt » Sun Jul 06, 2014 4:44 am

The idea is simple enough. Let's take bishops and rooks separately. With a white king at e1 and a white bishop at c3, a diagonal moving piece on either b4 or a5 is pinning the bishop on c3. But if the bishop moves along that direction of travel, it doesn't expose the king to check and is therefore legal (if the pinning piece is on a5, then the bishop on c3 can legally go to b4 or d2. But it can't move anywhere along the other diagonal.

Of course the bishop can be pinned on the king in a direction it can not move (for example, white king e1, white bishop at e2, black rook/queen anywhere on the e-file where it is directly attacking the bishop at e2. Now the bishop has zero legal moves as any move takes it out of the blocking position it occupies and exposes the king to check.

If you really want to do this, the easiest way is to do it with a loop (and once you understand the loop you can simplify the process a bit with bit boards, but not completely). Find the white king. Then look down each ray one at a time for the first friendly piece you find. Then look down that ray for the next piece. If it is an enemy piece that moves up and down that ray, then the intermediate piece you found is pinned and if it moves, it must stay on that same ray to avoid exposing the king to check.

Seems like a lot of work that is not really going to help much. Most moves are legal, so screening the illegal moves is simpler, either by (a) noticing after making a move your own king is in check, which is illegal, or by (b) noticing at the next ply you capture the opponent's (yours now since you are one ply deeper) king which means his last move was illegal. Since most moves are illegal, except for the cases where you start off in check, it makes more sense to check them for legality at the last possible minute, rather than when generating them, because 1/2 of the nodes you search only need to search 1-2 moves before you get a cutoff and ignore the rest. If you are in check, then recognizing illegal moves and only generating legal moves can have a benefit, because now you have the opposite case, namely that most moves are illegal in this position.

tetra
Posts: 17
Joined: Sun Jul 06, 2014 1:14 am
Real Name: Ricardo

Re: Legality move test

Post by tetra » Sun Jul 06, 2014 10:52 am

Thanks for the explanation, but I was hoping for a faster way.. The for loop looks too slow. Is there another way but with bitboards and with cpu instructions like "AND", "OR"?
If that's the case I think it would be faster to make the move and see if that move resulted in discovered check only when the King isn't in check, but not sure.

Post Reply