-
-/// score() assign a numerical move ordering score to each move in a move list.
-/// The moves with highest scores will be picked first.
-template<>
-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* it = moves; it != end; ++it)
- {
- m = it->move;
- it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
- - type_of(pos.piece_moved(m));
-
- if (type_of(m) == PROMOTION)
- it->score += PieceValue[MG][promotion_type(m)] - PieceValue[MG][PAWN];
-
- else if (type_of(m) == ENPASSANT)
- it->score += PieceValue[MG][PAWN];
- }
-}
-
-template<>
-void MovePicker::score<QUIETS>() {
-
- Move m;
-
- for (MoveStack* it = moves; it != end; ++it)
- {
- m = it->move;
- it->score = Hist[pos.piece_moved(m)][to_sq(m)];
- }
-}
-
-template<>
-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;
-
- for (MoveStack* it = moves; it != end; ++it)
- {
- m = it->move;
- if ((seeScore = pos.see_sign(m)) < 0)
- it->score = seeScore - History::Max; // At the bottom
-
- else if (pos.is_capture(m))
- it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
- - type_of(pos.piece_moved(m)) + History::Max;
- else
- it->score = Hist[pos.piece_moved(m)][to_sq(m)];
- }
+/// 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))]
+ + (*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);
+ }