Saturday, November 12, 2011

You can't win with 2 knights, so it's a draw, isn't it ?

I just encountered an awkward bug in iCE. I had some simple rules in my evaluation that check whether there is still enough mating material on the board. One of those rules was

If the only material left on the board is 2 knights it is a draw.

This is in general correct, unfortunately not always and according Murphy's law things that can go wrong will go wrong.

I was still coding 5 man endgame knowledge and I was just working on a a very simple one. King and 2 knights vs King and 1 knight (KNNKN). This is also almost always a draw, but as the defender side still has a knight, it can now be mated where without the knight the move sequence would only lead to a stalemate. So some easy checks to recognize those positions and otherwise declare a draw.

I then checked whether it works correctly in the engine with that position

 White moves and mates in 4
A possible mate sequence is

1. Kc2  Nc5    2. Nb4+  Ka1    3. Nd4  Nb3     4. Nxb3#

Leading to this position
Mate !
So now Murphy's law hits and iCE was not recognizing this Mate, because it obeyed the 2 knights rule and announced a draw. iCE was therefore not able to solve this simple Mate in 4. Even if this probably never appears in a real game as problem it bothers me to know it's there. I modified the 2 knights draw rule slightly so that is does not announce a draw if the defender is in check and has nowhere to go. Knowing only 2 knights are on the board this is a simple mate check (much simpler than in a normal position). And now it solves this simple position correctly in no time.

iCE 0.3 build 491 [2011.11.12]
position fen 8/8/8/1N6/n7/3N4/k7/2K5 w - - 0 1
go depth 8
info depth 1 time 0 nodes 34 pv b5d6  nps 33999 score cp 22
info depth 2 time 0 nodes 116 pv c1c2 a4b2  nps 115999 score cp 22 
info depth 3 time 16 nodes 333 pv c1c2 a4b2 d3b4 a2a1 b5c7  nps 20812 score cp 23
info depth 4 time 0 nodes 606 pv c1c2 a4b2 d3b4 a2a1 b5c7 b2d1  nps 605999 score cp 23
info depth 5 time 15 nodes 1012 pv c1c2 a4b2 d3b4 a2a1 b5d4 b2d1 d4b3 nps 67466 score mate 4
info depth 6 time 16 nodes 1490 pv c1c2 a4b2 d3b4 a2a1 b5d4 b2d1 d4b3 nps 93124 score mate 4
info depth 7 time 15 nodes 1614 pv c1c2 a4b2 d3b4 a2a1 b5d4 b2d1 d4b3 nps 107600 score mate 4
bestmove c1c2 ponder a4b2