|
1 | 1 | #include "movepick.h" |
2 | 2 | #include <algorithm> |
| 3 | + |
| 4 | +// clang-format off |
| 5 | +constexpr int16_t MVV_LVA_TABLE[7][7] = { |
| 6 | + // P N B R Q K none |
| 7 | + /* P */{ 0, 200, 250, 450, 900, 0, 0}, |
| 8 | + /* N */{-200, 10, 50, 250, 700, 0, 0}, |
| 9 | + /* B */{-250, -50, 5, 200, 650, 0, 0}, |
| 10 | + /* R */{-450, -250, -200, 15, 450, 0, 0}, |
| 11 | + /* Q */{-900, -700, -650, -450, 20, 0, 0}, |
| 12 | + /* K */{ 0, 0, 0, 0, 0, 0, 0}, |
| 13 | + /* */{ 0, 0, 0, 0, 0, 0, 0}, |
| 14 | +}; |
| 15 | + |
| 16 | +constexpr int16_t CHECK_BONUS = 200; |
| 17 | +constexpr int16_t PROMOTION_BONUS = 200; |
| 18 | +// clang-format on |
| 19 | + |
| 20 | +Move MovePicker::next() { |
| 21 | + switch (stage) { |
| 22 | + case MovePickerStage::TT: { |
| 23 | + // Generate TT move. First check if such move is legal. |
| 24 | + // If not, simply ignore it. |
| 25 | + Move ttMove = Move(ttMoveCode); |
| 26 | + if (ttMove.isValid() && pos.isLegal(ttMove)) { |
| 27 | + stage = MovePickerStage::GEN_NOISY; |
| 28 | + return ttMove; |
| 29 | + } |
| 30 | + } |
| 31 | + [[fallthrough]]; |
| 32 | + |
| 33 | + case MovePickerStage::GEN_NOISY: |
| 34 | + // Generate noisy moves |
| 35 | + generateNoisyMoves(); |
| 36 | + stage = MovePickerStage::GOOD_NOISY; |
| 37 | + [[fallthrough]]; |
| 38 | + |
| 39 | + case MovePickerStage::GOOD_NOISY: |
| 40 | + // Pick a good noisy move |
| 41 | + while (!noisyBuffer.empty()) { |
| 42 | + const auto& scoredMove = noisyBuffer.back(); |
| 43 | + if (scoredMove.score < 0) { // not a good move anymore |
| 44 | + break; // we are done |
| 45 | + } |
| 46 | + noisyBuffer.pop_back(); |
| 47 | + if (scoredMove.moveCode == ttMoveCode) { |
| 48 | + continue; // do not yield the same move twice |
| 49 | + } |
| 50 | + return scoredMove.move(); |
| 51 | + } |
| 52 | + stage = MovePickerStage::KILLER_1; |
| 53 | + [[fallthrough]]; |
| 54 | + |
| 55 | + case MovePickerStage::KILLER_1: { |
| 56 | + // Killer moves are never tactic moves, so they should never appear |
| 57 | + // in the noisy buffer. |
| 58 | + const Move killer1 = history.killerTable[ply].killer1; |
| 59 | + if (killer1.isValid() && pos.isLegal<movegen::MoveGenType::QUIET>(killer1)) { |
| 60 | + stage = MovePickerStage::KILLER_2; |
| 61 | + return killer1; |
| 62 | + } |
| 63 | + } |
| 64 | + [[fallthrough]]; |
| 65 | + |
| 66 | + case MovePickerStage::KILLER_2: { |
| 67 | + const Move killer2 = history.killerTable[ply].killer2; |
| 68 | + if (killer2.isValid() && pos.isLegal<movegen::MoveGenType::QUIET>(killer2)) { |
| 69 | + stage = MovePickerStage::GEN_QUIET; |
| 70 | + return killer2; |
| 71 | + } |
| 72 | + } |
| 73 | + [[fallthrough]]; |
| 74 | + |
| 75 | + case MovePickerStage::GEN_QUIET: |
| 76 | + if (_skipQuiet) { // Maybe used in pruning |
| 77 | + stage = MovePickerStage::END_NORMAL; |
| 78 | + } else { |
| 79 | + generateQuietMoves(); |
| 80 | + stage = MovePickerStage::GOOD_QUIET; |
| 81 | + } |
| 82 | + [[fallthrough]]; |
| 83 | + |
| 84 | + case MovePickerStage::GOOD_QUIET: |
| 85 | + while (!quietBuffer.empty()) { |
| 86 | + const auto& scoredMove = quietBuffer.back(); |
| 87 | + if (scoredMove.score < 0) { // not a good move anymore |
| 88 | + break; // we are done |
| 89 | + } |
| 90 | + quietBuffer.pop_back(); |
| 91 | + if (scoredMove.moveCode == ttMoveCode || |
| 92 | + scoredMove.moveCode == history.killerTable[ply].killer1 || |
| 93 | + scoredMove.moveCode == history.killerTable[ply].killer2) { |
| 94 | + continue; // do not yield the same move twice |
| 95 | + } |
| 96 | + return scoredMove.move(); |
| 97 | + } |
| 98 | + stage = MovePickerStage::BAD_NOISY; |
| 99 | + [[fallthrough]]; |
| 100 | + |
| 101 | + case MovePickerStage::BAD_NOISY: |
| 102 | + while (!noisyBuffer.empty()) { |
| 103 | + const auto& scoredMove = noisyBuffer.back(); |
| 104 | + noisyBuffer.pop_back(); |
| 105 | + if (scoredMove.moveCode == ttMoveCode) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + return scoredMove.move(); |
| 109 | + } |
| 110 | + stage = MovePickerStage::BAD_QUIET; |
| 111 | + [[fallthrough]]; |
| 112 | + |
| 113 | + case MovePickerStage::BAD_QUIET: |
| 114 | + while (!quietBuffer.empty()) { |
| 115 | + const auto& scoredMove = quietBuffer.back(); |
| 116 | + quietBuffer.pop_back(); |
| 117 | + if (scoredMove.moveCode == ttMoveCode || |
| 118 | + scoredMove.moveCode == history.killerTable[ply].killer1 || |
| 119 | + scoredMove.moveCode == history.killerTable[ply].killer2) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + return scoredMove.move(); |
| 123 | + } |
| 124 | + stage = MovePickerStage::END_NORMAL; |
| 125 | + [[fallthrough]]; |
| 126 | + |
| 127 | + case MovePickerStage::END_NORMAL: |
| 128 | + return Move(Move::NO_MOVE); |
| 129 | + |
| 130 | + case MovePickerStage::GEN_QSEARCH: |
| 131 | + if (inCheck) { |
| 132 | + generateEvasionMoves(); |
| 133 | + } else { |
| 134 | + generateNoisyMoves(); |
| 135 | + } |
| 136 | + stage = MovePickerStage::GOOD_QSEARCH; |
| 137 | + [[fallthrough]]; |
| 138 | + |
| 139 | + case MovePickerStage::GOOD_QSEARCH: |
| 140 | + while (!noisyBuffer.empty()) { |
| 141 | + const auto& scoredMove = noisyBuffer.back(); |
| 142 | + if (!inCheck && scoredMove.score < 0) { |
| 143 | + break; |
| 144 | + } |
| 145 | + noisyBuffer.pop_back(); |
| 146 | + if (scoredMove.moveCode == ttMoveCode) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + return scoredMove.move(); |
| 150 | + } |
| 151 | + stage = MovePickerStage::END_QSEARCH; |
| 152 | + [[fallthrough]]; |
| 153 | + |
| 154 | + case MovePickerStage::END_QSEARCH: |
| 155 | + return Move(Move::NO_MOVE); |
| 156 | + |
| 157 | + default: |
| 158 | + return Move(Move::NO_MOVE); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +void MovePicker::skipQuiet() { |
| 163 | + _skipQuiet = true; |
| 164 | +} |
| 165 | + |
| 166 | +void MovePicker::generateNoisyMoves() { |
| 167 | + Movelist noisyMoves; |
| 168 | + movegen::legalmoves<movegen::MoveGenType::CAPTURE>(noisyMoves, pos); |
| 169 | + // Assign scores to moves based on MVV/LVA & SEE |
| 170 | + for (const Move& move : noisyMoves) { |
| 171 | + int16_t score = 0; |
| 172 | + const auto fromSq = move.from(); |
| 173 | + const auto toSq = move.to(); |
| 174 | + const PieceType attacker = pos.at(fromSq).type(); |
| 175 | + const PieceType victim = |
| 176 | + (move.typeOf() == Move::ENPASSANT) ? PieceType::PAWN : pos.at(toSq).type(); |
| 177 | + const int16_t mvvlva = MVV_LVA_TABLE[(int) attacker][(int) victim]; |
| 178 | + if (pos.see(move, 0)) { // static exchange evaluation indicates an acceptable capture |
| 179 | + score = mvvlva; |
| 180 | + // Additional bonus for checks |
| 181 | + if (pos.isCheckMove(move)) { |
| 182 | + score += CHECK_BONUS; |
| 183 | + } |
| 184 | + } else { // a losing capture |
| 185 | + score = mvvlva - 1000; |
| 186 | + } |
| 187 | + |
| 188 | + noisyBuffer.emplace_back(ScoredMove {move.move(), score}); |
| 189 | + } |
| 190 | + // Sort moves by score in ascending order |
| 191 | + std::sort(noisyBuffer.begin(), noisyBuffer.end()); |
| 192 | +} |
| 193 | + |
| 194 | +void MovePicker::generateQuietMoves() { |
| 195 | + Movelist quietMoves; |
| 196 | + movegen::legalmoves<movegen::MoveGenType::QUIET>(quietMoves, pos); |
| 197 | + |
| 198 | + for (const Move& move : quietMoves) { |
| 199 | + int16_t score = 0; |
| 200 | + // Bonus for checks |
| 201 | + if (pos.isCheckMove(move)) { |
| 202 | + score += CHECK_BONUS; |
| 203 | + } |
| 204 | + // Promotion bonus |
| 205 | + if (move.typeOf() == Move::PROMOTION && move.promotionType() == PieceType::QUEEN) { |
| 206 | + score += PROMOTION_BONUS; |
| 207 | + } |
| 208 | + // Penalty for moving into squares controlled by opponent pawns |
| 209 | + if (pos.at(move.from()).type() != PieceType::PAWN && |
| 210 | + (attacks::pawn(pos.sideToMove(), move.to()) & |
| 211 | + pos.pieces(PieceType::PAWN, ~pos.sideToMove()))) { |
| 212 | + score -= 200; |
| 213 | + } |
| 214 | + |
| 215 | + quietBuffer.emplace_back(ScoredMove {move.move(), score}); |
| 216 | + } |
| 217 | + std::sort(quietBuffer.begin(), quietBuffer.end()); |
| 218 | +} |
| 219 | + |
| 220 | +void MovePicker::generateEvasionMoves() { |
| 221 | + Movelist moves; |
| 222 | + movegen::legalmoves(moves, pos); |
| 223 | + for (const Move& move : moves) { |
| 224 | + noisyBuffer.emplace_back(ScoredMove {move.move(), 0}); |
| 225 | + } |
| 226 | +} |
0 commit comments