Created
September 6, 2024 22:38
-
-
Save cheeseonamonkey/361e8480bdda119782b9dc8c8f4f4909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Piece values | |
const piece = { | |
"P": 100, | |
"N": 280, | |
"B": 320, | |
"R": 479, | |
"Q": 929, | |
"K": 60000 | |
}; | |
// Piece-Square Tables (PSTs) | |
const pst = { | |
'P': [ | |
0, 0, 0, 0, 0, 0, 0, 0, | |
78, 83, 86, 73, 102, 82, 85, 90, | |
7, 29, 21, 44, 40, 31, 44, 7, | |
-17, 16, -2, 15, 14, 0, 15, -13, | |
-26, 3, 10, 9, 6, 1, 0, -23, | |
-22, 9, 5, -11, -10, -2, 3, -19, | |
-31, 8, -7, -37, -36, -14, 3, -31, | |
0, 0, 0, 0, 0, 0, 0, 0 | |
], | |
'N': [ | |
-66, -53, -75, -75, -10, -55, -58, -70, | |
-3, -6, 100, -36, 4, 62, -4, -14, | |
10, 67, 1, 74, 73, 27, 62, -2, | |
24, 24, 45, 37, 33, 41, 25, 17, | |
-1, 5, 31, 21, 22, 35, 2, 0, | |
-18, 10, 13, 22, 18, 15, 11, -14, | |
-23, -15, 2, 0, 2, 0, -23, -20, | |
-74, -23, -26, -24, -19, -35, -22, -69 | |
], | |
'B': [ | |
-59, -78, -82, -76, -23, -107, -37, -50, | |
-11, 20, 35, -42, -39, 31, 2, -22, | |
-9, 39, -32, 41, 52, -10, 28, -14, | |
25, 17, 20, 34, 26, 25, 15, 10, | |
13, 10, 17, 23, 17, 16, 0, 7, | |
14, 25, 24, 15, 8, 25, 20, 15, | |
19, 20, 11, 6, 7, 6, 20, 16, | |
-7, 2, -15, -12, -14, -15, -10, -10 | |
], | |
'R': [ | |
35, 29, 33, 4, 37, 33, 56, 50, | |
55, 29, 56, 67, 55, 62, 34, 60, | |
19, 35, 28, 33, 45, 27, 25, 15, | |
0, 5, 16, 13, 18, -4, -9, -6, | |
-28, -35, -16, -21, -13, -29, -46, -30, | |
-42, -28, -42, -25, -25, -35, -26, -46, | |
-53, -38, -31, -26, -29, -43, -44, -53, | |
-30, -24, -18, 5, -2, -18, -31, -32 | |
], | |
'Q': [ | |
6, 1, -8, -104, 69, 24, 88, 26, | |
14, 32, 60, -10, 20, 76, 57, 24, | |
-2, 43, 32, 60, 72, 63, 43, 2, | |
1, -16, 22, 17, 25, 20, -13, -6, | |
-14, -15, -2, -5, -1, -10, -20, -22, | |
-30, -6, -13, -11, -16, -11, -16, -27, | |
-36, -18, 0, -19, -15, -15, -21, -38, | |
-39, -30, -31, -13, -31, -36, -34, -42 | |
], | |
'K': [ | |
4, 54, 47, -99, -99, 60, 83, -62, | |
-32, 10, 55, 56, 56, 55, 10, 3, | |
-62, 12, -57, 44, -67, 28, 37, -31, | |
-55, 50, 11, -4, -19, 13, 0, -49, | |
-55, -43, -52, -28, -51, -47, -8, -50, | |
-47, -42, -43, -79, -64, -32, -29, -32, | |
-4, 3, -14, -50, -57, -18, 13, 4, | |
17, 30, -3, -14, 6, -1, 40, 18 | |
] | |
}; | |
// Function to parse a FEN string and calculate the advantage | |
function calculateAdvantage(fen) { | |
const pieceValues = { | |
'P': piece['P'], 'N': piece['N'], 'B': piece['B'], | |
'R': piece['R'], 'Q': piece['Q'], 'K': piece['K'], | |
'p': piece['P'], 'n': piece['N'], 'b': piece['B'], | |
'r': piece['R'], 'q': piece['Q'], 'k': piece['K'] | |
}; | |
// Convert FEN to 8x8 board | |
const board = fen.split(' ')[0] | |
.split('/') | |
.map(row => row.replace(/(\d)/g, d => ' '.repeat(Number(d))) // Replace numbers with spaces | |
.split('')) // Split into characters | |
.flat(); // Flatten into a single array | |
let materialWhite = 0; | |
let materialBlack = 0; | |
let positionWhite = 0; | |
let positionBlack = 0; | |
for (let i = 0; i < board.length; i++) { | |
const square = board[i]; | |
if (square !== ' ') { | |
const value = pieceValues[square]; | |
const pstValue = getPstValue(square, i); | |
if (square === square.toUpperCase()) { | |
materialWhite += value; | |
positionWhite += pstValue; | |
} else { | |
materialBlack += value; | |
positionBlack += pstValue; | |
} | |
} | |
} | |
const materialAdvantage = materialWhite - materialBlack; | |
const positionAdvantage = positionWhite - positionBlack; | |
const combinedAdvantage = materialAdvantage + positionAdvantage; | |
return combinedAdvantage; | |
} | |
// Get PST value based on piece and position index | |
function getPstValue(piece, squareIndex) { | |
// Index adjustment for black pieces | |
const adjustedIndex = piece === piece.toUpperCase() ? squareIndex : 63 - squareIndex; | |
return pst[piece.toUpperCase()][adjustedIndex]; | |
} | |
// Test FEN | |
const fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'; | |
console.log(calculateAdvantage(fen)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment