I haven't implemented mobility or king safety yet, but as a chess player, I can say: don't conflate mobility and attacks on the king. A piece can definitely attack the opposite king zone, and still not be very mobile; such as a rook on h8 which is blocked by its own king on g8. If the other king is well defended, for example by a knight on f3 which defends h2, or has even played h2h3 and has a queen on e3 defending h3, then the "attack" may not bring you much.algerbrex wrote: ↑Fri Nov 12, 2021 2:48 am Any advice from others who have experience with implementing king safety would be appreciated. Otherwise, I'll continue tinkering with things myself, as it seems like I'm close to getting it all to work, based on some games I've seen the dev version of Blunder play.
It would mean that your engine is going to leave the rook stuck on h8 because it is "attacking".
It would be better to _first_ calculate mobility, and _then_ calculate attacking values and stack that on top of the mobility values.
Remember that in chess, an attack comes from a superior position. It is not the other way around: an attack does _not_ necessarily give you a superior position. A superior position comes from:
- Having your king safe
- Being more mobile
- Have the king of the opponent in the open
- Attack the king
So that is the order in which you have to calculate the values.
Don't conflate any of them. Your safety value is not less because the king's zone is attacked; your opponent just gets more attacking points. That may make your king less safe, but it does not lower your king safety value.
Let's say you are being shot at by a single person with one gun. You are behind a wall which can't be penetrated by the gun. You are therefore safe. Now your opponent whips out a cannon that could demolish the wall. Your safety value is still exactly the same, but your opponent's attacking points became much more; so you are now less safe. Not because your position became less safe, but because the opponent has more attacking power.
So you calculate:
- king safety (pawn shield, open lines or not, own pieces near king)
- your own mobility value
- your own attacking value
Then you add all of them for each side. If you subtract your opponent's total from yours and you are still in the positive, your position is superior, and therefore there will be tactical attacking chances. (Assuming your calculations are half-way decent, and the positive value is large enough.)
There can be more terms involved in calculating the positional value. If your positional value is +125 _after_ you subtract your opponent's value, but your opponent is 1 pawn up, you are still +25; your position is so superior that it compensates for the loss of one pawn.