-
-/// MovePicker::score_captures(), MovePicker::score_noncaptures() and
-/// MovePicker::score_evasions() assign a numerical move ordering score
-/// to each move in a move list. The moves with highest scores will be
-/// picked first by next_move().
-
-void MovePicker::score_captures() {
- // Winning and equal captures in the main search are ordered by MVV/LVA.
- // Suprisingly, this appears to perform slightly better than SEE based
- // move ordering. The reason is probably that in a position with a winning
- // capture, capturing a more valuable (but sufficiently defended) piece
- // first usually doesn't hurt. The opponent will have to recapture, and
- // the hanging piece will still be hanging (except in the unusual cases
- // where it is possible to recapture with the hanging piece). Exchanging
- // big pieces before capturing a hanging piece probably helps to reduce
- // the subtree size.
- // In main search we want to push captures with negative SEE values to
- // badCaptures[] array, but instead of doing it now we delay till when
- // the move has been picked up in pick_move_from_list(), this way we save
- // some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
- Move m;
-
- for (MoveStack* cur = moves; cur != lastMove; cur++)
- {
- m = cur->move;
- cur->score = PieceValueMidgame[pos.piece_on(to_sq(m))]
- - type_of(pos.piece_moved(m));
-
- if (is_promotion(m))
- cur->score += PieceValueMidgame[Piece(promotion_piece_type(m))];
- }
-}
-
-void MovePicker::score_noncaptures() {
-
- Move m;
- Square from;
-
- for (MoveStack* cur = moves; cur != lastMove; cur++)
- {
- m = cur->move;
- from = from_sq(m);
- cur->score = H.value(pos.piece_on(from), to_sq(m));
- }
-}
-
-void MovePicker::score_evasions() {
- // Try good captures ordered by MVV/LVA, then non-captures if destination square
- // is not under attack, ordered by history value, then bad-captures and quiet
- // moves with a negative SEE. This last group is ordered by the SEE score.
- Move m;
- int seeScore;
-
- if (lastMove < moves + 2)
- return;
-
- for (MoveStack* cur = moves; cur != lastMove; cur++)
- {
- m = cur->move;
- if ((seeScore = pos.see_sign(m)) < 0)
- cur->score = seeScore - History::MaxValue; // Be sure we are at the bottom
- else if (pos.is_capture(m))
- cur->score = PieceValueMidgame[pos.piece_on(to_sq(m))]
- - type_of(pos.piece_moved(m)) + History::MaxValue;
- else
- cur->score = H.value(pos.piece_moved(m), to_sq(m));
- }
-}
-
-
-/// MovePicker::next_phase() generates, scores and sorts the next bunch of moves,
-/// when there are no more moves to try for the current phase.
-
-void MovePicker::next_phase() {
-
- curMove = moves;
-
- switch (++phase) {
-
- case TT_MOVE_S1: case TT_MOVE_S2: case TT_MOVE_S3: case TT_MOVE_S4: case TT_MOVE_S5:
- lastMove = curMove + 1;
- return;
-
- case GOOD_CAPTURES_S1:
- case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5:
- case RECAPTURES_S6:
- lastMove = generate<MV_CAPTURE>(pos, moves);
- score_captures();
- return;
-
- case KILLERS_S1:
- curMove = killers;
- lastMove = curMove + 2;
- return;
-
- case NONCAPTURES_1_S1:
- lastNonCapture = lastMove = generate<MV_NON_CAPTURE>(pos, moves);
- score_noncaptures();
- lastMove = std::partition(curMove, lastMove, has_positive_score);
- sort<MoveStack>(curMove, lastMove);
- return;
-
- case NONCAPTURES_2_S1:
- curMove = lastMove;
- lastMove = lastNonCapture;
- if (depth >= 3 * ONE_PLY)
- sort<MoveStack>(curMove, lastMove);
- return;
-
- case BAD_CAPTURES_S1:
- // Bad captures SEE value is already calculated so just pick them in order
- // to get SEE move ordering.
- curMove = badCaptures;
- lastMove = moves + MAX_MOVES;
- return;
-
- case EVASIONS_S2:
- assert(pos.in_check());
- lastMove = generate<MV_EVASION>(pos, moves);
- score_evasions();
- return;
-
- case CHECKS_S3:
- lastMove = generate<MV_NON_CAPTURE_CHECK>(pos, moves);
- return;
-
- case STOP_S1: case STOP_S2: case STOP_S3: case STOP_S4: case STOP_S5: case STOP_S6:
- lastMove = curMove + 1; // Avoid another next_phase() call
- return;
-
- default:
- assert(false);
- }
+/// score() assigns a numerical value to each move in a list, used for sorting.
+/// Captures are ordered by Most Valuable Victim (MVV), preferring captures
+/// with a good history. Quiets are ordered using the histories.
+template<GenType Type>
+void MovePicker::score() {
+
+ static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
+
+ for (auto& m : *this)
+ if (Type == CAPTURES)
+ m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
+ + Value((*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))]);
+
+ else if (Type == QUIETS)
+ m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
+ + (*contHistory[0])[pos.moved_piece(m)][to_sq(m)]
+ + (*contHistory[1])[pos.moved_piece(m)][to_sq(m)]
+ + (*contHistory[3])[pos.moved_piece(m)][to_sq(m)];
+
+ else // Type == EVASIONS
+ {
+ if (pos.capture(m))
+ m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
+ - Value(type_of(pos.moved_piece(m)));
+ else
+ m.value = (*mainHistory)[pos.side_to_move()][from_to(m)] - (1 << 28);
+ }