Skip to content

When checking EnPassant legality, why do you check if our king is behind our pawn? #22

Description

@FismanMaxim

When you check the legality of an en passant capture, except for this capture opening a horizontal check, you also have this interesting condition, which I don't understand why it's necessary.
As I understand the code, it seems to check that there's no friendly king behind the friendly pawn and looks like a rule "En passant cannot be used to avoid check", although this rule does not exist...

// check if 
enemy pawn is controlling this square (can't use pawn attack bitboard, because pawn has been captured)
	for (int i = 0; i < 2; i++) {
		// Check if square exists diagonal to friendly king from which enemy pawn could be attacking it
		if (numSquaresToEdge[friendlyKingSquare][pawnAttackDirections[friendlyColourIndex][i]] > 0) {
			// move in direction friendly pawns attack to get square from which enemy pawn would attack
			int piece = board.Square[friendlyKingSquare + directionOffsets[pawnAttackDirections[friendlyColourIndex][i]]];
			if (piece == (Piece.Pawn | opponentColour)) // is enemy pawn
			{
				return true;
			}
		}
	}

Why is this condition necessary?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions