Skip to content

Conversation

@YusungAN
Copy link

Fixes #558

This PR updates the validateFen() function to correctly handle cases where the side to move is already delivering check.

Changes

  • Updated validateFen() logic to detect when the side to move is already delivering check
export function validateFen(fen: string): { ok: boolean; error?: string } {
  
  // .. Rest of code

  // 12th criterion: is side to move already delivering check?
  const tokensTemp = tokens.slice()
  tokensTemp[1] = swapColor(tokens[1] as Color)
  const tempFen = tokensTemp.join(' ') // swap the side to move

  const tempChess = new Chess(tempFen, { skipValidation: true })
  if (tempChess.inCheck()) {
    return {
      ok: false,
      error: 'Invalid FEN: side to move is already delivering check',
    }
  }

  return { ok: true }
}
  • Added and updated related test cases
    • Two testcases were added in validate_fen.test.ts.
    • Four testcases in put.test.ts were updated because they triggered the invalid FEN error. Please check if the modified testcases still reflect the original intention.
      put - occupying white en passant square clears it
      put - replacing black pawn clears white en passant square 2
      put - occupying black en passant square clears it
      put - replacing white pawn clears black en passant square 2

@neofight78
Copy link
Collaborator

The code is creating a temporary chess object to do its validation, which, if we are calling it from the constructor is unnecessarily inefficient.

We could add this validation to the constructor instead, but then we'd lose the functionality from this function.

So whilst I understand the aim, I'm not quite sure of the best way forward yet. @jhlywa Thoughts?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing invalid case in validateFen

2 participants