- while (true)
- {
- while (curMove != lastMove)
- {
- switch (phase) {
-
- case PH_TT_MOVES:
- move = (curMove++)->move;
- if ( move != MOVE_NONE
- && pos.move_is_legal(move, pinned))
- return move;
- break;
-
- case PH_GOOD_CAPTURES:
- move = pick_best(curMove++, lastMove).move;
- if ( move != ttMoves[0].move
- && move != ttMoves[1].move
- && pos.pl_move_is_legal(move, pinned))
- {
- // Check for a non negative SEE now
- int seeValue = pos.see_sign(move);
- if (seeValue >= badCaptureThreshold)
- return move;
-
- // Losing capture, move it to the tail of the array, note
- // that move has now been already checked for legality.
- (--badCaptures)->move = move;
- badCaptures->score = seeValue;
- }
- break;
-
- case PH_KILLERS:
- move = (curMove++)->move;
- if ( move != MOVE_NONE
- && pos.move_is_legal(move, pinned)
- && move != ttMoves[0].move
- && move != ttMoves[1].move
- && !pos.move_is_capture(move))
- return move;
- break;
-
- case PH_NONCAPTURES:
-
- // Sort negative scored moves only when we get there
- if (curMove == lastGoodNonCapture)
- insertion_sort<MoveStack>(lastGoodNonCapture, lastMove);
-
- move = (curMove++)->move;
- if ( move != ttMoves[0].move
- && move != ttMoves[1].move
- && move != killers[0].move
- && move != killers[1].move
- && pos.pl_move_is_legal(move, pinned))
- return move;
- break;
-
- case PH_BAD_CAPTURES:
- move = pick_best(curMove++, lastMove).move;
- return move;
-
- case PH_EVASIONS:
- case PH_QCAPTURES:
- move = pick_best(curMove++, lastMove).move;
- if ( move != ttMoves[0].move
- && pos.pl_move_is_legal(move, pinned))
- return move;
- break;
-
- case PH_QCHECKS:
- move = (curMove++)->move;
- if ( move != ttMoves[0].move
- && pos.pl_move_is_legal(move, pinned))
- return move;
- break;
-
- case PH_STOP:
- return MOVE_NONE;
-
- default:
- assert(false);
- break;
- }
- }
- go_next_phase();
+ 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;
+
+ ++stage;
+ /* fallthrough */
+
+ case QCHECK_INIT:
+ cur = moves;
+ endMoves = generate<QUIET_CHECKS>(pos, cur);
+
+ ++stage;
+ /* fallthrough */
+
+ case QCHECK:
+ return select<Next>([](){ return true; });