diff --git a/js/goals/piece-structures.js b/js/goals/piece-structures.js index fad74a6..4b0050d 100644 --- a/js/goals/piece-structures.js +++ b/js/goals/piece-structures.js @@ -12,18 +12,19 @@ module.exports = { }, tripleDoublePawns: function (position) { - // Fast check for any kind of doubled pawns to quickly eliminate most positions - if (!/P[A-Za-z.]{7}P|p[A-Za-z.]{7}p/.test(position)) { - return false - } - for (const piece of ['P', 'p']) { - const inFile = new Set() - const re = new RegExp(`${piece}(?=[A-Za-z.]{7}${piece})`, 'g') - for (const m of position.matchAll(re)) { - inFile.add(m.index % 8) + let doublePawnCount = 0 + for (let file = 0; file < 8; file++) { + let pawnsOnFile = 0 + for (let rank = 1; rank < 7; rank++) { + if (position[file + rank * 8] === piece) { + pawnsOnFile++ + } + } + if (pawnsOnFile >= 2) doublePawnCount++ } - if (inFile.size > 2) { + + if (doublePawnCount >= 3) { return piece === 'P' ? 'white' : 'black' } } diff --git a/tests/pawn-stacks.test.js b/tests/pawn-stacks.test.js index 28c6743..d9d5331 100644 --- a/tests/pawn-stacks.test.js +++ b/tests/pawn-stacks.test.js @@ -50,6 +50,16 @@ test('test quad pawns do not count for triple double pawns', () => { expect(pieceStructures.tripleDoublePawns(fenToPosition(fen))).toBe(false) }) +test('triple double pawns with spaces in between', () => { + let fen = '4k3/6P1/5n2/1N1P2P1/1P1q4/3P4/1P6/4K3 w - - 0 1' + expect(pieceStructures.tripleDoublePawns(fenToPosition(fen))).toBe('white') +}) + +test('realistic position of triple double pawns with spaces in between', () => { + let fen = '4k3/q2p3p/p6p/P6p/P1P3Qp/7P/2P4P/4K3 w - - 0 1' + expect(pieceStructures.tripleDoublePawns(fenToPosition(fen))).toBe('white') +}) + test('test 6 pawns in the same file', () => { let fen = 'k7/3p4/3p4/3p4/3P4/3P4/3P4/K7 w - - 0 1' expect(pieceStructures.sixPawnsInTheSameFile(fenToPosition(fen))).toBe(true)