public static bool IsKrkpDrawish(ChessBoard cb) { var leadingColor = cb.Pieces[White][Rook] != 0 ? White : Black; var rook = cb.Pieces[leadingColor][Rook]; var pawn = cb.Pieces[1 - leadingColor][Pawn]; var pawnIndex = BitOperations.TrailingZeroCount(pawn); var winningKing = cb.Pieces[leadingColor][King]; var losingKing = cb.Pieces[1 - leadingColor][King]; if ((Bitboard.GetFile(pawn) & winningKing) != 0 && (leadingColor == White && pawnIndex > cb.KingIndex[leadingColor] || leadingColor == Black && pawnIndex < cb.KingIndex[leadingColor])) { // If the stronger side's king is in front of the pawn, it's a win return(false); } if (Util.GetDistance(losingKing, pawn) >= 3 + (cb.ColorToMove == 1 - leadingColor ? 1 : 0) && Util.GetDistance(losingKing, rook) >= 3) { // If the weaker side's king is too far from the pawn and the rook, it's a win. return(false); } if (leadingColor == White) { if (Bitboard.GetRank(losingKing) <= Bitboard.Rank3 && Util.GetDistance(losingKing, pawn) == 1 && Bitboard.GetRank(winningKing) >= Bitboard.Rank4 && Util.GetDistance(winningKing, pawn) > 2 + (cb.ColorToMove == leadingColor ? 1 : 0)) { // If the pawn is far advanced and supported by the defending king, the position is drawish return(true); } } else { if (Bitboard.GetRank(losingKing) >= Bitboard.Rank5 && Util.GetDistance(losingKing, pawn) == 1 && Bitboard.GetRank(winningKing) <= Bitboard.Rank5 && Util.GetDistance(winningKing, pawn) > 2 + (cb.ColorToMove == leadingColor ? 1 : 0)) { // If the pawn is far advanced and supported by the defending king, the position is drawish return(true); } } return(false); }