Skip to content

Commit

Permalink
fix: maia board mirroring bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjosethomas committed Feb 13, 2025
1 parent 3d83585 commit f8c5bc8
Showing 1 changed file with 52 additions and 35 deletions.
87 changes: 52 additions & 35 deletions src/hooks/useMaiaEngine/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,51 @@ function mirrorSquare(square: string): string {
}

/**
* Mirrors a FEN string vertically (top-to-bottom flip).
* The active color, castling rights, and en passant target are adjusted accordingly.
* Swaps the colors of pieces in a rank by changing uppercase to lowercase and vice versa.
* @param rank The rank to be mirrored.
* @returns The mirrored rank.
*/
function swapColorsInRank(rank: string): string {
let swappedRank = ''
for (const char of rank) {
if (/[A-Z]/.test(char)) {
swappedRank += char.toLowerCase()
} else if (/[a-z]/.test(char)) {
swappedRank += char.toUpperCase()
} else {
// Numbers representing empty squares
swappedRank += char
}
}
return swappedRank
}

function swapCastlingRights(castling: string): string {
if (castling === '-') return '-'

// Capture the current rights in a Set.
const rights = new Set(castling.split(''))
const swapped = new Set<string>()

// Swap white and black castling rights.
if (rights.has('K')) swapped.add('k')
if (rights.has('Q')) swapped.add('q')
if (rights.has('k')) swapped.add('K')
if (rights.has('q')) swapped.add('Q')

// Output in canonical order: white kingside, white queenside, black kingside, black queenside.
let output = ''
if (swapped.has('K')) output += 'K'
if (swapped.has('Q')) output += 'Q'
if (swapped.has('k')) output += 'k'
if (swapped.has('q')) output += 'q'

return output === '' ? '-' : output
}

/**
* Mirrors a FEN string vertically (top-to-bottom flip) while swapping piece colors.
* Additionally, the active color, castling rights, and en passant target are adjusted accordingly.
*
* @param fen - The FEN string to be mirrored.
* @returns The mirrored FEN string.
Expand All @@ -242,50 +285,24 @@ function mirrorFEN(fen: string): string {
const [position, activeColor, castling, enPassant, halfmove, fullmove] =
fen.split(' ')

// Split the position into ranks
// Mirror board rows vertically and swap piece colors.
const ranks = position.split('/')

// Mirror the ranks (top-to-bottom flip)
const mirroredRanks = ranks
.slice()
.reverse()
.map((rank) => swapColorsInRank(rank))

// Reconstruct the mirrored position
const mirroredPosition = mirroredRanks.join('/')

// Swap active color
// Swap active color.
const mirroredActiveColor = activeColor === 'w' ? 'b' : 'w'

// Adjust castling rights: Swap uppercase (white) with lowercase (black) and vice versa
// const mirroredCastling = swapCastlingRights(castling)

// En passant square: Mirror the rank only (since flipping top-to-bottom)
// const mirroredEnPassant = enPassant !== '-' ? mirrorEnPassant(enPassant) : '-'
// Swap castling rights.
const mirroredCastling = swapCastlingRights(castling)

// Return the new FEN
// return `${mirroredPosition} ${mirroredActiveColor} ${mirroredCastling} ${mirroredEnPassant} ${halfmove} ${fullmove}`
return `${mirroredPosition} ${mirroredActiveColor} ${castling} ${enPassant} ${halfmove} ${fullmove}`
}
// Mirror en passant target square.
const mirroredEnPassant = enPassant !== '-' ? mirrorSquare(enPassant) : '-'

/**
* Swaps the colors of pieces in a rank by changing uppercase to lowercase and vice versa.
* @param rank The rank to be mirrored.
* @returns The mirrored rank.
*/
function swapColorsInRank(rank: string): string {
let swappedRank = ''
for (const char of rank) {
if (/[A-Z]/.test(char)) {
swappedRank += char.toLowerCase()
} else if (/[a-z]/.test(char)) {
swappedRank += char.toUpperCase()
} else {
// Numbers representing empty squares
swappedRank += char
}
}
return swappedRank
return `${mirroredPosition} ${mirroredActiveColor} ${mirroredCastling} ${mirroredEnPassant} ${halfmove} ${fullmove}`
}

export { preprocess, mirrorMove, allPossibleMovesReversed }

0 comments on commit f8c5bc8

Please sign in to comment.