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-2009 Marco Costalba
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/>.
37 //// Local definitions
43 const MovegenPhaseT MainSearchPhaseTable[] = { PH_TT_MOVES, PH_GOOD_CAPTURES, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
44 const MovegenPhaseT EvasionsPhaseTable[] = { PH_EVASIONS, PH_STOP};
45 const MovegenPhaseT QsearchWithChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_QCHECKS, PH_STOP};
46 const MovegenPhaseT QsearchWithoutChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_STOP};
55 /// Constructor for the MovePicker class. Apart from the position for which
56 /// it is asked to pick legal moves, MovePicker also wants some information
57 /// to help it to return the presumably good moves first, to decide which
58 /// moves to return (in the quiescence search, for instance, we only want to
59 /// search captures, promotions and some checks) and about how important good
60 /// move ordering is at the current node.
62 MovePicker::MovePicker(const Position& p, Move ttm, Depth d,
63 const History& h, SearchStack* ss) : pos(p), H(h) {
64 ttMoves[0].move = ttm;
67 ttMoves[1].move = (ss->mateKiller == ttm)? MOVE_NONE : ss->mateKiller;
68 killers[0].move = ss->killers[0];
69 killers[1].move = ss->killers[1];
71 ttMoves[1].move = killers[0].move = killers[1].move = MOVE_NONE;
76 Color us = pos.side_to_move();
78 dc = p.discovered_check_candidates(us);
79 pinned = p.pinned_pieces(us);
82 phasePtr = EvasionsPhaseTable;
83 else if (d > Depth(0))
84 phasePtr = MainSearchPhaseTable;
85 else if (d == Depth(0))
86 phasePtr = QsearchWithChecksPhaseTable;
88 phasePtr = QsearchWithoutChecksPhaseTable;
95 /// MovePicker::go_next_phase() generates, scores and sorts the next bunch
96 /// of moves when there are no more moves to try for the current phase.
98 void MovePicker::go_next_phase() {
101 phase = *(++phasePtr);
106 lastMove = curMove + 2;
109 case PH_GOOD_CAPTURES:
110 lastMove = generate_captures(pos, moves);
112 std::sort(moves, lastMove);
117 lastMove = curMove + 2;
121 lastMove = generate_noncaptures(pos, moves);
123 std::sort(moves, lastMove);
126 case PH_BAD_CAPTURES:
127 // Bad captures SEE value is already calculated so just sort them
128 // to get SEE move ordering.
129 curMove = badCaptures;
130 lastMove = badCaptures + numOfBadCaptures;
131 std::sort(badCaptures, lastMove);
135 assert(pos.is_check());
136 lastMove = generate_evasions(pos, moves, pinned);
138 std::sort(moves, lastMove);
142 lastMove = generate_captures(pos, moves);
144 std::sort(moves, lastMove);
148 // Perhaps we should order moves move here? FIXME
149 lastMove = generate_non_capture_checks(pos, moves, dc);
153 lastMove = curMove + 1; // hack to be friendly for get_next_move()
163 /// MovePicker::score_captures(), MovePicker::score_noncaptures() and
164 /// MovePicker::score_evasions() assign a numerical move ordering score
165 /// to each move in a move list. The moves with highest scores will be
166 /// picked first by get_next_move().
168 void MovePicker::score_captures() {
169 // Winning and equal captures in the main search are ordered by MVV/LVA.
170 // Suprisingly, this appears to perform slightly better than SEE based
171 // move ordering. The reason is probably that in a position with a winning
172 // capture, capturing a more valuable (but sufficiently defended) piece
173 // first usually doesn't hurt. The opponent will have to recapture, and
174 // the hanging piece will still be hanging (except in the unusual cases
175 // where it is possible to recapture with the hanging piece). Exchanging
176 // big pieces before capturing a hanging piece probably helps to reduce
178 // In main search we want to push captures with negative SEE values to
179 // badCaptures[] array, but instead of doing it now we delay till when
180 // the move has been picked up in pick_move_from_list(), this way we save
181 // some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
184 // Use MVV/LVA ordering
185 for (MoveStack* cur = moves; cur != lastMove; cur++)
188 if (move_is_promotion(m))
189 cur->score = QueenValueMidgame;
191 cur->score = int(pos.midgame_value_of_piece_on(move_to(m)))
192 -int(pos.type_of_piece_on(move_from(m)));
196 void MovePicker::score_noncaptures() {
197 // First score by history, when no history is available then use
198 // piece/square tables values. This seems to be better then a
199 // random choice when we don't have an history for any move.
205 for (MoveStack* cur = moves; cur != lastMove; cur++)
210 piece = pos.piece_on(from);
211 hs = H.move_ordering_score(piece, to);
213 // Ensure history is always preferred to pst
218 cur->score = hs + pos.pst_delta<Position::MidGame>(piece, from, to);
222 void MovePicker::score_evasions() {
226 for (MoveStack* cur = moves; cur != lastMove; cur++)
229 if (m == ttMoves[0].move)
230 cur->score = 2 * HistoryMax;
231 else if (!pos.square_is_empty(move_to(m)))
233 int seeScore = pos.see(m);
234 cur->score = seeScore + (seeScore >= 0 ? HistoryMax : 0);
236 cur->score = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
240 /// MovePicker::get_next_move() is the most important method of the MovePicker
241 /// class. It returns a new legal move every time it is called, until there
242 /// are no more moves left.
243 /// It picks the move with the biggest score from a list of generated moves taking
244 /// care not to return the tt move if that has already been serched previously.
246 Move MovePicker::get_next_move() {
248 assert(!pos.is_check() || *phasePtr == PH_EVASIONS || *phasePtr == PH_STOP);
249 assert( pos.is_check() || *phasePtr != PH_EVASIONS);
255 while (curMove != lastMove)
257 move = (curMove++)->move;
262 if ( move != MOVE_NONE
263 && move_is_legal(pos, move, pinned))
267 case PH_GOOD_CAPTURES:
268 if ( move != ttMoves[0].move
269 && move != ttMoves[1].move
270 && pos.pl_move_is_legal(move, pinned))
272 // Check for a non negative SEE now
273 int seeValue = pos.see_sign(move);
277 // Losing capture, move it to the badCaptures[] array, note
278 // that move has now been already checked for legality.
279 assert(numOfBadCaptures < 63);
280 badCaptures[numOfBadCaptures].move = move;
281 badCaptures[numOfBadCaptures++].score = seeValue;
286 if ( move != MOVE_NONE
287 && move != ttMoves[0].move
288 && move != ttMoves[1].move
289 && move_is_legal(pos, move, pinned)
290 && !pos.move_is_capture(move))
295 if ( move != ttMoves[0].move
296 && move != ttMoves[1].move
297 && move != killers[0].move
298 && move != killers[1].move
299 && pos.pl_move_is_legal(move, pinned))
304 case PH_BAD_CAPTURES:
309 // Maybe postpone the legality check until after futility pruning?
310 if ( move != ttMoves[0].move
311 && pos.pl_move_is_legal(move, pinned))
327 /// A variant of get_next_move() which takes a lock as a parameter, used to
328 /// prevent multiple threads from picking the same move at a split point.
330 Move MovePicker::get_next_move(Lock &lock) {
338 Move m = get_next_move();