From 583f9ba34610d959db25e9e11f6db9599bc104fe Mon Sep 17 00:00:00 2001 From: Artsvi Manukyan <42891870+Manukyanq@users.noreply.github.com> Date: Sun, 28 May 2023 16:59:51 +0300 Subject: [PATCH] Add check for invalid pawn placement in validateFen (#412) --- __tests__/validate_fen.test.ts | 10 ++++++++++ src/chess.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/__tests__/validate_fen.test.ts b/__tests__/validate_fen.test.ts index 17316f5c..79c9b24a 100644 --- a/__tests__/validate_fen.test.ts +++ b/__tests__/validate_fen.test.ts @@ -26,6 +26,16 @@ test.each([ fen: 'kk6/8/8/8/8/8/8/7K w - - 0 1', ok: false, }, + { + // white pawn on 8th row + fen: '3P4/1k2K3/8/8/8/8/8/8 w - - 0 1', + ok: false, + }, + { + // black pawn on 1st row + fen: '8/8/8/8/8/8/1k3K2/3p4 w - - 0 1', + ok: false, + }, { fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNRw KQkq - 0 1', ok: false, diff --git a/src/chess.ts b/src/chess.ts index 178e3850..b01c6bdd 100644 --- a/src/chess.ts +++ b/src/chess.ts @@ -379,6 +379,7 @@ export function validateFen(fen: string) { } } + // 9th criterion: is en-passant square legal? if ( (tokens[3][1] == '3' && tokens[1] == 'w') || (tokens[3][1] == '6' && tokens[1] == 'b') @@ -386,6 +387,7 @@ export function validateFen(fen: string) { return { ok: false, error: 'Invalid FEN: illegal en-passant square' } } + // 10th criterion: does chess position contain exact two kings? const kings = [ { color: 'white', regex: /K/g }, { color: 'black', regex: /k/g }, @@ -401,6 +403,16 @@ export function validateFen(fen: string) { } } + // 11th criterion: are any pawns on the first or eighth rows? + if ( + Array.from(rows[0] + rows[7]).some((char) => char.toUpperCase() === 'P') + ) { + return { + ok: false, + error: 'Invalid FEN: some pawns are on the edge rows', + } + } + return { ok: true } }