Subversion Repositories Games.Chess Giants

Rev

Rev 96 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 96 Rev 169
Line 4... Line 4...
4
  Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
4
  Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5
  Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
5
  Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
 
6
 
7
  Stockfish is free software: you can redistribute it and/or modify
7
  Stockfish is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU General Public License as published by
8
  it under the terms of the GNU General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
10
  (at your option) any later version.
Line 29... Line 29...
29
 
29
 
30
#include "position.h"
30
#include "position.h"
31
#include "types.h"
31
#include "types.h"
32
 
32
 
33
 
33
 
34
/// EndgameType lists all supported endgames
34
/// EndgameCode lists all supported endgame functions by corresponding codes
35
 
35
 
36
enum EndgameType {
36
enum EndgameCode {
37
 
-
 
38
  // Evaluation functions
-
 
39
 
37
 
-
 
38
  EVALUATION_FUNCTIONS,
40
  KNNK,  // KNN vs K
39
  KNNK,  // KNN vs K
41
  KXK,   // Generic "mate lone king" eval
40
  KXK,   // Generic "mate lone king" eval
42
  KBNK,  // KBN vs K
41
  KBNK,  // KBN vs K
43
  KPK,   // KP vs K
42
  KPK,   // KP vs K
44
  KRKP,  // KR vs KP
43
  KRKP,  // KR vs KP
45
  KRKB,  // KR vs KB
44
  KRKB,  // KR vs KB
46
  KRKN,  // KR vs KN
45
  KRKN,  // KR vs KN
47
  KQKP,  // KQ vs KP
46
  KQKP,  // KQ vs KP
48
  KQKR,  // KQ vs KR
47
  KQKR,  // KQ vs KR
49
 
48
 
50
 
-
 
51
  // Scaling functions
-
 
52
  SCALING_FUNCTIONS,
49
  SCALING_FUNCTIONS,
53
 
-
 
54
  KBPsK,   // KB and pawns vs K
50
  KBPsK,   // KB and pawns vs K
55
  KQKRPs,  // KQ vs KR and pawns
51
  KQKRPs,  // KQ vs KR and pawns
56
  KRPKR,   // KRP vs KR
52
  KRPKR,   // KRP vs KR
57
  KRPKB,   // KRP vs KB
53
  KRPKB,   // KRP vs KB
58
  KRPPKRP, // KRPP vs KRP
54
  KRPPKRP, // KRPP vs KRP
Line 66... Line 62...
66
};
62
};
67
 
63
 
68
 
64
 
69
/// Endgame functions can be of two types depending on whether they return a
65
/// Endgame functions can be of two types depending on whether they return a
70
/// Value or a ScaleFactor.
66
/// Value or a ScaleFactor.
71
template<EndgameType E> using
67
template<EndgameCode E> using
72
eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
68
eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
73
 
69
 
74
 
70
 
75
/// Base and derived templates for endgame evaluation and scaling functions
71
/// Base and derived functors for endgame evaluation and scaling functions
76
 
72
 
77
template<typename T>
73
template<typename T>
78
struct EndgameBase {
74
struct EndgameBase {
79
 
75
 
-
 
76
  explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {}
80
  virtual ~EndgameBase() = default;
77
  virtual ~EndgameBase() = default;
81
  virtual Color strong_side() const = 0;
-
 
82
  virtual T operator()(const Position&) const = 0;
78
  virtual T operator()(const Position&) const = 0;
-
 
79
 
-
 
80
  const Color strongSide, weakSide;
83
};
81
};
84
 
82
 
85
 
83
 
86
template<EndgameType E, typename T = eg_type<E>>
84
template<EndgameCode E, typename T = eg_type<E>>
87
struct Endgame : public EndgameBase<T> {
85
struct Endgame : public EndgameBase<T> {
88
 
86
 
89
  explicit Endgame(Color c) : strongSide(c), weakSide(~c) {}
87
  explicit Endgame(Color c) : EndgameBase<T>(c) {}
90
  Color strong_side() const { return strongSide; }
-
 
91
  T operator()(const Position&) const;
88
  T operator()(const Position&) const override;
92
 
-
 
93
private:
-
 
94
  Color strongSide, weakSide;
-
 
95
};
89
};
96
 
90
 
97
 
91
 
98
/// The Endgames class stores the pointers to endgame evaluation and scaling
92
/// The Endgames class stores the pointers to endgame evaluation and scaling
99
/// base objects in two std::map. We use polymorphism to invoke the actual
93
/// base objects in two std::map. We use polymorphism to invoke the actual
100
/// endgame function by calling its virtual operator().
94
/// endgame function by calling its virtual operator().
101
 
95
 
102
class Endgames {
96
class Endgames {
103
 
97
 
104
  template<typename T> using Map = std::map<Key, std::unique_ptr<EndgameBase<T>>>;
98
  template<typename T> using Ptr = std::unique_ptr<EndgameBase<T>>;
105
 
-
 
106
  template<EndgameType E, typename T = eg_type<E>>
99
  template<typename T> using Map = std::map<Key, Ptr<T>>;
107
  void add(const std::string& code);
-
 
108
 
100
 
109
  template<typename T>
101
  template<typename T>
110
  Map<T>& map() {
102
  Map<T>& map() {
111
    return std::get<std::is_same<T, ScaleFactor>::value>(maps);
103
    return std::get<std::is_same<T, ScaleFactor>::value>(maps);
-
 
104
  }
-
 
105
 
-
 
106
  template<EndgameCode E, typename T = eg_type<E>, typename P = Ptr<T>>
-
 
107
  void add(const std::string& code) {
-
 
108
 
-
 
109
    StateInfo st;
-
 
110
    map<T>()[Position().set(code, WHITE, &st).material_key()] = P(new Endgame<E>(WHITE));
-
 
111
    map<T>()[Position().set(code, BLACK, &st).material_key()] = P(new Endgame<E>(BLACK));
112
  }
112
  }
113
 
113
 
114
  std::pair<Map<Value>, Map<ScaleFactor>> maps;
114
  std::pair<Map<Value>, Map<ScaleFactor>> maps;
115
 
115
 
116
public:
116
public: