44#include " timeman.h"
55#include " uci.h"
66#include < atomic>
7+ #include < iostream>
78#include < moves_io.h>
89#include < position.h>
910#include < printers.h>
10- #include < iostream>
1111using namespace chess ;
1212namespace engine {
1313TranspositionTable search::tt (16 );
@@ -31,50 +31,49 @@ void update_pv(Move *pv, Move move, const Move *childPv) {
3131// Adjusts a mate or TB score from "plies to mate from the root" to
3232// "plies to mate from the current position". Standard scores are unchanged.
3333// The function is called before storing a value in the transposition table.
34- Value value_to_tt (Value v, int ply) { return is_win (v) ? v + ply : is_loss (v) ? v - ply : v; }
35-
34+ Value value_to_tt (Value v, int ply) {
35+ return is_win (v) ? v + ply : is_loss (v) ? v - ply : v;
36+ }
3637
37- // Inverse of value_to_tt(): it adjusts a mate or TB score from the transposition
38- // table (which refers to the plies to mate/be mated from current position) to
39- // "plies to mate/be mated (TB win/loss) from the root". However, to avoid
40- // potentially false mate or TB scores related to the 50 moves rule and the
41- // graph history interaction, we return the highest non-TB score instead.
38+ // Inverse of value_to_tt(): it adjusts a mate or TB score from the
39+ // transposition table (which refers to the plies to mate/be mated from current
40+ // position) to "plies to mate/be mated (TB win/loss) from the root". However,
41+ // to avoid potentially false mate or TB scores related to the 50 moves rule and
42+ // the graph history interaction, we return the highest non-TB score instead.
4243Value value_from_tt (Value v, int ply, int r50c) {
4344
44- if (!is_valid (v))
45- return VALUE_NONE ;
45+ if (!is_valid (v))
46+ return VALUE_NONE ;
4647
47- // handle TB win or better
48- if (is_win (v))
49- {
50- // Downgrade a potentially false mate score
51- if (v >= VALUE_MATE_IN_MAX_PLY && VALUE_MATE - v > 100 - r50c)
52- return VALUE_TB_WIN_IN_MAX_PLY - 1 ;
48+ // handle TB win or better
49+ if (is_win (v)) {
50+ // Downgrade a potentially false mate score
51+ if (v >= VALUE_MATE_IN_MAX_PLY && VALUE_MATE - v > 100 - r50c)
52+ return VALUE_TB_WIN_IN_MAX_PLY - 1 ;
5353
54- // Downgrade a potentially false TB score.
55- if (VALUE_TB - v > 100 - r50c)
56- return VALUE_TB_WIN_IN_MAX_PLY - 1 ;
54+ // Downgrade a potentially false TB score.
55+ if (VALUE_TB - v > 100 - r50c)
56+ return VALUE_TB_WIN_IN_MAX_PLY - 1 ;
5757
58- return v - ply;
59- }
58+ return v - ply;
59+ }
6060
61- // handle TB loss or worse
62- if (is_loss (v))
63- {
64- // Downgrade a potentially false mate score.
65- if (v <= VALUE_MATED_IN_MAX_PLY && VALUE_MATE + v > 100 - r50c)
66- return VALUE_TB_LOSS_IN_MAX_PLY + 1 ;
61+ // handle TB loss or worse
62+ if (is_loss (v)) {
63+ // Downgrade a potentially false mate score.
64+ if (v <= VALUE_MATED_IN_MAX_PLY && VALUE_MATE + v > 100 - r50c)
65+ return VALUE_TB_LOSS_IN_MAX_PLY + 1 ;
6766
68- // Downgrade a potentially false TB score.
69- if (VALUE_TB + v > 100 - r50c)
70- return VALUE_TB_LOSS_IN_MAX_PLY + 1 ;
67+ // Downgrade a potentially false TB score.
68+ if (VALUE_TB + v > 100 - r50c)
69+ return VALUE_TB_LOSS_IN_MAX_PLY + 1 ;
7170
72- return v + ply;
73- }
71+ return v + ply;
72+ }
7473
75- return v;
76- }
74+ return v;
7775}
76+ } // namespace
7877Value qsearch (Board &board, Value alpha, Value beta, Session &session,
7978 int ply = 0 ) {
8079 session.nodes ++;
@@ -124,7 +123,7 @@ Value doSearch(Board &board, int depth, Value alpha, Value beta,
124123 Move::none ());
125124 std::fill (std::begin (session.pv [ply + 1 ]), std::end (session.pv [ply + 1 ]),
126125 Move::none ());
127- if (board.is_draw (3 )|| board.is_insufficient_material ()) {
126+ if (board.is_draw (3 ) || board.is_insufficient_material ()) {
128127 session.nodes ++;
129128 session.pv [ply][0 ] = Move::none ();
130129 return 0 ;
@@ -134,7 +133,8 @@ Value doSearch(Board &board, int depth, Value alpha, Value beta,
134133 Move preferred = Move::none ();
135134 if (TTEntry *entry = search::tt.lookup (hash)) {
136135 if (entry->getDepth () >= depth) {
137- Value ttScore = value_from_tt (entry->getScore (), ply, board.rule50_count ());
136+ Value ttScore =
137+ value_from_tt (entry->getScore (), ply, board.rule50_count ());
138138 TTFlag flag = entry->getFlag ();
139139
140140 if (flag == TTFlag::EXACT ) {
@@ -167,7 +167,7 @@ Value doSearch(Board &board, int depth, Value alpha, Value beta,
167167 movepick::orderMoves (board, moves, preferred, ply);
168168 if (bool useNMP = depth >= 3 && !board.checkers () && ply > 0 ) {
169169 int R = 2 + depth / 6 ;
170- uint64_t hash_= board.hash ();
170+ uint64_t hash_ = board.hash ();
171171 board.doNullMove ();
172172 Value score =
173173 doSearch (board, depth - 1 - R, -beta, -beta + 1 , session, ply + 1 );
@@ -201,17 +201,17 @@ Value doSearch(Board &board, int depth, Value alpha, Value beta,
201201 Value score;
202202
203203 if (i == 0 ) {
204- uint64_t hash_= board.hash ();
204+ uint64_t hash_ = board.hash ();
205205 // --- First move: full window (PVS root move) ---
206206 score = doSearch (board, depth - 1 , -beta, -alpha, session, ply + 1 );
207207
208208 if (score == VALUE_NONE ) {
209209 board.undoMove ();
210210 return VALUE_NONE ;
211211 }
212- score= -score;
212+ score = -score;
213213 } else {
214- uint64_t hash_= board.hash ();
214+ uint64_t hash_ = board.hash ();
215215 // --- Null-window search (PVS + LMR) ---
216216 score = doSearch (board, depth - 1 - reduction, -alpha - 1 , -alpha,
217217 session, ply + 1 );
@@ -271,7 +271,8 @@ Value doSearch(Board &board, int depth, Value alpha, Value beta,
271271 else
272272 flag = TTFlag::EXACT ;
273273
274- search::tt.store (hash, session.pv [ply][0 ], value_to_tt (maxScore, ply), depth, flag);
274+ search::tt.store (hash, session.pv [ply][0 ], value_to_tt (maxScore, ply),
275+ depth, flag);
275276 }
276277 return maxScore;
277278}
0 commit comments