-
-/// MovePicker::go_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::go_next_phase() {
-
- curMove = moves;
- phase = *(++phasePtr);
- switch (phase) {
-
- case PH_TT_MOVE:
- lastMove = curMove + 1;
- return;
-
- case PH_GOOD_CAPTURES:
- case PH_GOOD_PROBCUT:
- lastMove = generate<MV_CAPTURE>(pos, moves);
- score_captures();
- return;
-
- case PH_KILLERS:
- curMove = killers;
- lastMove = curMove + 2;
- return;
-
- case PH_NONCAPTURES_1:
- lastNonCapture = lastMove = generate<MV_NON_CAPTURE>(pos, moves);
- score_noncaptures();
- sort_moves(moves, lastNonCapture, &lastMove);
- return;
-
- case PH_NONCAPTURES_2:
- curMove = lastMove;
- lastMove = lastNonCapture;
- if (depth >= 3 * ONE_PLY)
- insertion_sort<MoveStack>(curMove, lastMove);
- return;
-
- case PH_BAD_CAPTURES:
- // 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 PH_EVASIONS:
- assert(pos.in_check());
- lastMove = generate<MV_EVASION>(pos, moves);
- score_evasions();
- return;
-
- case PH_QCAPTURES:
- lastMove = generate<MV_CAPTURE>(pos, moves);
- score_captures();
- return;
-
- case PH_QRECAPTURES:
- lastMove = generate<MV_CAPTURE>(pos, moves);
- return;
-
- case PH_QCHECKS:
- lastMove = generate<MV_NON_CAPTURE_CHECK>(pos, moves);
- return;
-
- case PH_STOP:
- lastMove = curMove + 1; // Avoid another go_next_phase() call
- return;
-
- default:
- assert(false);
- return;
- }
+/// MovePicker::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 moves 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)))] / 8;
+
+ else if (Type == QUIETS)
+ m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
+ + (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
+ + (*continuationHistory[1])[pos.moved_piece(m)][to_sq(m)]
+ + (*continuationHistory[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)]
+ + (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
+ - (1 << 28);
+ }