- while (true)
- {
- switch (phase) {
-
- case PH_NULL_MOVE:
- go_next_phase();
- return MOVE_NULL;
-
- case PH_TT_MOVES:
- while (movesPicked < 2)
- {
- Move move = ttMoves[movesPicked++];
- if ( move != MOVE_NONE
- && move_is_legal(pos, move, pinned))
- return move;
- }
- break;
-
- case PH_GOOD_CAPTURES:
- while (curMove != lastMove)
- {
- Move move = (curMove++)->move;
- if ( move != ttMoves[0]
- && move != ttMoves[1]
- && pos.pl_move_is_legal(move, pinned))
- {
- // Check for a non negative SEE now
- int seeValue = pos.see_sign(move);
- if (seeValue >= 0)
- return move;
-
- // Losing capture, move it to the badCaptures[] array, note
- // that move has now been already checked for legality.
- assert(numOfBadCaptures < 63);
- badCaptures[numOfBadCaptures].move = move;
- badCaptures[numOfBadCaptures++].score = seeValue;
- }
- }
- break;
-
- case PH_KILLERS:
- while (movesPicked < 2)
- {
- Move move = killers[movesPicked++];
- if ( move != MOVE_NONE
- && move != ttMoves[0]
- && move != ttMoves[1]
- && move_is_legal(pos, move, pinned)
- && !pos.move_is_capture(move))
- return move;
- }
- break;
-
- case PH_NONCAPTURES:
- while (curMove != lastMove)
- {
- Move move = (curMove++)->move;
- if ( move != ttMoves[0]
- && move != ttMoves[1]
- && move != killers[0]
- && move != killers[1]
- && pos.pl_move_is_legal(move, pinned))
- return move;
- }
- break;
-
- case PH_EVASIONS:
- case PH_BAD_CAPTURES:
- if (curMove != lastMove)
- return (curMove++)->move;
- break;
-
- case PH_QCAPTURES:
- case PH_QCHECKS:
- while (curMove != lastMove)
- {
- Move move = (curMove++)->move;
- // Maybe postpone the legality check until after futility pruning?
- if ( move != ttMoves[0]
- && pos.pl_move_is_legal(move, pinned))
- return move;
- }
- break;
-
- case PH_STOP:
+ // 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)