Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate_fen : additional checks on kings and on pawns locations. #353

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions __tests__/chess.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,42 @@ describe('Validate FEN', () => {
fen: 'rnbqkbnr/1p1ppppp/B1p5/8/6P1/4P3/PPPP1P1P/RNBQK1NR w KQkq g3 0 3',
error_number: 11,
},
{
fen: '8/2K5/8/8/2K1NB2/8/8/8 w - - 96 46',
error_number: 12,
},
{
fen: 'K7/K7/8/8/8/8/8/8 w - - 0 1',
error_number: 12,
},
{
fen: 'k7/k7/8/8/8/8/8/8 w - - 0 1',
error_number: 12,
},
{
fen: '8/8/8/8/8/3k4/2K5/8 w - - 0 1',
error_number: 13,
},
{
fen: '8/8/8/4k3/4K3/8/8/8 b - - 0 1',
error_number: 13,
},
{
fen: '2P5/8/8/3k4/8/2K5/8/8 w - - 0 1',
error_number: 14,
},
{
fen: '8/8/8/3k4/8/2K5/8/5P2 w - - 0 1',
error_number: 14,
},
{
fen: '8/8/8/3k4/8/5K2/8/p7 w - - 0 1',
error_number: 14,
},
{
fen: '6p1/8/8/3k4/8/5K2/8/8 w - - 0 1',
error_number: 14,
},
{
fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
error_number: 0,
Expand Down
157 changes: 113 additions & 44 deletions chess.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,40 +47,40 @@ const PIECE_OFFSETS = {

// prettier-ignore
const ATTACKS = [
20, 0, 0, 0, 0, 0, 0, 24,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 0, 0,-17, 0, 0,
-15, 0, 0, 0, 0, 0, 0,-16, 0, 0, 0, 0, 0, 0,-17
17, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 15, 0,
0, 17, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 15, 0, 0,
0, 0, 17, 0, 0, 0, 0, 16, 0, 0, 0, 0, 15, 0, 0, 0,
0, 0, 0, 17, 0, 0, 0, 16, 0, 0, 0, 15, 0, 0, 0, 0,
0, 0, 0, 0, 17, 0, 0, 16, 0, 0, 15, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 17, 0, 16, 0, 15, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 17, 16, 15, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 0, -1, -1, -1, -1, -1, -1, -1, 0,
0, 0, 0, 0, 0, 0, -15, -16, -17, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, -15, 0, -16, 0, -17, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, -15, 0, 0, -16, 0, 0, -17, 0, 0, 0, 0, 0,
0, 0, 0, -15, 0, 0, 0, -16, 0, 0, 0, -17, 0, 0, 0, 0,
0, 0, -15, 0, 0, 0, 0, -16, 0, 0, 0, 0, -17, 0, 0, 0,
0, -15, 0, 0, 0, 0, 0, -16, 0, 0, 0, 0, 0, -17, 0, 0,
-15, 0, 0, 0, 0, 0, 0, -16, 0, 0, 0, 0, 0, 0, -17
];

const SHIFTS = { p: 0, n: 1, b: 2, r: 3, q: 4, k: 5 }
Expand All @@ -106,13 +106,13 @@ const RANK_8 = 0

// prettier-ignore
const SQUARE_MAP = {
a8: 0, b8: 1, c8: 2, d8: 3, e8: 4, f8: 5, g8: 6, h8: 7,
a7: 16, b7: 17, c7: 18, d7: 19, e7: 20, f7: 21, g7: 22, h7: 23,
a6: 32, b6: 33, c6: 34, d6: 35, e6: 36, f6: 37, g6: 38, h6: 39,
a5: 48, b5: 49, c5: 50, d5: 51, e5: 52, f5: 53, g5: 54, h5: 55,
a4: 64, b4: 65, c4: 66, d4: 67, e4: 68, f4: 69, g4: 70, h4: 71,
a3: 80, b3: 81, c3: 82, d3: 83, e3: 84, f3: 85, g3: 86, h3: 87,
a2: 96, b2: 97, c2: 98, d2: 99, e2: 100, f2: 101, g2: 102, h2: 103,
a8: 0, b8: 1, c8: 2, d8: 3, e8: 4, f8: 5, g8: 6, h8: 7,
a7: 16, b7: 17, c7: 18, d7: 19, e7: 20, f7: 21, g7: 22, h7: 23,
a6: 32, b6: 33, c6: 34, d6: 35, e6: 36, f6: 37, g6: 38, h6: 39,
a5: 48, b5: 49, c5: 50, d5: 51, e5: 52, f5: 53, g5: 54, h5: 55,
a4: 64, b4: 65, c4: 66, d4: 67, e4: 68, f4: 69, g4: 70, h4: 71,
a3: 80, b3: 81, c3: 82, d3: 83, e3: 84, f3: 85, g3: 86, h3: 87,
a2: 96, b2: 97, c2: 98, d2: 99, e2: 100, f2: 101, g2: 102, h2: 103,
a1: 112, b1: 113, c1: 114, d1: 115, e1: 116, f1: 117, g1: 118, h1: 119
};

Expand Down Expand Up @@ -404,11 +404,6 @@ export const Chess = function (fen) {
return true
}

/* TODO: this function is pretty much crap - it validates structure but
* completely ignores content (e.g. doesn't verify that each side has a king)
* ... we should rewrite this, and ditch the silly error_number field while
* we're at it
*/
function validate_fen(fen) {
var errors = {
0: 'No errors.',
Expand All @@ -422,11 +417,24 @@ export const Chess = function (fen) {
8: '1st field (piece positions) is invalid [consecutive numbers].',
9: '1st field (piece positions) is invalid [invalid piece].',
10: '1st field (piece positions) is invalid [row too large].',
11: 'Illegal en-passant square',
11: '4th field (en-passant square) is invalid [illegal value].',
12: '1st field (piece positions) is invalid [illegal kings count].',
13: '1st field (piece positions) is invalid [kings on neighbour cells].',
14: '1st field (piece positions) is invalid [pawn on rank 1/8].',
}

/* 1st criterion: 6 space-seperated fields? */
var tokens = fen.split(/\s+/)

/* Empty board is valid */
if (
tokens[0] === '8/8/8/8/8/8/8/8' &&
tokens[2] === '-' &&
tokens[3] === '-'
) {
return { valid: true, error_number: 0, error: errors[0] }
}

/* 1st criterion: 6 space-seperated fields? */
if (tokens.length !== 6) {
return { valid: false, error_number: 1, error: errors[1] }
}
Expand Down Expand Up @@ -495,6 +503,67 @@ export const Chess = function (fen) {
return { valid: false, error_number: 11, error: errors[11] }
}

/* Is there exactly one white king and one black king ? */
var wk_count = 0
var bk_count = 0

for (var i = 0, l = tokens[0].length; i < l; i++) {
if (tokens[0][i] === 'K') {
wk_count += 1
} else if (tokens[0][i] === 'k') {
bk_count += 1
}
}
if (wk_count > 1 || bk_count > 1) {
return { valid: false, error_number: 12, error: errors[12] }
}

/* Are white and black kings on neighbour cells ? */
var wk_square = [-1, -1]
var bk_square = [-1, -1]

var lines = tokens[0].split('/')

for (var row = 0; row < lines.length; row++) {
var line = lines[row]
var lineArr = line.split('')

var file = 0
/* Row index is reversed, but that does not matter here for us. */
for (var col = 0; col < lineArr.length; col++) {
var elem = lineArr[col]
if (elem.match(/\d/)) {
var digit = elem.charCodeAt(0) - '0'.charCodeAt(0)
file += digit
} else {
if (elem === 'K') {
wk_square[0] = row
wk_square[1] = file
} else if (elem === 'k') {
bk_square[0] = row
bk_square[1] = file
}
file++
}
}
}
let abs_dy = Math.abs(wk_square[0] - bk_square[0])
let abs_dx = Math.abs(wk_square[1] - bk_square[1])

if (abs_dx <= 1 && abs_dy <= 1) {
return { valid: false, error_number: 13, error: errors[13] }
}

/* pawn on rank 1 / rank 8 ? */
if (
lines[0].includes('P') ||
lines[0].includes('p') ||
lines[7].includes('P') ||
lines[7].includes('p')
) {
return { valid: false, error_number: 14, error: errors[14] }
}

/* everything's okay! */
return { valid: true, error_number: 0, error: errors[0] }
}
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"main": "chess.js",
"homepage": "https://github.com/jhlywa/chess.js",
"author": "Jeff Hlywa <[email protected]>",
"keywords": [
"chess"
],
"keywords": ["chess"],
"repository": {
"type": "git",
"url": "https://github.com/jhlywa/chess.js.git"
Expand Down