Friday, June 29, 2007

Chess computer and castling

You may have noticed in my last published game by my chess computer that none of the sides castled. That's no accident since I have no particular code to make it value castling (and in fact nothing to even make it understand when it's illegal -- it may, for example, move the king and then go back and then castle. Cheater!) That's because I haven't decided on how to do that.

It's not entirely trivial, for the following reason. You want the computer to value castling, but disvalue losing the ability to castle with a non-castling king move. So, you can't just have a single boolean variable that contains info on whether it has the possibility to castle and make your judgement based on that, because you haven't distinguished losing the ability to castle by castling, and losing it by just moving the king elsewhere, and you have to give points based on having the ability to castle or not having it. Which is best? There is no way to tell just from examining that variable.

At first I solved this by having two booleans. One called "can_castle" and one "have_castled". The default for them is of course "true" and "false", respectively. And then I gave plus for the combination have_castled = true and can_castle = false, and minus for the combination can_castle = false and have_castled = false, and neutral (no value either way) for can_castle = true and have_castled = false (the default). (Observe that the fourth combination -- can_castle = true and have_castled = false -- can never occur since having castled by necessity also means losing the ability to castle.)

Now I've decided to use just one variable, a simple int using the values 0,1,2 to cover the above possibilities. [Edit: 0-3 is better, so I can differentiate between short and long castling.] But I haven't implemented it yet. So I currently have nothing to tell the Ai to value castling specifically, which is why none of the sides castled in the recently published game. And that's that. (It does castle from time to time anyway, to achieve some other goal, like a more active rook.)

Edit: it didn't occur to me until now that there are cases when castling in one direction is all that ever will be allowed (when the castling rook has been moved). That has to be reflected in the code somehow too. Guess I have to use more numbers.

No comments:

Locations of visitors to this page