- switch(PhaseTable[phaseIndex]) {
-
- case PH_GOOD_CAPTURES:
- assert(!pos->is_check());
- assert(movesPicked >= 0);
- while(movesPicked < numOfMoves) {
- bestScore = -10000000;
- bestIndex = -1;
- for(int i = movesPicked; i < numOfMoves; i++) {
- if(moves[i].score < 0) {
- // Losing capture, move it to the badCaptures[] array
- assert(numOfBadCaptures < 63);
- badCaptures[numOfBadCaptures++] = moves[i];
- moves[i--] = moves[--numOfMoves];
- }
- else if(moves[i].score > bestScore) {
- bestIndex = i;
- bestScore = moves[i].score;
- }
- }
- if(bestIndex != -1) { // Found a good capture
- move = moves[bestIndex].move;\r
- moves[bestIndex] = moves[movesPicked++];
- if(move != ttMove && move != mateKiller &&
- pos->move_is_legal(move, pinned))
- return move;
- }
- }
- break;
-
- case PH_NONCAPTURES:
- assert(!pos->is_check());
- assert(movesPicked >= 0);
- while(movesPicked < numOfMoves) {
- bestScore = -10000000;
-
- // If this is a PV node or we have only picked a few moves, scan
- // the entire move list for the best move. If many moves have already
- // been searched and it is not a PV node, we are probably failing low
- // anyway, so we just pick the first move from the list.
- if(pvNode || movesPicked < 12) {
- bestIndex = -1;
- for(int i = movesPicked; i < numOfMoves; i++)
- if(moves[i].score > bestScore) {
- bestIndex = i;
- bestScore = moves[i].score;
- }
- }
- else
- bestIndex = movesPicked;
-
- if(bestIndex != -1) {
- move = moves[bestIndex].move;\r
- moves[bestIndex] = moves[movesPicked++];
- if(move != ttMove && move != mateKiller &&
- pos->move_is_legal(move, pinned))
- return move;
- }
- }
- break;
-
- case PH_EVASIONS:
- assert(pos->is_check());
- assert(movesPicked >= 0);
- while(movesPicked < numOfMoves) {
- bestScore = -10000000;
- bestIndex = -1;
- for(int i = movesPicked; i < numOfMoves; i++)
- if(moves[i].score > bestScore) {
- bestIndex = i;
- bestScore = moves[i].score;
- }
-
- if(bestIndex != -1) {
- move = moves[bestIndex].move;\r
- moves[bestIndex] = moves[movesPicked++];
- return move;
- }
- }
- break;
-
- case PH_BAD_CAPTURES:
- assert(!pos->is_check());
- assert(badCapturesPicked >= 0);
- // It's probably a good idea to use SEE move ordering here, instead
- // of just picking the first move. FIXME
- while(badCapturesPicked < numOfBadCaptures) {
- move = badCaptures[badCapturesPicked++].move;
- if(move != ttMove && move != mateKiller &&
- pos->move_is_legal(move, pinned))
- return move;
- }
- break;
-
- case PH_QCAPTURES:
- assert(!pos->is_check());
- assert(movesPicked >= 0);
- while(movesPicked < numOfMoves) {
- bestScore = -10000000;
- if(movesPicked < 4) {
- bestIndex = -1;
- for(int i = movesPicked; i < numOfMoves; i++)
- if(moves[i].score > bestScore) {
- bestIndex = i;
- bestScore = moves[i].score;
+ while (true)
+ {
+ while (curMove != lastMove)
+ {
+ switch (phase) {
+
+ case PH_TT_MOVES:
+ move = (curMove++)->move;
+ if ( move != MOVE_NONE
+ && move_is_legal(pos, 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 >= 0)
+ return move;
+
+ // Losing capture, move it to the badCaptures[] array, note
+ // that move has now been already checked for legality.
+ assert(int(lastBadCapture - badCaptures) < 63);
+ lastBadCapture->move = move;
+ lastBadCapture->score = seeValue;
+ lastBadCapture++;
+ }
+ break;
+
+ case PH_KILLERS:
+ move = (curMove++)->move;
+ if ( move != MOVE_NONE
+ && move != ttMoves[0].move
+ && move != ttMoves[1].move
+ && move_is_legal(pos, move, pinned)
+ && !pos.move_is_capture(move))
+ return move;
+ break;
+
+ case PH_NONCAPTURES:
+ 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;