]> git.sesse.net Git - stockfish/blobdiff - src/endgame.cpp
Add KNPKB endgame
[stockfish] / src / endgame.cpp
index 8591676305f434ef8c10716058624d70d3dc32c5..52786cdbe3b58b6166192802f35ab2f3eef674a8 100644 (file)
@@ -1,7 +1,7 @@
 /*
   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
-  Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
+  Copyright (C) 2008-2013 Marco Costalba, Joona Kiiski, Tord Romstad
 
   Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -100,6 +100,7 @@ Endgames::Endgames() {
   add<KBBKN>("KBBKN");
 
   add<KNPK>("KNPK");
+  add<KNPKB>("KNPKB");
   add<KRPKR>("KRPKR");
   add<KBPKB>("KBPKB");
   add<KBPKN>("KBPKN");
@@ -904,6 +905,24 @@ ScaleFactor Endgame<KNPK>::operator()(const Position& pos) const {
 }
 
 
+/// K, knight and a pawn vs K and bishop. If knight can block bishop from taking
+/// pawn, it's a win. Otherwise, drawn.
+template<>
+ScaleFactor Endgame<KNPKB>::operator()(const Position& pos) const {
+
+  Square pawnSq = pos.piece_list(strongerSide, PAWN)[0];
+  Square bishopSq = pos.piece_list(weakerSide, BISHOP)[0];
+  Square weakerKingSq = pos.king_square(weakerSide);
+
+  // King needs to get close to promoting pawn to prevent knight from blocking.
+  // Rules for this are very tricky, so just approximate.
+  if (forward_bb(strongerSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
+      return ScaleFactor(square_distance(weakerKingSq, pawnSq));
+
+  return SCALE_FACTOR_NONE;
+}
+
+
 /// K and a pawn vs K and a pawn. This is done by removing the weakest side's
 /// pawn and probing the KP vs K bitbase: If the weakest side has a draw without
 /// the pawn, she probably has at least a draw with the pawn as well. The exception