@@ -20,6 +20,8 @@ import { Chess } from 'chess.ts'
2020type PieceType = 'p' | 'n' | 'b' | 'r' | 'q' | 'k'
2121type MaterialCount = Record < PieceType , number >
2222
23+ const PIECE_DISPLAY_ORDER : PieceType [ ] = [ 'p' , 'n' , 'b' , 'r' , 'q' ]
24+
2325const PIECE_VALUES : Record < string , number > = {
2426 p : 1 , // pawn
2527 n : 3 , // knight
@@ -131,6 +133,24 @@ export const PlayerInfo: React.FC<PlayerInfoProps> = ({
131133 // Get pieces captured by this player (pieces of opposite color that were captured)
132134 const myCapturedPieces =
133135 color === 'white' ? capturedPieces . black : capturedPieces . white
136+ const opponentCapturedPieces =
137+ color === 'white' ? capturedPieces . white : capturedPieces . black
138+
139+ const pieceDifference = useMemo ( ( ) => {
140+ const diff : Partial < Record < PieceType , number > > = { }
141+
142+ PIECE_DISPLAY_ORDER . forEach ( ( piece ) => {
143+ const myCount = myCapturedPieces ?. [ piece ] ?? 0
144+ const opponentCount = opponentCapturedPieces ?. [ piece ] ?? 0
145+ const net = myCount - opponentCount
146+
147+ if ( net > 0 ) {
148+ diff [ piece ] = net
149+ }
150+ } )
151+
152+ return diff
153+ } , [ myCapturedPieces , opponentCapturedPieces ] )
134154
135155 // Calculate net material advantage (white total - black total)
136156 const netAdvantage = materialAdvantage . white - materialAdvantage . black
@@ -165,10 +185,8 @@ export const PlayerInfo: React.FC<PlayerInfoProps> = ({
165185 const pieceGroups : React . JSX . Element [ ] = [ ]
166186
167187 // Order pieces by value (lowest to highest)
168- const orderedPieces = [ 'p' , 'n' , 'b' , 'r' , 'q' ]
169-
170- orderedPieces . forEach ( ( piece ) => {
171- const count = myCapturedPieces [ piece ] || 0
188+ PIECE_DISPLAY_ORDER . forEach ( ( piece ) => {
189+ const count = pieceDifference [ piece ] || 0
172190 if ( count > 0 ) {
173191 const piecesOfType : React . JSX . Element [ ] = [ ]
174192
0 commit comments