File size: 1,344 Bytes
3568151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import React from 'react'
import { ChessPiece as ChessPieceType } from '../types/chess'
import '../styles/ChessPiece.css'

export const ChessPiece: React.FC<{
  piece: ChessPieceType
  size: number
}> = ({ 
  piece, 
  size, 
}) => {

  const getPieceImagePath = () => {
    const color = piece.color === 'w' ? 'white' : 'dark'
    const orientation = 'upright'
    
    const pieceFileMap: { [key: string]: string } = {
      'k': piece.color === 'w' ? 'Chess_klt45.svg' : 'Chess_kdt45.svg',
      'q': piece.color === 'w' ? 'Chess_qlt45.svg' : 'Chess_qdt45.svg',
      'r': piece.color === 'w' ? 'Chess_rlt45.svg' : 'Chess_rdt45.svg',
      'b': piece.color === 'w' ? 'Chess_blt45.svg' : 'Chess_bdt45.svg',
      'n': piece.color === 'w' ? 'Chess_nlt45.svg' : 'Chess_ndt45.svg',
      'p': piece.color === 'w' ? 'Chess_plt45.svg' : 'Chess_pdt45.svg'
    }
    
    const fileName = pieceFileMap[piece.type]
    return `/assets/pieces/${color}/${orientation}/${fileName}`
  }

  return (
    <div
      className="chess-piece"
      style={{
        width: size,
        height: size
      }}
    >
      <img
        src={getPieceImagePath()}
        alt={`${piece.color === 'w' ? 'White' : 'Black'} ${piece.type}`}
        width={size}
        height={size}
        className="piece-image"
        draggable={false}
      />
    </div>
  )
}