- // Case 2: King can capture pawn
- if (bit_is_set(bka, pawnSquare) && !bit_is_set(wka, pawnSquare))
- return true;
- }
- else
- {
- // Case 1: Stalemate
- if ( whiteKingSquare == SQ_A8
- && pawnSquare == SQ_A7
- && (blackKingSquare == SQ_C7 || blackKingSquare == SQ_C8))
- return true;
- }
- return false;
+ // The position is an immediate win if it is white to move and the
+ // white pawn can be promoted without getting captured.
+ if ( square_rank(pawnSquare) == RANK_7
+ && sideToMove == WHITE
+ && whiteKingSquare != pawnSquare + DELTA_N
+ && ( square_distance(blackKingSquare, pawnSquare + DELTA_N) > 1
+ || bit_is_set(wk_attacks(), pawnSquare + DELTA_N)))
+ return RESULT_WIN;
+
+ // Check for known draw positions
+ //
+ // Case 1: Stalemate
+ if ( sideToMove == BLACK
+ && (bk_attacks() & ~(wk_attacks() | pawn_attacks())) == EmptyBoardBB)
+ return RESULT_DRAW;
+
+ // Case 2: King can capture pawn
+ if ( sideToMove == BLACK
+ && bit_is_set(bk_attacks(), pawnSquare) && !bit_is_set(wk_attacks(), pawnSquare))
+ return RESULT_DRAW;
+
+ // Case 3: Black king in front of white pawn
+ if ( blackKingSquare == pawnSquare + DELTA_N
+ && square_rank(pawnSquare) < RANK_7)
+ return RESULT_DRAW;
+
+ // Case 4: White king in front of pawn and black has opposition
+ if ( whiteKingSquare == pawnSquare + DELTA_N
+ && blackKingSquare == pawnSquare + DELTA_N + DELTA_N + DELTA_N
+ && square_rank(pawnSquare) < RANK_5
+ && sideToMove == WHITE)
+ return RESULT_DRAW;
+
+ // Case 5: Stalemate with rook pawn
+ if ( blackKingSquare == SQ_A8
+ && square_file(pawnSquare) == FILE_A)
+ return RESULT_DRAW;
+
+ return RESULT_UNKNOWN;