King safety

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
cms271828
Posts: 316
Joined: Wed Apr 12, 2006 10:47 pm

King safety

Post by cms271828 »

Hi, is there a simple way to implement basic king safety?

Assuming king isn't on a or h file, then there are 3 pawns in front of king.
But theres a few variations of pawn cover..
Quite often one plays a3/h3, or b3/g3 to fianchetto a bishop, which can cause a hole if the bishop is captured.

Often, I like to castle king side, then advance the f pawn, which is supported by rook, in that case, theres only 2 pawns protecting king.

Also.. if you just wanted king behind a wall of 3 pawns, it might want to just sit in the middle, how can I encourage king to castle/'end up in either corner perhaps by not castling'

Thanks
Colin
Harald Johnsen

Re: King safety

Post by Harald Johnsen »

cms271828 wrote:Hi, is there a simple way to implement basic king safety?
No, because the king safety evaluation is one of the most speculative term in the eval. Of course a bad pawn structure evaluation is also speculative but with a bad castle the game can end in a few moves.
Assuming king isn't on a or h file, then there are 3 pawns in front of king.
But theres a few variations of pawn cover..
Quite often one plays a3/h3, or b3/g3 to fianchetto a bishop, which can cause a hole if the bishop is captured.
That's why king safety depends on the position of pieces too and not only on the position of pawns. The fastest way to handle that is to compute the king tropism (distance from pieces to king), it's of course also very inacurate.
Often, I like to castle king side, then advance the f pawn, which is supported by rook, in that case, theres only 2 pawns protecting king.
Opening a file is not a problem, the problem is being in check. So it's dangerous only if the opponent has more activity than you on this side of the board.
Also.. if you just wanted king behind a wall of 3 pawns, it might want to just sit in the middle, how can I encourage king to castle/'end up in either corner perhaps by not castling'
Thanks
The pawns don't protect the king anymore when they are advanced so if you include that in your eval the engine will prefer to castle. You surely have a table for pawns that gives a good eval for advanced pawn in the center and a bad eval for advanced pawn on the wings.

HJ.
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: King safety

Post by Gerd Isenberg »

http://members.home.nl/matador/chess840 ... G%20SAFETY

King safety is complex and several terms progressively interact.

A simple solution is to treat the king as slider (bishop and rook) and to minimize the "mobility" by own pawns and pieces, but to maximize the "mobility" considering neighboured squares attacked or even dominated by the opponent.

One common approach is to analyze the pawn-structure, for instance wing pawns and center pawns of both sides, to calculate some indices, stored inside the pawn hashtable. With such indices togeter with eg. the file, or "area" of opposing king, one may index precalculated piece-square tables - already considering long term closed rammed files (good for defending side) or open and half-open files and/or the ability to open files by pawn-levers (good for attacking side).

The need for a king to castle is encouraged by the pawn structure in the center and whether both kings are still in center and have about same options and number of tempi to castle.

Another term is king-tropism of pieces (average distance of pieces), or progressivly considering attacks and defends around or in front of the king.