-
- Result classify_btm(const KPKPosition &p) {
-
- // If one move leads to a position classified as RESULT_DRAW, the result
- // of the current position is RESULT_DRAW. If all moves lead to positions
- // classified as RESULT_WIN, the current position is classified as
- // RESULT_LOSS. Otherwise, the current position is classified as
- // RESULT_UNKNOWN.
-
- bool unknownFound = false;
- Bitboard b;
- Square s;
-
- // King moves
- b = p.bk_attacks();
- while(b) {
- s = pop_1st_bit(&b);
- switch(Bitbase[compute_index(p.whiteKingSquare, s, p.pawnSquare,
- WHITE)]) {
- case RESULT_DRAW:
- return RESULT_DRAW;
-
- case RESULT_UNKNOWN:
- unknownFound = true;
- break;
-
- case RESULT_WIN: case RESULT_INVALID:
- break;
-
- default:
- assert(false);
- }
+ template<Color Us>
+ Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
+
+ // White to move: If one move leads to a position classified as WIN, the result
+ // of the current position is WIN. If all moves lead to positions classified
+ // as DRAW, the current position is classified as DRAW, otherwise the current
+ // position is classified as UNKNOWN.
+ //
+ // Black to move: If one move leads to a position classified as DRAW, the result
+ // of the current position is DRAW. If all moves lead to positions classified
+ // as WIN, the position is classified as WIN, otherwise the current position is
+ // classified as UNKNOWN.
+
+ constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
+ constexpr Result Good = (Us == WHITE ? WIN : DRAW);
+ constexpr Result Bad = (Us == WHITE ? DRAW : WIN);
+
+ Result r = INVALID;
+ Bitboard b = PseudoAttacks[KING][ksq[Us]];
+
+ while (b)
+ r |= Us == WHITE ? db[index(Them, ksq[Them] , pop_lsb(&b), psq)]
+ : db[index(Them, pop_lsb(&b), ksq[Them] , psq)];
+
+ if (Us == WHITE)
+ {
+ if (rank_of(psq) < RANK_7) // Single push
+ r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH)];
+
+ if ( rank_of(psq) == RANK_2 // Double push
+ && psq + NORTH != ksq[Us]
+ && psq + NORTH != ksq[Them])
+ r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH + NORTH)];