- Result KPKPosition::classify_leaf(unsigned idx) {
-
- wksq = Square((idx >> 0) & 0x3F);
- bksq = Square((idx >> 6) & 0x3F);
- us = Color((idx >> 12) & 0x01);
- 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
- || (StepAttacksBB[KING][wksq] & bksq)
- || (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
- return res = INVALID;
-
- if (us == WHITE)
- {
- // Immediate win if pawn can be promoted without getting captured
- if ( rank_of(psq) == RANK_7
- && wksq != psq + DELTA_N
- && ( square_distance(bksq, psq + DELTA_N) > 1
- ||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
- return res = WIN;
- }
- // Immediate draw if is stalemate or king captures undefended pawn
- else if ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
- || (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
- return res = DRAW;
-
- return res = UNKNOWN;
+ KPKPosition::KPKPosition(unsigned idx) {
+
+ ksq[WHITE] = Square((idx >> 0) & 0x3F);
+ ksq[BLACK] = Square((idx >> 6) & 0x3F);
+ stm = Color ((idx >> 12) & 0x01);
+ psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
+
+ // Invalid if two pieces are on the same square or if a king can be captured
+ if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
+ || ksq[WHITE] == psq
+ || ksq[BLACK] == psq
+ || (stm == WHITE && (pawn_attacks_bb(WHITE, psq) & ksq[BLACK])))
+ result = INVALID;
+
+ // Win if the pawn can be promoted without getting captured
+ else if ( stm == WHITE
+ && rank_of(psq) == RANK_7
+ && ksq[WHITE] != psq + NORTH
+ && ( distance(ksq[BLACK], psq + NORTH) > 1
+ || (distance(ksq[WHITE], psq + NORTH) == 1)))
+ result = WIN;
+
+ // Draw if it is stalemate or the black king can capture the pawn
+ else if ( stm == BLACK
+ && ( !(attacks_bb<KING>(ksq[BLACK]) & ~(attacks_bb<KING>(ksq[WHITE]) | pawn_attacks_bb(WHITE, psq)))
+ || (attacks_bb<KING>(ksq[BLACK]) & ~attacks_bb<KING>(ksq[WHITE]) & psq)))
+ result = DRAW;
+
+ // Position will be classified later
+ else
+ result = UNKNOWN;