File size: 9,568 Bytes
100a6dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# chess_engine/pieces.py

import chess
from typing import Dict, List, Optional, Tuple
from enum import Enum

class PieceType(Enum):
    PAWN = chess.PAWN
    KNIGHT = chess.KNIGHT
    BISHOP = chess.BISHOP
    ROOK = chess.ROOK
    QUEEN = chess.QUEEN
    KING = chess.KING

class PieceColor(Enum):
    WHITE = chess.WHITE
    BLACK = chess.BLACK

class ChessPiece:
    """Represents a chess piece with its properties and behaviors"""
    
    # Unicode symbols for pieces
    UNICODE_PIECES = {
        chess.WHITE: {
            chess.PAWN: "♙", chess.KNIGHT: "♘", chess.BISHOP: "♗",
            chess.ROOK: "♖", chess.QUEEN: "♕", chess.KING: "♔"
        },
        chess.BLACK: {
            chess.PAWN: "♟", chess.KNIGHT: "♞", chess.BISHOP: "♝",
            chess.ROOK: "♜", chess.QUEEN: "♛", chess.KING: "♚"
        }
    }
    
    # Piece values for evaluation
    PIECE_VALUES = {
        chess.PAWN: 1,
        chess.KNIGHT: 3,
        chess.BISHOP: 3,
        chess.ROOK: 5,
        chess.QUEEN: 9,
        chess.KING: 0  # King is invaluable
    }
    
    def __init__(self, piece_type: chess.PieceType, color: chess.Color):
        self.piece_type = piece_type
        self.color = color
        self.piece = chess.Piece(piece_type, color)
    
    @property
    def symbol(self) -> str:
        """Get the piece symbol (uppercase for white, lowercase for black)"""
        return self.piece.symbol()
    
    @property
    def unicode_symbol(self) -> str:
        """Get the Unicode symbol for the piece"""
        return self.UNICODE_PIECES[self.color][self.piece_type]
    
    @property
    def value(self) -> int:
        """Get the piece value"""
        return self.PIECE_VALUES[self.piece_type]
    
    @property
    def name(self) -> str:
        """Get the piece name"""
        piece_names = {
            chess.PAWN: "Pawn",
            chess.KNIGHT: "Knight",
            chess.BISHOP: "Bishop",
            chess.ROOK: "Rook",
            chess.QUEEN: "Queen",
            chess.KING: "King"
        }
        return piece_names[self.piece_type]
    
    @property
    def color_name(self) -> str:
        """Get the color name"""
        return "White" if self.color == chess.WHITE else "Black"
    
    def __str__(self) -> str:
        return f"{self.color_name} {self.name}"
    
    def __repr__(self) -> str:
        return f"ChessPiece({self.piece_type}, {self.color})"

