-template<>
-void MovePicker::score<EVASIONS>() {
- // Try captures ordered by MVV/LVA, then non-captures ordered by stats heuristics
- const ButterflyHistory& history = pos.this_thread()->history;
- Color c = pos.side_to_move();
-
- for (auto& m : *this)
- if (pos.capture(m))
- m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
- - Value(type_of(pos.moved_piece(m))) + (1 << 28);
- else
- m.value = history[c][from_to(m)];
-}
-
-
-/// next_move() is the most important method of the MovePicker class. It returns
-/// a new pseudo legal move every time it is called, until there are no more moves
-/// left. It picks the move with the biggest value from a list of generated moves
-/// taking care not to return the ttMove if it has already been searched.
-
-Move MovePicker::next_move(bool skipQuiets) {
-
- Move move;
-
- switch (stage) {
-
- case MAIN_SEARCH: case EVASION: case QSEARCH_WITH_CHECKS:
- case QSEARCH_NO_CHECKS: case PROBCUT:
- ++stage;
- return ttMove;
-
- case CAPTURES_INIT:
- endBadCaptures = cur = moves;
- endMoves = generate<CAPTURES>(pos, cur);
- score<CAPTURES>();
- ++stage;
- /* fallthrough */
-
- case GOOD_CAPTURES:
- while (cur < endMoves)
- {
- move = pick_best(cur++, endMoves);
- if (move != ttMove)
- {
- if (pos.see_ge(move))
- return move;
-
- // Losing capture, move it to the beginning of the array
- *endBadCaptures++ = move;
- }
- }
-
- ++stage;
- move = killers[0]; // First killer move
- if ( move != MOVE_NONE
- && move != ttMove
- && pos.pseudo_legal(move)
- && !pos.capture(move))
- return move;
- /* fallthrough */
-
- case KILLERS:
- ++stage;
- move = killers[1]; // Second killer move
- if ( move != MOVE_NONE
- && move != ttMove
- && pos.pseudo_legal(move)
- && !pos.capture(move))
- return move;
- /* fallthrough */
-
- case COUNTERMOVE:
- ++stage;
- move = countermove;
- if ( move != MOVE_NONE
- && move != ttMove
- && move != killers[0]
- && move != killers[1]
- && pos.pseudo_legal(move)
- && !pos.capture(move))
- return move;
- /* fallthrough */
-
- case QUIET_INIT:
- cur = endBadCaptures;
- endMoves = generate<QUIETS>(pos, cur);
- score<QUIETS>();
- partial_insertion_sort(cur, endMoves, -4000 * depth / ONE_PLY);
- ++stage;
- /* fallthrough */
-
- case QUIET:
- while ( cur < endMoves
- && (!skipQuiets || cur->value >= VALUE_ZERO))
- {
- move = *cur++;
-
- if ( move != ttMove
- && move != killers[0]
- && move != killers[1]
- && move != countermove)
- return move;
- }
- ++stage;
- cur = moves; // Point to beginning of bad captures
- /* fallthrough */
-
- case BAD_CAPTURES:
- if (cur < endBadCaptures)
- return *cur++;
- break;
-
- case EVASIONS_INIT:
- cur = moves;
- endMoves = generate<EVASIONS>(pos, cur);
- score<EVASIONS>();
- ++stage;
- /* fallthrough */
-
- case ALL_EVASIONS:
- while (cur < endMoves)
- {
- move = pick_best(cur++, endMoves);
- if (move != ttMove)
- return move;
- }
- break;
-
- case PROBCUT_INIT:
- cur = moves;
- endMoves = generate<CAPTURES>(pos, cur);
- score<CAPTURES>();
- ++stage;
- /* fallthrough */
-
- case PROBCUT_CAPTURES:
- while (cur < endMoves)
- {
- move = pick_best(cur++, endMoves);
- if ( move != ttMove
- && pos.see_ge(move, threshold))
- return move;
- }
- break;
-
- case QCAPTURES_1_INIT: case QCAPTURES_2_INIT:
- cur = moves;
- endMoves = generate<CAPTURES>(pos, cur);
- score<CAPTURES>();
- ++stage;
- /* fallthrough */
-
- case QCAPTURES_1: case QCAPTURES_2:
- while (cur < endMoves)
- {
- move = pick_best(cur++, endMoves);
- if (move != ttMove)
- return move;
- }
- if (stage == QCAPTURES_2)
- break;
- cur = moves;
- endMoves = generate<QUIET_CHECKS>(pos, cur);
- ++stage;
- /* fallthrough */
-
- case QCHECKS:
- while (cur < endMoves)
- {
- move = cur++->move;
- if (move != ttMove)
- return move;
- }
- break;
-
- case QSEARCH_RECAPTURES:
- cur = moves;
- endMoves = generate<CAPTURES>(pos, cur);
- score<CAPTURES>();
- ++stage;
- /* fallthrough */
-
- case QRECAPTURES:
- while (cur < endMoves)
- {
- move = pick_best(cur++, endMoves);
- if (to_sq(move) == recaptureSquare)
- return move;
- }
- break;
-
- default:
- assert(false);
- }
-
- return MOVE_NONE;
-}