Squashed commit of the following:

commit c769a4d3bfbd22804ea4eeb324e7afeaaaa55f82
Author: GuillaumeSD <gsd.lfny@gmail.com>
Date:   Fri Apr 12 02:49:21 2024 +0200

    style : capturedPieces UI final touches

commit 838ad95bfa1746a3a8de38ace2520fe93ec6a0af
Author: GuillaumeSD <gsd.lfny@gmail.com>
Date:   Thu Apr 11 03:00:01 2024 +0200

    feat : captured pieces wip

commit e4bb4dcc346aa5dea5527fef4389b01673645e76
Merge: 785dba2 e9e772c
Author: GuillaumeSD <gsd.lfny@gmail.com>
Date:   Thu Apr 11 01:15:31 2024 +0200

    Merge branch 'main' into feat/add-board-captured-pieces

commit 785dba28509ac04655d4cab736a18a821ca52433
Author: GuillaumeSD <gsd.lfny@gmail.com>
Date:   Tue Apr 9 02:25:29 2024 +0200

    feat : add board captured pieces init
This commit is contained in:
GuillaumeSD
2024-04-12 02:49:48 +02:00
parent e9e772ca81
commit daaf145fa9
6 changed files with 211 additions and 6 deletions

View File

@@ -267,3 +267,31 @@ export const isCheck = (fen: string): boolean => {
const game = new Chess(fen);
return game.inCheck();
};
export const getCapturedPieces = (
fen: string,
color: Color
): Record<string, number | undefined> => {
const capturedPieces: Record<string, number | undefined> = {};
if (color === Color.White) {
capturedPieces.p = 8;
capturedPieces.r = 2;
capturedPieces.n = 2;
capturedPieces.b = 2;
capturedPieces.q = 1;
} else {
capturedPieces.P = 8;
capturedPieces.R = 2;
capturedPieces.N = 2;
capturedPieces.B = 2;
capturedPieces.Q = 1;
}
const fenPiecePlacement = fen.split(" ")[0];
for (const piece of Object.keys(capturedPieces)) {
const count = fenPiecePlacement.match(new RegExp(piece, "g"))?.length;
if (count) capturedPieces[piece] = (capturedPieces[piece] ?? 0) - count;
}
return capturedPieces;
};