X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fendgame.cpp;h=ae29a71aba07d0faa77847e7045a31f1ef92b58c;hb=09bef14c76e119103cc1a9404cbde7e249205deb;hp=11f060bb8b108f5fb53bc772097cd4dcb45972f0;hpb=5dc23121215039938a9ef4e59ae934312774571c;p=stockfish
diff --git a/src/endgame.cpp b/src/endgame.cpp
index 11f060bb..276b942e 100644
--- a/src/endgame.cpp
+++ b/src/endgame.cpp
@@ -1,868 +1,806 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
- Copyright (C) 2008 Marco Costalba
+ 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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
-
+
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"
+#include "movegen.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
-////
+using std::string;
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,
+ // Table used to drive the king towards the edge of the board
+ // in KX vs K and KQ vs KR endgames.
+ constexpr 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 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
+ // Table used to drive the king towards a corner square of the
+ // right color in KBN vs K endgames.
+ constexpr int PushToCorners[SQUARE_NB] = {
+ 6400, 6080, 5760, 5440, 5120, 4800, 4480, 4160,
+ 6080, 5760, 5440, 5120, 4800, 4480, 4160, 4480,
+ 5760, 5440, 4960, 4480, 4480, 4000, 4480, 4800,
+ 5440, 5120, 4480, 3840, 3520, 4480, 4800, 5120,
+ 5120, 4800, 4480, 3520, 3840, 4480, 5120, 5440,
+ 4800, 4480, 4000, 4480, 4480, 4960, 5440, 5760,
+ 4480, 4160, 4480, 4800, 5120, 5440, 5760, 6080,
+ 4160, 4480, 4800, 5120, 5440, 5760, 6080, 6400
};
- // 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};
+ // Tables used to drive a piece towards or away from another piece
+ constexpr int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
+ constexpr int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 };
- // Bitbase for KP vs K:
- uint8_t KPKBitbase[24576];
+ // Pawn Rank based scaling factors used in KRPPKRP endgame
+ constexpr int KRPPKRPScaleFactors[RANK_NB] = { 0, 9, 10, 14, 21, 44, 0, 0 };
- // 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]);
+#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
- inline Value kbnk_mate_table(Square s) {
- return Value(KBNKMateTable[s]);
- }
+ // 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) {
- inline Value distance_bonus(int d) {
- return Value(DistanceBonus[d]);
- }
+ assert(pos.count(strongSide) == 1);
- inline Value krkn_king_knight_distance_penalty(int d) {
- return Value(KRKNKingKnightDistancePenalty[d]);
- }
+ if (file_of(pos.square(strongSide)) >= FILE_E)
+ sq = Square(int(sq) ^ 7); // Mirror SQ_H1 -> SQ_A1
- // Function for probing the KP vs K bitbase:
- int probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm);
+ return strongSide == WHITE ? sq : ~sq;
+ }
-}
-
+} // namespace
-////
-//// Functions
-////
-/// Constructors
+namespace Endgames {
-EndgameEvaluationFunction::EndgameEvaluationFunction(Color c) {
- strongerSide = c;
- weakerSide = opposite_color(strongerSide);
-}
+ std::pair