X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fendgame.cpp;h=d9e76348ecdc8f57916ba65ffc80ebebe57fc9bb;hb=527d832a6de81c455cc8818e85c309fa1443f862;hp=76fb597aa096aad7a3431b3b72d33ebf43bcd8c3;hpb=d0e51bc0f0c77f93323aaa86c9c2485c41d38271;p=stockfish
diff --git a/src/endgame.cpp b/src/endgame.cpp
index 76fb597a..d9e76348 100644
--- a/src/endgame.cpp
+++ b/src/endgame.cpp
@@ -1,867 +1,745 @@
/*
- Glaurung, a UCI chess playing engine.
- Copyright (C) 2004-2008 Tord Romstad
+ Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+ Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
+ Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
+ Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
- Glaurung is free software: you can redistribute it and/or modify
+ Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
-
- Glaurung is distributed in the hope that it will be useful,
+
+ Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
-
-////
-//// Includes
-////
-
#include
-#include "bitbase.h"
+#include "bitboard.h"
#include "endgame.h"
-
-
-////
-//// Constants and variables
-////
-
-/// Evaluation functions
-
-// Generic "mate lone king" eval:
-KXKEvaluationFunction EvaluateKXK = KXKEvaluationFunction(WHITE);
-KXKEvaluationFunction EvaluateKKX = KXKEvaluationFunction(BLACK);
-
-// KBN vs K:
-KBNKEvaluationFunction EvaluateKBNK = KBNKEvaluationFunction(WHITE);
-KBNKEvaluationFunction EvaluateKKBN = KBNKEvaluationFunction(BLACK);
-
-// KP vs K:
-KPKEvaluationFunction EvaluateKPK = KPKEvaluationFunction(WHITE);
-KPKEvaluationFunction EvaluateKKP = KPKEvaluationFunction(BLACK);
-
-// KR vs KP:
-KRKPEvaluationFunction EvaluateKRKP = KRKPEvaluationFunction(WHITE);
-KRKPEvaluationFunction EvaluateKPKR = KRKPEvaluationFunction(BLACK);
-
-// KR vs KB:
-KRKBEvaluationFunction EvaluateKRKB = KRKBEvaluationFunction(WHITE);
-KRKBEvaluationFunction EvaluateKBKR = KRKBEvaluationFunction(BLACK);
-
-// KR vs KN:
-KRKNEvaluationFunction EvaluateKRKN = KRKNEvaluationFunction(WHITE);
-KRKNEvaluationFunction EvaluateKNKR = KRKNEvaluationFunction(BLACK);
-
-// KQ vs KR:
-KQKREvaluationFunction EvaluateKQKR = KQKREvaluationFunction(WHITE);
-KQKREvaluationFunction EvaluateKRKQ = KQKREvaluationFunction(BLACK);
-
-
-/// Scaling functions
-
-// KBP vs K:
-KBPKScalingFunction ScaleKBPK = KBPKScalingFunction(WHITE);
-KBPKScalingFunction ScaleKKBP = KBPKScalingFunction(BLACK);
-
-// KQ vs KRP:
-KQKRPScalingFunction ScaleKQKRP = KQKRPScalingFunction(WHITE);
-KQKRPScalingFunction ScaleKRPKQ = KQKRPScalingFunction(BLACK);
-
-// KRP vs KR:
-KRPKRScalingFunction ScaleKRPKR = KRPKRScalingFunction(WHITE);
-KRPKRScalingFunction ScaleKRKRP = KRPKRScalingFunction(BLACK);
-
-// KRPP vs KRP:
-KRPPKRPScalingFunction ScaleKRPPKRP = KRPPKRPScalingFunction(WHITE);
-KRPPKRPScalingFunction ScaleKRPKRPP = KRPPKRPScalingFunction(BLACK);
-
-// King and pawns vs king:
-KPsKScalingFunction ScaleKPsK = KPsKScalingFunction(WHITE);
-KPsKScalingFunction ScaleKKPs = KPsKScalingFunction(BLACK);
-
-// KBP vs KB:
-KBPKBScalingFunction ScaleKBPKB = KBPKBScalingFunction(WHITE);
-KBPKBScalingFunction ScaleKBKBP = KBPKBScalingFunction(BLACK);
-
-// KBP vs KN:
-KBPKNScalingFunction ScaleKBPKN = KBPKNScalingFunction(WHITE);
-KBPKNScalingFunction ScaleKNKBP = KBPKNScalingFunction(BLACK);
-
-// KNP vs K:
-KNPKScalingFunction ScaleKNPK = KNPKScalingFunction(WHITE);
-KNPKScalingFunction ScaleKKNP = KNPKScalingFunction(BLACK);
-
-// KPKP
-KPKPScalingFunction ScaleKPKPw = KPKPScalingFunction(WHITE);
-KPKPScalingFunction ScaleKPKPb = KPKPScalingFunction(BLACK);
-
-
-////
-//// Local definitions
-////
+#include "movegen.h"
namespace {
- // Table used to drive the defending king towards the edge of the board
- // in KX vs K and KQ vs KR endgames:
- const uint8_t MateTable[64] = {
- 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 defending king towards a corner square of the
- // right color in KBN vs K endgames:
- const uint8_t KBNKMateTable[64] = {
- 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
- };
-
- // The attacking side is given a descending bonus based on distance between
- // the two kings in basic endgames:
- const int DistanceBonus[8] = {0, 0, 100, 80, 60, 40, 20, 10};
-
- // Bitbase for KP vs K:
- uint8_t KPKBitbase[24576];
-
- // Penalty for big distance between king and knight for the defending king
- // and knight in KR vs KN endgames:
- const int KRKNKingKnightDistancePenalty[8] = { 0, 0, 4, 10, 20, 32, 48, 70 };
-
- // Various inline functions for accessing the above arrays:
-
- inline Value mate_table(Square s) {
- return Value(MateTable[s]);
+ // Used to drive the king towards the edge of the board
+ // in KX vs K and KQ vs KR endgames.
+ // Values range from 27 (center squares) to 90 (in the corners)
+ 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);
}
- inline Value kbnk_mate_table(Square s) {
- return Value(KBNKMateTable[s]);
+ // Used to drive the king towards A1H8 corners in KBN vs K endgames.
+ // Values range from 0 on A8H1 diagonal to 7 in A1H8 corners
+ inline int push_to_corner(Square s) {
+ return abs(7 - rank_of(s) - file_of(s));
}
- inline Value distance_bonus(int d) {
- return Value(DistanceBonus[d]);
- }
+ // 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); }
- inline Value krkn_king_knight_distance_penalty(int d) {
- return Value(KRKNKingKnightDistancePenalty[d]);
+#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
- // Function for probing the KP vs K bitbase:
- int probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm);
+ // 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) {
-}
-
+ assert(pos.count(strongSide) == 1);
-////
-//// Functions
-////
+ if (file_of(pos.square(strongSide)) >= FILE_E)
+ sq = flip_file(sq);
-/// Constructors
+ return strongSide == WHITE ? sq : flip_rank(sq);
+ }
-EndgameEvaluationFunction::EndgameEvaluationFunction(Color c) {
- strongerSide = c;
- weakerSide = opposite_color(strongerSide);
-}
+} // namespace
-KXKEvaluationFunction::KXKEvaluationFunction(Color c) : EndgameEvaluationFunction(c) { }
-KBNKEvaluationFunction::KBNKEvaluationFunction(Color c) : EndgameEvaluationFunction(c) { }
-KPKEvaluationFunction::KPKEvaluationFunction(Color c) : EndgameEvaluationFunction(c) { }
-KRKPEvaluationFunction::KRKPEvaluationFunction(Color c) : EndgameEvaluationFunction(c) { }
-KRKBEvaluationFunction::KRKBEvaluationFunction(Color c) : EndgameEvaluationFunction(c) { }
-KRKNEvaluationFunction::KRKNEvaluationFunction(Color c) : EndgameEvaluationFunction(c) { }
-KQKREvaluationFunction::KQKREvaluationFunction(Color c) : EndgameEvaluationFunction(c) { }
+namespace Endgames {
-ScalingFunction::ScalingFunction(Color c) {
- strongerSide = c;
- weakerSide = opposite_color(c);
-}
+ std::pair