-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:
- endBadCaptures = cur = moves;
- endMoves = generate<CAPTURES>(pos, cur);
- score<CAPTURES>();
- ++stage;
- goto top;
-
- case GOOD_CAPTURE:
- if (select_move<BEST_SCORE>([&](){ return pos.see_ge(move, Value(-55 * (cur-1)->value / 1024)) ?
- // Move losing capture to endBadCaptures to be tried later
- true : (*endBadCaptures++ = move, false); }))
- return move;
-
- // If the countermove is the same as a killer, skip it
- if ( refutations[0] == refutations[2]
- || refutations[1] == refutations[2])
- refutations[2] = MOVE_NONE;
- ++stage;
- /* fallthrough */
-
- case KILLER0:
- case KILLER1:
- case COUNTERMOVE:
- while (stage <= COUNTERMOVE)
- {
- move = refutations[ stage++ - KILLER0];
- if ( move != MOVE_NONE
- && move != ttMove
- && 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:
- if ( !skipQuiets
- && select_move<NEXT>([&](){return move != refutations[0]
- && move != refutations[1]
- && move != refutations[2];}))
- return move;
-
- // Point to beginning and end of bad captures
- cur = moves, endMoves = endBadCaptures;
- ++stage;
- /* fallthrough */
-
- case BAD_CAPTURE:
- return select_move<NEXT>(Any);
-
- case EVASION_INIT:
- cur = moves;
- endMoves = generate<EVASIONS>(pos, cur);
- score<EVASIONS>();
- ++stage;
- /* fallthrough */
-
- case EVASION:
- return select_move<BEST_SCORE>(Any);
-
- case PROBCUT:
- return select_move<BEST_SCORE>([&](){ return pos.see_ge(move, threshold); });
-
- case QCAPTURE:
- if (select_move<BEST_SCORE>([&](){ return depth > DEPTH_QS_RECAPTURES
- || to_sq(move) == recaptureSquare; }))
- return move;
-
- // If we did not find any move and we do not try checks, we have finished
- if (depth != DEPTH_QS_CHECKS)
- return MOVE_NONE;
-
- ++stage;
- /* fallthrough */
-
- case QCHECK_INIT:
- cur = moves;
- endMoves = generate<QUIET_CHECKS>(pos, cur);
- ++stage;
- /* fallthrough */
-
- case QCHECK:
- return select_move<NEXT>(Any);
- }
-
- assert(false);
- return MOVE_NONE; // Silence warning
+ // Prepare the pointers to loop over the bad captures
+ cur = moves;
+ endMoves = endBadCaptures;
+
+ ++stage;
+ [[fallthrough]];
+
+ case BAD_CAPTURE :
+ if (select<Next>([]() { return true; }))
+ return *(cur - 1);
+
+ // Prepare the pointers to loop over the bad quiets
+ cur = beginBadQuiets;
+ endMoves = endBadQuiets;
+
+ ++stage;
+ [[fallthrough]];
+
+ case BAD_QUIET :
+ if (!skipQuiets)
+ return select<Next>([]() { return true; });
+
+ return Move::none();
+
+ 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<Next>([&]() { return pos.see_ge(*cur, threshold); });
+
+ case QCAPTURE :
+ return select<Next>([]() { return true; });
+ }
+
+ assert(false);
+ return Move::none(); // Silence warning