+ void KPKPosition::classify_leaf(unsigned idx) {
+
+ stm = Color(idx & 1);
+ bksq = Square((idx >> 1) & 0x3F);
+ wksq = Square((idx >> 7) & 0x3F);
+ psq = File((idx >> 13) & 3) | Rank(6 - (idx >> 15));
+
+ // Check if two pieces are on the same square or if a king can be captured
+ if ( wksq == psq || wksq == bksq || bksq == psq
+ || (k_attacks<WHITE>() & bksq)
+ || (stm == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
+ res = INVALID;
+
+ // The position is an immediate win if it is white to move and the white
+ // pawn can be promoted without getting captured.
+ else if ( rank_of(psq) == RANK_7
+ && stm == WHITE
+ && wksq != psq + DELTA_N
+ && ( square_distance(bksq, psq + DELTA_N) > 1
+ ||(k_attacks<WHITE>() & (psq + DELTA_N))))
+ res = WIN;
+
+ // Check for known draw positions
+ //
+ // Case 1: Stalemate
+ else if ( stm == BLACK
+ && !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | StepAttacksBB[PAWN][psq])))
+ res = DRAW;
+
+ // Case 2: King can capture undefended pawn
+ else if ( stm == BLACK
+ && (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
+ res = DRAW;
+
+ // Case 3: Black king in front of white pawn
+ else if ( bksq == psq + DELTA_N
+ && rank_of(psq) < RANK_7)
+ res = DRAW;
+
+ // Case 4: White king in front of pawn and black has opposition
+ else if ( stm == WHITE
+ && wksq == psq + DELTA_N
+ && bksq == wksq + DELTA_N + DELTA_N
+ && rank_of(psq) < RANK_5)
+ res = DRAW;
+
+ // Case 5: Stalemate with rook pawn
+ else if ( bksq == SQ_A8
+ && file_of(psq) == FILE_A)
+ res = DRAW;
+
+ // Case 6: White king trapped on the rook file
+ else if ( file_of(wksq) == FILE_A
+ && file_of(psq) == FILE_A
+ && rank_of(wksq) > rank_of(psq)
+ && bksq == wksq + 2)
+ res = DRAW;