- // Case 2: King can capture pawn
- if (bit_is_set(bka, pawnSquare) && !bit_is_set(wka, pawnSquare))
- return true;
- }
+ KPKPosition::KPKPosition(unsigned idx) {
+
+ ksq[WHITE] = Square((idx >> 0) & 0x3F);
+ ksq[BLACK] = Square((idx >> 6) & 0x3F);
+ us = Color ((idx >> 12) & 0x01);
+ psq = make_square(File((idx >> 13) & 0x3), RANK_7 - Rank((idx >> 15) & 0x7));
+
+ // Check 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
+ || (us == WHITE && (StepAttacksBB[PAWN][psq] & ksq[BLACK])))
+ result = INVALID;
+
+ // Immediate win if a pawn can be promoted without getting captured
+ else if ( us == WHITE
+ && rank_of(psq) == RANK_7
+ && ksq[us] != psq + DELTA_N
+ && ( distance(ksq[~us], psq + DELTA_N) > 1
+ || (StepAttacksBB[KING][ksq[us]] & (psq + DELTA_N))))
+ result = WIN;
+
+ // Immediate draw if it is a stalemate or a king captures undefended pawn
+ else if ( us == BLACK
+ && ( !(StepAttacksBB[KING][ksq[us]] & ~(StepAttacksBB[KING][ksq[~us]] | StepAttacksBB[PAWN][psq]))
+ || (StepAttacksBB[KING][ksq[us]] & psq & ~StepAttacksBB[KING][ksq[~us]])))
+ result = DRAW;
+
+ // Position will be classified later