- if ( wksq == psq || wksq == bksq || bksq == psq
- || (k_attacks<WHITE>() & bksq)
- || (stm == WHITE && (p_attacks() & bksq)))
- return INVALID;
-
- // The position is an immediate win if it is white to move and the white
- // pawn can be promoted without getting captured.
- 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))))
- return WIN;
-
- // Check for known draw positions
- //
- // Case 1: Stalemate
- if ( stm == BLACK
- && !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | p_attacks())))
- return DRAW;
-
- // Case 2: King can capture undefended pawn
- if ( stm == BLACK
- && (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
- return DRAW;
-
- // Case 3: Black king in front of white pawn
- if ( bksq == psq + DELTA_N
- && rank_of(psq) < RANK_7)
- return DRAW;
-
- // Case 4: White king in front of pawn and black has opposition
- if ( stm == WHITE
- && wksq == psq + DELTA_N
- && bksq == wksq + DELTA_N + DELTA_N
- && rank_of(psq) < RANK_5)
- return DRAW;
-
- // Case 5: Stalemate with rook pawn
- if ( bksq == SQ_A8
- && file_of(psq) == FILE_A)
- return DRAW;
-
- // Case 6: White king trapped on the rook file
- if ( file_of(wksq) == FILE_A
- && file_of(psq) == FILE_A
- && rank_of(wksq) > rank_of(psq)
- && bksq == wksq + 2)
- return DRAW;
-
- return UNKNOWN;
+ if ( square_distance(wksq, bksq) <= 1 || wksq == psq || bksq == psq
+ || (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
+ result = INVALID;
+
+ else if (us == WHITE)
+ {
+ // Immediate win if a 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))))
+ result = WIN;
+ }
+ // Immediate draw if it is a stalemate or a king captures undefended pawn
+ else if ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
+ || (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
+ result = DRAW;