views:

267

answers:

3

Hello everybody! I am trying to write AI Chess and I have a problem. I am ready with pieces movement rules, and I'm trying to remove moves that are not valid (leave the king in check etc). I Wrote something like this:

ValidateMove(board);
{
for(i=0;i<64;i++)
if(board[i]==king.opposite) kingpos=board[i];

createmoves(board);

if (moves.contains(kingpos)) return false;
}

However, I'm using minimax + alpha beta and that validation is making my search really slow.

+5  A: 

Instead of checking for 'check' every move, just set losing your king to give a score of -infinity and end the game then the algorithm will never choose a move that allows the king to be lost unless there is no option.

Just watch out for stalemate which will need special treatment because according to the above algorithm it will result in a loss, but according to standard chess rules, it is called as a draw.

Mark Byers
That doesn't work for leaf nodes in the game tree. OTOH the farther out the leaves are, the less it impact it has.
phkahler
not so good if your king gets captured early in the tree... then you end up potentially searching a large number of invalid positions
tbischel
+1  A: 

My understanding is that finding valid moves usually takes the most time in most chess algorithms.

You may be able to improve your overall performance by improving your move generation by using the clever Bitboard techniques found in computer chess theory.

http://www.frayn.net/beowulf/theory.html

http://en.wikipedia.org/wiki/Board_representation_%28chess%29

http://en.wikipedia.org/wiki/Bitboard

tarn
A: 

Here is a blog post with some source code that will get you started:

Chess Valid Moves Generator

Adam Berent