class PieceManager:
    """Manages piece-related operations and utilities"""
    
    @staticmethod
    def create_piece(piece_type: str, color: str) -> Optional[ChessPiece]:
        """
        Create a piece from string representations
        
        Args:
            piece_type: 'pawn', 'knight', 'bishop', 'rook', 'queen', 'king'
            color: 'white' or 'black'
            
        Returns:
            ChessPiece instance or None if invalid input
        """
        piece_type_map = {
            'pawn': chess.PAWN,
            'knight': chess.KNIGHT,
            'bishop': chess.BISHOP,
            'rook': chess.ROOK,
            'queen': chess.QUEEN,
            'king': chess.KING
        }
        
        color_map = {
            'white': chess.WHITE,
            'black': chess.BLACK
        }
        
        if piece_type.lower() not in piece_type_map or color.lower() not in color_map:
            return None
            
        return ChessPiece(
            piece_type_map[piece_type.lower()],
            color_map[color.lower()]
        )
    
    @staticmethod
    def get_piece_moves(board: chess.Board, square: str) -> List[str]:
        """
        Get all possible moves for a piece at given square
        
        Args:
            board: Chess board instance
            square: Square in algebraic notation
            
        Returns:
            List of destination squares in algebraic notation
        """
        try:
            square_index = chess.parse_square(square)
            piece = board.piece_at(square_index)
            
            if piece is None:
                return []
            
            moves = []
            for move in board.legal_moves:
                if move.from_square == square_index:
                    moves.append(chess.square_name(move.to_square))
            
            return moves
            
        except ValueError:
            return []
    
    @staticmethod
    def get_piece_attacks(board: chess.Board, square: str) -> List[str]:
        """
        Get all squares attacked by piece at given square
        
        Args:
            board: Chess board instance
            square: Square in algebraic notation
            
        Returns:
            List of attacked squares in algebraic notation
        """
        try:
            square_index = chess.parse_square(square)
            piece = board.piece_at(square_index)
            
            if piece is None:
                return []
            
            attacks = []
            for target_square in chess.SQUARES:
                if board.is_attacked_by(piece.color, target_square):
                    # Check if this specific piece is doing the attacking
                    # This is a simplified check - in a real game you might need more sophisticated logic
                    attacks.append(chess.square_name(target_square))
            
            return attacks
            
        except ValueError:
            return []
    
    @staticmethod
    def get_material_count(board: chess.Board) -> Dict[str, Dict[str, int]]:
        """
        Get material count for both sides
        
        Args:
            board: Chess board instance
            
        Returns:
            Dictionary with material counts for white and black
        """
        white_pieces = {'pawn': 0, 'knight': 0, 'bishop': 0, 'rook': 0, 'queen': 0, 'king': 0}
        black_pieces = {'pawn': 0, 'knight': 0, 'bishop': 0, 'rook': 0, 'queen': 0, 'king': 0}
        
        piece_names = {
            chess.PAWN: 'pawn',
            chess.KNIGHT: 'knight',
            chess.BISHOP: 'bishop',
            chess.ROOK: 'rook',
            chess.QUEEN: 'queen',
            chess.KING: 'king'
        }
        
        for square in chess.SQUARES:
            piece = board.piece_at(square)
            if piece:
                piece_name = piece_names[piece.piece_type]
                if piece.color == chess.WHITE:
                    white_pieces[piece_name] += 1
                else:
                    black_pieces[piece_name] += 1
        
        return {
            'white': white_pieces,
            'black': black_pieces
        }
    
    @staticmethod
    def get_material_value(board: chess.Board) -> Dict[str, int]:
        """
        Get total material value for both sides
        
        Args:
            board: Chess board instance
            
        Returns:
            Dictionary with material values for white and black
        """
        white_value = 0
        black_value = 0
        
        for square in chess.SQUARES:
            piece = board.piece_at(square)
            if piece:
                value = ChessPiece.PIECE_VALUES[piece.piece_type]
                if piece.color == chess.WHITE:
                    white_value += value
                else:
                    black_value += value
        
        return {
            'white': white_value,
            'black': black_value,
            'advantage': white_value - black_value
        }
    
    @staticmethod
    def get_piece_list(board: chess.Board) -> Dict[str, List[Dict[str, str]]]:
        """
        Get list of all pieces on the board
        
        Args:
            board: Chess board instance
            
        Returns:
            Dictionary with lists of white and black pieces
        """
        white_pieces = []
        black_pieces = []
        
        for square in chess.SQUARES:
            piece = board.piece_at(square)
            if piece:
                piece_info = {
                    'type': chess.piece_name(piece.piece_type),
                    'square': chess.square_name(square),
                    'symbol': piece.symbol(),
                    'unicode': ChessPiece.UNICODE_PIECES[piece.color][piece.piece_type]
                }
                
                if piece.color == chess.WHITE:
                    white_pieces.append(piece_info)
                else:
                    black_pieces.append(piece_info)
        
        return {
            'white': white_pieces,
            'black': black_pieces
        }
    
    @staticmethod
    def is_promotion_move(board: chess.Board, move_str: str) -> bool:
        """
        Check if a move is a pawn promotion
        
        Args:
            board: Chess board instance
            move_str: Move in UCI notation
            
        Returns:
            True if the move is a promotion
        """
        try:
            move = chess.Move.from_uci(move_str)
            return move.promotion is not None
        except ValueError:
            return False
    
    @staticmethod
    def get_promotion_pieces() -> List[Dict[str, str]]:
        """
        Get available promotion pieces
        
        Returns:
            List of promotion piece options
        """
        return [
            {'type': 'queen', 'symbol': 'Q', 'name': 'Queen'},
            {'type': 'rook', 'symbol': 'R', 'name': 'Rook'},
            {'type': 'bishop', 'symbol': 'B', 'name': 'Bishop'},
            {'type': 'knight', 'symbol': 'N', 'name': 'Knight'}
        ]