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

triple double pawns: Also count positions with spaces in between #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
21 changes: 11 additions & 10 deletions js/goals/piece-structures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/pawn-stacks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down