2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
36 //// Local definitions
42 PH_TT_MOVES, // Transposition table move and mate killer
43 PH_GOOD_CAPTURES, // Queen promotions and captures with SEE values >= 0
44 PH_KILLERS, // Killer moves from the current ply
45 PH_NONCAPTURES, // Non-captures and underpromotions
46 PH_BAD_CAPTURES, // Queen promotions and captures with SEE values < 0
47 PH_EVASIONS, // Check evasions
48 PH_QCAPTURES, // Captures in quiescence search
49 PH_QCHECKS, // Non-capture checks in quiescence search
54 const uint8_t MainSearchPhaseTable[] = { PH_TT_MOVES, PH_GOOD_CAPTURES, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
55 const uint8_t EvasionsPhaseTable[] = { PH_TT_MOVES, PH_EVASIONS, PH_STOP};
56 const uint8_t QsearchWithChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_QCHECKS, PH_STOP};
57 const uint8_t QsearchWithoutChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_STOP};
66 /// Constructor for the MovePicker class. Apart from the position for which
67 /// it is asked to pick legal moves, MovePicker also wants some information
68 /// to help it to return the presumably good moves first, to decide which
69 /// moves to return (in the quiescence search, for instance, we only want to
70 /// search captures, promotions and some checks) and about how important good
71 /// move ordering is at the current node.
73 MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
74 SearchStack* ss, Value beta) : pos(p), H(h) {
76 ttMoves[0].move = ttm;
77 badCaptureThreshold = 0;
78 badCaptures = moves + MOVES_MAX;
80 pinned = p.pinned_pieces(pos.side_to_move());
82 if (ss && !p.is_check())
84 ttMoves[1].move = (ss->mateKiller == ttm) ? MOVE_NONE : ss->mateKiller;
85 searchTT |= ttMoves[1].move;
86 killers[0].move = ss->killers[0];
87 killers[1].move = ss->killers[1];
89 ttMoves[1].move = killers[0].move = killers[1].move = MOVE_NONE;
92 phasePtr = EvasionsPhaseTable;
93 else if (d > DEPTH_ZERO)
95 // Consider sligtly negative captures as good if at low
96 // depth and far from beta.
97 if (ss && ss->eval < beta - PawnValueMidgame && d < 3 * ONE_PLY)
98 badCaptureThreshold = -PawnValueMidgame;
100 phasePtr = MainSearchPhaseTable;
102 else if (d >= DEPTH_QS_CHECKS)
103 phasePtr = QsearchWithChecksPhaseTable;
106 phasePtr = QsearchWithoutChecksPhaseTable;
108 // Skip TT move if is not a capture or a promotion, this avoids
109 // qsearch tree explosion due to a possible perpetual check or
110 // similar rare cases when TT table is full.
111 if (ttm != MOVE_NONE && !pos.move_is_capture_or_promotion(ttm))
112 searchTT = ttMoves[0].move = MOVE_NONE;
115 phasePtr += int(!searchTT) - 1;
120 /// MovePicker::go_next_phase() generates, scores and sorts the next bunch
121 /// of moves when there are no more moves to try for the current phase.
123 void MovePicker::go_next_phase() {
126 phase = *(++phasePtr);
131 lastMove = curMove + 2;
134 case PH_GOOD_CAPTURES:
135 lastMove = generate<MV_CAPTURE>(pos, moves);
141 lastMove = curMove + 2;
145 lastMove = generate<MV_NON_CAPTURE>(pos, moves);
147 sort_moves(moves, lastMove, &lastGoodNonCapture);
150 case PH_BAD_CAPTURES:
151 // Bad captures SEE value is already calculated so just pick
152 // them in order to get SEE move ordering.
153 curMove = badCaptures;
154 lastMove = moves + MOVES_MAX;
158 assert(pos.is_check());
159 lastMove = generate<MV_EVASION>(pos, moves);
164 lastMove = generate<MV_CAPTURE>(pos, moves);
169 lastMove = generate<MV_NON_CAPTURE_CHECK>(pos, moves);
173 lastMove = curMove + 1; // Avoids another go_next_phase() call
183 /// MovePicker::score_captures(), MovePicker::score_noncaptures() and
184 /// MovePicker::score_evasions() assign a numerical move ordering score
185 /// to each move in a move list. The moves with highest scores will be
186 /// picked first by get_next_move().
188 void MovePicker::score_captures() {
189 // Winning and equal captures in the main search are ordered by MVV/LVA.
190 // Suprisingly, this appears to perform slightly better than SEE based
191 // move ordering. The reason is probably that in a position with a winning
192 // capture, capturing a more valuable (but sufficiently defended) piece
193 // first usually doesn't hurt. The opponent will have to recapture, and
194 // the hanging piece will still be hanging (except in the unusual cases
195 // where it is possible to recapture with the hanging piece). Exchanging
196 // big pieces before capturing a hanging piece probably helps to reduce
198 // In main search we want to push captures with negative SEE values to
199 // badCaptures[] array, but instead of doing it now we delay till when
200 // the move has been picked up in pick_move_from_list(), this way we save
201 // some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
204 // Use MVV/LVA ordering
205 for (MoveStack* cur = moves; cur != lastMove; cur++)
208 if (move_is_promotion(m))
209 cur->score = QueenValueMidgame;
211 cur->score = pos.midgame_value_of_piece_on(move_to(m))
212 - pos.type_of_piece_on(move_from(m));
216 void MovePicker::score_noncaptures() {
217 // First score by history, when no history is available then use
218 // piece/square tables values. This seems to be better then a
219 // random choice when we don't have an history for any move.
224 for (MoveStack* cur = moves; cur != lastMove; cur++)
229 piece = pos.piece_on(from);
230 cur->score = H.value(piece, to) + H.gain(piece, to);
234 void MovePicker::score_evasions() {
235 // Try good captures ordered by MVV/LVA, then non-captures if
236 // destination square is not under attack, ordered by history
237 // value, and at the end bad-captures and non-captures with a
238 // negative SEE. This last group is ordered by the SEE score.
242 // Skip if we don't have at least two moves to order
243 if (lastMove < moves + 2)
246 for (MoveStack* cur = moves; cur != lastMove; cur++)
249 if ((seeScore = pos.see_sign(m)) < 0)
250 cur->score = seeScore - (1 << 29); // Be sure are at the bottom
251 else if (pos.move_is_capture(m))
252 cur->score = pos.midgame_value_of_piece_on(move_to(m))
253 - pos.type_of_piece_on(move_from(m)) + (1 << 29);
255 cur->score = H.value(pos.piece_on(move_from(m)), move_to(m));
259 /// MovePicker::get_next_move() is the most important method of the MovePicker
260 /// class. It returns a new legal move every time it is called, until there
261 /// are no more moves left.
262 /// It picks the move with the biggest score from a list of generated moves taking
263 /// care not to return the tt move if has already been searched previously.
264 /// Note that this function is not thread safe so should be lock protected by
265 /// caller when accessed through a shared MovePicker object.
267 Move MovePicker::get_next_move() {
273 while (curMove != lastMove)
278 move = (curMove++)->move;
279 if ( move != MOVE_NONE
280 && move_is_legal(pos, move, pinned))
284 case PH_GOOD_CAPTURES:
285 move = pick_best(curMove++, lastMove).move;
286 if ( move != ttMoves[0].move
287 && move != ttMoves[1].move
288 && pos.pl_move_is_legal(move, pinned))
290 // Check for a non negative SEE now
291 int seeValue = pos.see_sign(move);
292 if (seeValue >= badCaptureThreshold)
295 // Losing capture, move it to the tail of the array, note
296 // that move has now been already checked for legality.
297 (--badCaptures)->move = move;
298 badCaptures->score = seeValue;
303 move = (curMove++)->move;
304 if ( move != MOVE_NONE
305 && move_is_legal(pos, move, pinned)
306 && move != ttMoves[0].move
307 && move != ttMoves[1].move
308 && !pos.move_is_capture(move))
314 // Sort negative scored moves only when we get there
315 if (curMove == lastGoodNonCapture)
316 insertion_sort<MoveStack>(lastGoodNonCapture, lastMove);
318 move = (curMove++)->move;
319 if ( move != ttMoves[0].move
320 && move != ttMoves[1].move
321 && move != killers[0].move
322 && move != killers[1].move
323 && pos.pl_move_is_legal(move, pinned))
327 case PH_BAD_CAPTURES:
328 move = pick_best(curMove++, lastMove).move;
333 move = pick_best(curMove++, lastMove).move;
334 if ( move != ttMoves[0].move
335 && pos.pl_move_is_legal(move, pinned))
340 move = (curMove++)->move;
341 if ( move != ttMoves[0].move
342 && pos.pl_move_is_legal(move, pinned))