X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fendgame.cpp;h=e232da6243a32f0e2c9e647bf1814e0c916689bd;hb=bde1506ba56ae566ac4e797e642017fe386f6425;hp=635ff4085382ad07f4d2ecc67a7fbcfacb615695;hpb=3cddb0c0765e7a4ecd66806d92b72e1faddac4c4;p=stockfish
diff --git a/src/endgame.cpp b/src/endgame.cpp
index 635ff408..e232da62 100644
--- a/src/endgame.cpp
+++ b/src/endgame.cpp
@@ -1,7 +1,8 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
- Copyright (C) 2008-2013 Marco Costalba, Joona Kiiski, Tord Romstad
+ Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
+ Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, 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
@@ -17,120 +18,79 @@
along with this program. If not, see .
*/
-#include
#include
#include "bitboard.h"
-#include "bitcount.h"
#include "endgame.h"
#include "movegen.h"
-using std::string;
-
namespace {
- // Table used to drive the king towards the edge of the board
+ // Used to drive the king towards the edge of the board
// in KX vs K and KQ vs KR endgames.
- const int PushToEdges[SQUARE_NB] = {
- 100, 90, 80, 70, 70, 80, 90, 100,
- 90, 70, 60, 50, 50, 60, 70, 90,
- 80, 60, 40, 30, 30, 40, 60, 80,
- 70, 50, 30, 20, 20, 30, 50, 70,
- 70, 50, 30, 20, 20, 30, 50, 70,
- 80, 60, 40, 30, 30, 40, 60, 80,
- 90, 70, 60, 50, 50, 60, 70, 90,
- 100, 90, 80, 70, 70, 80, 90, 100,
- };
-
- // Table used to drive the king towards a corner square of the
- // right color in KBN vs K endgames.
- const int PushToCorners[SQUARE_NB] = {
- 200, 190, 180, 170, 160, 150, 140, 130,
- 190, 180, 170, 160, 150, 140, 130, 140,
- 180, 170, 155, 140, 140, 125, 140, 150,
- 170, 160, 140, 120, 110, 140, 150, 160,
- 160, 150, 140, 110, 120, 140, 160, 170,
- 150, 140, 125, 140, 140, 155, 170, 180,
- 140, 130, 140, 150, 160, 170, 180, 190,
- 130, 140, 150, 160, 170, 180, 190, 200
- };
-
- // Tables used to drive a piece towards or away from another piece
- const int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
- const int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 };
+ inline int push_to_edge(Square s) {
+ int rd = edge_distance(rank_of(s)), fd = edge_distance(file_of(s));
+ return 90 - (7 * fd * fd / 2 + 7 * rd * rd / 2);
+ }
-#ifndef NDEBUG
- bool verify_material(const Position& pos, Color c, Value npm, int num_pawns) {
- return pos.non_pawn_material(c) == npm && pos.count(c) == num_pawns;
+ // Used to drive the king towards A1H8 corners in KBN vs K endgames.
+ inline int push_to_corner(Square s) {
+ return abs(7 - rank_of(s) - file_of(s));
}
-#endif
- // Get the material key of a Position out of the given endgame key code
- // like "KBPKN". The trick here is to first forge an ad-hoc fen string
- // and then let a Position object to do the work for us. Note that the
- // fen string could correspond to an illegal position.
- Key key(const string& code, Color c) {
+ // Drive a piece close to or away from another piece
+ inline int push_close(Square s1, Square s2) { return 140 - 20 * distance(s1, s2); }
+ inline int push_away(Square s1, Square s2) { return 120 - push_close(s1, s2); }
- assert(code.length() > 0 && code.length() < 8);
- assert(code[0] == 'K');
+#ifndef NDEBUG
+ bool verify_material(const Position& pos, Color c, Value npm, int pawnsCnt) {
+ return pos.non_pawn_material(c) == npm && pos.count(c) == pawnsCnt;
+ }
+#endif
- string sides[] = { code.substr(code.find('K', 1)), // Weak
- code.substr(0, code.find('K', 1)) }; // Strong
+ // Map the square as if strongSide is white and strongSide's only pawn
+ // is on the left half of the board.
+ Square normalize(const Position& pos, Color strongSide, Square sq) {
- std::transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower);
+ assert(pos.count(strongSide) == 1);
- string fen = sides[0] + char('0' + int(8 - code.length()))
- + sides[1] + "/8/8/8/8/8/8/8 w - - 0 10";
+ if (file_of(pos.square(strongSide)) >= FILE_E)
+ sq = flip_file(sq);
- return Position(fen, false, NULL).material_key();
+ return strongSide == WHITE ? sq : flip_rank(sq);
}
- template
- void delete_endgame(const typename M::value_type& p) { delete p.second; }
-
} // namespace
-/// Endgames members definitions
-
-Endgames::Endgames() {
-
- add("KPK");
- add("KNNK");
- add("KBNK");
- add("KRKP");
- add("KRKB");
- add("KRKN");
- add("KQKP");
- add("KQKR");
- add("KBBKN");
-
- add("KNPK");
- add("KNPKB");
- add("KRPKR");
- add("KRPKB");
- add("KBPKB");
- add("KBPKN");
- add("KBPPKB");
- add("KRPPKRP");
-}
+namespace Endgames {
-Endgames::~Endgames() {
+ std::pair