Count legal moves

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

metax
Posts: 344
Joined: Wed Sep 23, 2009 5:56 pm
Location: Germany

Count legal moves

Post by metax »

Can I (with a mailbox board representation) count the number of legal moves without generating them?
metax
Posts: 344
Joined: Wed Sep 23, 2009 5:56 pm
Location: Germany

Re: Count legal moves

Post by metax »

Sorry, I meant the number of legal moves if the king is in check, which is of course an important difference.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Count legal moves

Post by Sven »

metax wrote:Can I (with a mailbox board representation) count the number of legal moves without generating them?
metax wrote:Sorry, I meant the number of legal moves if the king is in check, which is of course an important difference.
It depends upon which other information you have besides knowing that the king is in check. For instance, if you also know

- whether it is double check or not,
- the location of the checking piece (in case of single check), and
- the location(s) of all friendly pieces being pinned to your king,

then you can implement such a counting algorithm. For instance:

a) In case of double check, count all squares adjacent to friendly king that are not occupied by a friendly piece and not attacked by an enemy piece. For this kind of "is attacked" check, consider the friendly king as "transparent".

b) In case of single check, do the same as a) but add:

b1) the number of moves that can capture the checking piece with a piece that is not pinned (don't forget e.p. capture, and think twice when deciding about legality of e.p. capture move!);

b2) if the checking piece is a slider: the number of moves that block the line between checking piece and friendly king, provided the blocking piece is currently not pinned.

Hope that helps a little bit.

Sven