-
Notifications
You must be signed in to change notification settings - Fork 2
/
root.c
186 lines (185 loc) · 8.05 KB
/
root.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "chess.h"
#include "data.h"
#include "epdglue.h"
/* last modified 04/21/15 */
/*
*******************************************************************************
* *
* RootMoveList() is used to set up the ply one move list. It is a more *
* accurate ordering of the move list than that done for plies deeper than *
* one. Briefly, Quiesce() is used to obtain the positional score plus the *
* expected gain/loss for pieces that can be captured. *
* *
*******************************************************************************
*/
void RootMoveList(int wtm) {
TREE *const tree = block[0];
unsigned mvp, *lastm, rmoves[256];
int temp, value;
#if !defined(NOEGTB)
int tb_value;
#endif
/*
************************************************************
* *
* If the position at the root is a draw, based on EGTB *
* results, we are going to behave differently. We will *
* extract the root moves that are draws, and toss the *
* losers out. Then, we will do a normal search on the *
* moves that draw to try and chose the drawing move that *
* gives our opponent the best chance to make an error. *
* This search will be done sans EGTB probes since we al- *
* ready know this is a draw at the root. We simply find *
* the best move (based on search/eval) that preserves the *
* draw. *
* *
************************************************************
*/
#if !defined(NOEGTB)
EGTB_draw = 0;
if (EGTBlimit && TotalAllPieces <= EGTBlimit &&
Castle(1, white) + Castle(1, black) == 0 &&
EGTBProbe(tree, 1, wtm, &tb_value)) {
if (swindle_mode && (tb_value == DrawScore(wtm)))
if ((wtm && Material > 0) || (!wtm && Material < 0))
EGTB_draw = 1;
}
#endif
/*
************************************************************
* *
* First, use GenerateMoves() to generate the set of legal *
* moves from the root position. *
* *
************************************************************
*/
lastm = GenerateCaptures(tree, 1, wtm, rmoves);
lastm = GenerateNoncaptures(tree, 1, wtm, lastm);
n_root_moves = lastm - rmoves;
for (mvp = 0; mvp < n_root_moves; mvp++)
root_moves[mvp].move = rmoves[mvp];
/*
************************************************************
* *
* Now make each move and use Quiesce() to analyze the *
* potential captures and return a minimax score. *
* *
* Special case: if this is an egtb draw at the root, *
* then this is where we cull the non-drawing moves by *
* doing an EGTB probe for each move. Any moves that lose *
* are left with a very bad sorting score to move them to *
* the end of the root move list. *
* *
************************************************************
*/
abort_search = 0;
for (mvp = 0; mvp < n_root_moves; mvp++) {
value = -4000000;
#if defined(TRACE)
if (trace_level >= 1) {
tree->curmv[1] = root_moves[mvp].move;
Trace(tree, 1, 0, wtm, -MATE, MATE, "RootMoves()", serial, HASH,
mvp + 1);
}
#endif
MakeMove(tree, 1, wtm, root_moves[mvp].move);
tree->nodes_searched++;
if (!Check(wtm))
do {
tree->curmv[1] = root_moves[mvp].move;
#if !defined(NOEGTB)
if (TotalAllPieces <= EGTBlimit && EGTB_draw &&
Castle(1, white) + Castle(1, black) == 0) {
temp = EGTBProbe(tree, 2, Flip(wtm), &tb_value);
if (temp && tb_value != DrawScore(Flip(wtm)))
break;
}
#endif
value = -Quiesce(tree, 2, Flip(wtm), -MATE, MATE, 0);
/*
************************************************************
* *
* Add in a bonus if this move is part of the previous *
* principal variation. It was good in the search, we *
* should try it first now. *
* *
************************************************************
*/
if ((Piece(root_moves[mvp].move) == Piece(last_pv.path[1]))
&& (From(root_moves[mvp].move) == From(last_pv.path[1]))
&& (To(root_moves[mvp].move) == To(last_pv.path[1]))
&& (Captured(root_moves[mvp].move) == Captured(last_pv.path[1]))
&& (Promote(root_moves[mvp].move) == Promote(last_pv.path[1])))
value += 2000000;
/*
************************************************************
* *
* Fudge the score for promotions so that promotion to a *
* queen is tried first. *
* *
************************************************************
*/
if (Promote(root_moves[mvp].move) &&
(Promote(root_moves[mvp].move) != queen))
value -= 50;
} while (0);
root_moves[mvp].path = tree->pv[1];
root_moves[mvp].path.pathv = value;
root_moves[mvp].status = 0;
root_moves[mvp].bm_age = 0;
UnmakeMove(tree, 1, wtm, root_moves[mvp].move);
}
/*
************************************************************
* *
* Sort the moves into order based on the scores returned *
* by Quiesce() which includes evaluation + captures. *
* *
************************************************************
*/
SortRootMoves();
/*
************************************************************
* *
* Trim the move list to eliminate those moves that hang *
* the king and are illegal. This also culls any non- *
* drawing moves when we are in the swindle-mode situation *
* and want to do a normal search but only on moves that *
* preserve the draw. *
* *
************************************************************
*/
for (; n_root_moves; n_root_moves--)
if (root_moves[n_root_moves - 1].path.pathv > -3000000)
break;
if (root_moves[0].path.pathv > 1000000)
root_moves[0].path.pathv -= 2000000;
/*
************************************************************
* *
* Debugging output to dump root move list and the stuff *
* used to sort them, for testing and debugging. *
* *
************************************************************
*/
if (display_options & 128) {
Print(128, "%d moves at root\n", n_root_moves);
Print(128, " score move/pv\n");
for (mvp = 0; mvp < n_root_moves; mvp++)
Print(128, "%10s %s\n", DisplayEvaluation(root_moves[mvp].path.pathv,
wtm), DisplayPath(tree, wtm, &root_moves[mvp].path));
}
/*
************************************************************
* *
* Finally, before we return the list of moves, we need to *
* set the values to an impossible negative value so that *
* as we sort the root move list after fail highs and lows *
* the un-searched moves won't pop to the top of the list. *
* *
************************************************************
*/
for (mvp = 1; mvp < n_root_moves; mvp++)
root_moves[mvp].path.pathv = -99999999;
return;
}