Better safe than sorry…

As already mentioned in another article, the main method of winning (or losing) a chess game is through a double attack (check the “Chess programming and basic chess principles: Episode 2 – Double attack” article).

So how can the computer program we develop be protected against such attacks?

Obviously, the computer needs to detect such possible positions and then move its pieces accordingly. But prevention is always the best therapy.

If we try to develop a chess program that seamlessly maneuvers away from such dangerous positions that would allow a double attack, we could follow a simple rule that will save us from trouble.

Rule: Having your King in the same column, row or diagonal with an attacking piece of the opponent is something that is not desirable.

Consider this position (last move is Qb3):

The King is in the same diagonal with the queen. This does not pose a threat now, but things could be complicated pretty fast, e.g. after c5.

So why not promote the score of the move that moves the king away from the (potential) danger? Something that could make the computer play Kh7 here, considering that there is no other pressing issue or opportunity to follow.

There you go! Safe and sound.

Surely in a computer chess program that anyway calculates everything in a sufficient depth, such ‘tricks’ are probably not needed. However, embedding this rule in the algorithm could allow the computer to avoid dangerous positions without explicitly calculating all the possible moves in a given position, thus saving time and computing time. This can be beneficial, especially in cases where the code size is of importance (as in Huo Chess).

Keep coding!

Keep experimenting!

Keep having fun!

Leave a comment