-/// MovePicker::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 score from a list
-/// of generated moves taking care not to return the tt move if has already been
-/// searched previously. Note that this function is not thread safe so should be
-/// lock protected by caller when accessed through a shared MovePicker object.
+/// MovePicker::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, picking the move with the highest score from a list of generated moves.
+Move MovePicker::next_move(bool skipQuiets) {
+
+top:
+ switch (stage) {
+
+ case MAIN_TT:
+ case EVASION_TT:
+ case QSEARCH_TT:
+ case PROBCUT_TT:
+ ++stage;
+ return ttMove;
+
+ case CAPTURE_INIT:
+ case PROBCUT_INIT:
+ case QCAPTURE_INIT:
+ cur = endBadCaptures = moves;
+ endMoves = generate<CAPTURES>(pos, cur);
+
+ score<CAPTURES>();
+ ++stage;
+ goto top;
+
+ case GOOD_CAPTURE:
+ if (select<Best>([&](){
+ return pos.see_ge(*cur, Value(-55 * cur->value / 1024)) ?
+ // Move losing capture to endBadCaptures to be tried later
+ true : (*endBadCaptures++ = *cur, false); }))
+ return *(cur - 1);
+
+ // Prepare the pointers to loop over the refutations array
+ cur = std::begin(refutations);
+ endMoves = std::end(refutations);
+
+ // If the countermove is the same as a killer, skip it
+ if ( refutations[0].move == refutations[2].move
+ || refutations[1].move == refutations[2].move)
+ --endMoves;
+
+ ++stage;
+ /* fallthrough */
+
+ case REFUTATION:
+ if (select<Next>([&](){ return *cur != MOVE_NONE
+ && !pos.capture(*cur)
+ && pos.pseudo_legal(*cur); }))
+ return *(cur - 1);
+ ++stage;
+ /* 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:
+ if ( !skipQuiets
+ && select<Next>([&](){return *cur != refutations[0].move
+ && *cur != refutations[1].move
+ && *cur != refutations[2].move;}))
+ return *(cur - 1);
+
+ // Prepare the pointers to loop over the bad captures
+ cur = moves;
+ endMoves = endBadCaptures;
+
+ ++stage;
+ /* fallthrough */
+
+ case BAD_CAPTURE:
+ return select<Next>([](){ return true; });
+
+ case EVASION_INIT:
+ cur = moves;
+ endMoves = generate<EVASIONS>(pos, cur);
+
+ score<EVASIONS>();
+ ++stage;
+ /* fallthrough */
+
+ case EVASION:
+ return select<Best>([](){ return true; });
+
+ case PROBCUT:
+ return select<Best>([&](){ return pos.see_ge(*cur, threshold); });
+
+ case QCAPTURE:
+ if (select<Best>([&](){ return depth > DEPTH_QS_RECAPTURES
+ || to_sq(*cur) == recaptureSquare; }))
+ return *(cur - 1);
+
+ // If we did not find any move and we do not try checks, we have finished
+ if (depth != DEPTH_QS_CHECKS)
+ return MOVE_NONE;