File size: 13,777 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# chess_engine/api/game_controller.py

import chess
from typing import Dict, List, Optional, Tuple, Any
from enum import Enum
from dataclasses import dataclass

from chess_engine.board import ChessBoard, MoveResult, GameState
from chess_engine.ai.stockfish_wrapper import StockfishWrapper, DifficultyLevel
from chess_engine.ai.evaluation import ChessEvaluator
from chess_engine.promotion import PromotionMoveHandler

@dataclass
class GameOptions:
    """Game configuration options"""
    player_color: chess.Color = chess.WHITE
    difficulty: DifficultyLevel = DifficultyLevel.MEDIUM
    time_limit: float = 1.0  # seconds for AI to think
    use_opening_book: bool = True
    enable_analysis: bool = True
    stockfish_path: Optional[str] = None

class GameController:
    """

    Main game controller that handles game flow, user moves, and AI responses

    """
    
    def __init__(self, options: Optional[GameOptions] = None):
        """

        Initialize game controller

        

        Args:

            options: Game configuration options

        """
        self.options = options or GameOptions()
        self.board = ChessBoard()
        self.engine = StockfishWrapper(stockfish_path=self.options.stockfish_path)
        self.evaluator = ChessEvaluator()
        self.game_active = False
        self.last_analysis = None
        
    def start_new_game(self, options: Optional[GameOptions] = None) -> Dict[str, Any]:
        """

        Start a new game

        

        Args:

            options: Game options (optional, uses current options if None)

            

        Returns:

            Game state information

        """
        if options:
            self.options = options
            
        # Reset board
        self.board.reset_board()
        
        # Initialize engine
        if not self.engine.is_initialized:
            engine_initialized = self.engine.initialize()
            if not engine_initialized:
                return {
                    "status": "error",
                    "message": "Failed to initialize chess engine",
                    "board_state": self.board.get_board_state()
                }
        
        # Set difficulty
        self.engine.set_difficulty_level(self.options.difficulty)
        
        self.game_active = True
        
        # If AI plays white, make first move
        if self.options.player_color == chess.BLACK:
            ai_move = self._get_ai_move()
            if ai_move:
                self.board.make_move(ai_move)
        
        return {
            "status": "success",
            "message": "New game started",
            "board_state": self.board.get_board_state(),
            "player_color": "white" if self.options.player_color == chess.WHITE else "black"
        }
    
    def make_player_move(self, move_str: str) -> Dict[str, Any]:
        """

        Process a player's move

        

        Args:

            move_str: Move in UCI notation (e.g., 'e2e4', 'e7e8q') or SAN (e.g., 'e4', 'e8=Q')

            

        Returns:

            Response with move result and updated game state

        """
        if not self.game_active:
            return {
                "status": "error",
                "message": "No active game",
                "board_state": self.board.get_board_state()
            }
        
        # Check if it's player's turn
        if self.board.board.turn != self.options.player_color:
            return {
                "status": "error",
                "message": "Not your turn",
                "board_state": self.board.get_board_state()
            }
        
        # Make the move
        result, move_info = self.board.make_move(move_str)
        
        # Handle promotion requirement
        if result == MoveResult.INVALID and move_info and move_info.promotion_required:
            return {
                "status": "promotion_required",
                "message": "Pawn promotion requires piece selection",
                "promotion_details": {
                    "from": move_info.uci[:2],
                    "to": move_info.uci[2:4],
                    "available_pieces": ["queen", "rook", "bishop", "knight"]
                },
                "board_state": self.board.get_board_state()
            }
        
        if result != MoveResult.VALID:
            return {
                "status": "error",
                "message": f"Invalid move: {move_str}",
                "board_state": self.board.get_board_state()
            }
        
        # Check if game is over after player's move
        game_state = self._check_game_state()
        if game_state:
            return game_state
        
        # Make AI move
        ai_move = self._get_ai_move()
        if ai_move:
            self.board.make_move(ai_move)
            
        # Check if game is over after AI's move
        game_state = self._check_game_state()
        if game_state:
            return game_state
        
        # Analyze position if enabled
        analysis = None
        if self.options.enable_analysis:
            analysis = self._analyze_position()
            self.last_analysis = analysis
        
        return {
            "status": "success",
            "player_move": move_str,
            "ai_move": ai_move,
            "board_state": self.board.get_board_state(),
            "analysis": analysis
        }
    
    def _get_ai_move(self) -> Optional[str]:
        """

        Get AI's move using Stockfish

        

        Returns:

            Move in UCI notation or None if error

        """
        return self.engine.get_best_move(
            self.board.board, 
            time_limit=self.options.time_limit
        )
    
    def _check_game_state(self) -> Optional[Dict[str, Any]]:
        """

        Check if game is over

        

        Returns:

            Game result dict if game is over, None otherwise

        """
        board_state = self.board.get_board_state()
        game_state = board_state["game_state"]
        
        if game_state in [GameState.CHECKMATE, GameState.STALEMATE, GameState.DRAW]:
            self.game_active = False
            
            result = "draw"
            winner = None
            
            if game_state == GameState.CHECKMATE:
                # Winner is the opposite of who's turn it is
                winner = "black" if self.board.board.turn == chess.WHITE else "white"
                result = f"{winner}_win"
            
            return {
                "status": "game_over",
                "result": result,
                "winner": winner,
                "reason": game_state.value,
                "board_state": board_state
            }
        
        return None
    
    def _analyze_position(self) -> Dict[str, Any]:
        """

        Analyze current position

        

        Returns:

            Analysis data

        """
        # Get evaluation from our evaluator
        evaluation = self.evaluator.evaluate_position(self.board.board)
        
        # Get Stockfish evaluation
        stockfish_eval = self.engine.evaluate_position(self.board.board)
        
        # Get best moves with evaluation
        best_moves = self.engine.get_legal_moves_with_evaluation(self.board.board)[:3]
        
        return {
            "evaluation": {
                "total": evaluation.total_score / 100,  # Convert to pawns
                "material": evaluation.material_score / 100,
                "positional": evaluation.positional_score / 100,
                "safety": evaluation.safety_score / 100,
                "mobility": evaluation.mobility_score / 100,
                "pawn_structure": evaluation.pawn_structure_score / 100,
                "endgame": evaluation.endgame_score / 100,
                "white_advantage": evaluation.white_advantage / 100,
                "stockfish": stockfish_eval
            },
            "best_moves": best_moves
        }
    
    def get_hint(self) -> Dict[str, Any]:
        """

        Get a hint for the current position

        

        Returns:

            Hint information

        """
        if not self.game_active:
            return {
                "status": "error",
                "message": "No active game"
            }
        
        # Get best move from engine
        best_move = self._get_ai_move()
        
        if not best_move:
            return {
                "status": "error",
                "message": "Could not generate hint"
            }
        
        # Get move explanation
        explanation = self._get_move_explanation(best_move)
        
        return {
            "status": "success",
            "hint": best_move,
            "explanation": explanation,
            "board_state": self.board.get_board_state()
        }
    
    def _get_move_explanation(self, move_str: str) -> str:
        """

        Generate a simple explanation for a move

        

        Args:

            move_str: Move in UCI notation

            

        Returns:

            Human-readable explanation

        """
        try:
            move = chess.Move.from_uci(move_str)
            board = self.board.board
            
            from_square = chess.square_name(move.from_square)
            to_square = chess.square_name(move.to_square)
            
            piece = board.piece_at(move.from_square)
            if not piece:
                return "Unknown move"
                
            piece_name = chess.piece_name(piece.piece_type).capitalize()
            
            # Check if move is a capture
            capture = ""
            if board.is_capture(move):
                captured_piece = board.piece_at(move.to_square)
                if captured_piece:
                    captured_name = chess.piece_name(captured_piece.piece_type).capitalize()
                    capture = f", capturing {captured_name}"
                else:
                    # En passant
                    capture = ", capturing Pawn en passant"
            
            # Check if move gives check
            check = ""
            if board.gives_check(move):
                check = ", giving check"
            
            # Check if castling
            if board.is_castling(move):
                if chess.square_file(move.to_square) > chess.square_file(move.from_square):
                    return "Kingside castling"
                else:
                    return "Queenside castling"
            
            # Check if promotion
            promotion = ""
            if move.promotion:
                promoted_piece = chess.piece_name(move.promotion).capitalize()
                promotion = f", promoting to {promoted_piece}"
            elif piece.piece_type == chess.PAWN and self.board.is_promotion_move(from_square, to_square):
                # This is a promotion move but piece not specified
                promotion = ", promotion required"
            
            return f"{piece_name} from {from_square} to {to_square}{capture}{promotion}{check}"
            
        except Exception:
            return "Move analysis not available"
    
    def undo_move(self, count: int = 2) -> Dict[str, Any]:
        """

        Undo moves (both player and AI)

        

        Args:

            count: Number of half-moves to undo (default 2 for one full move)

            

        Returns:

            Updated game state

        """
        if not self.game_active:
            return {
                "status": "error",
                "message": "No active game",
                "board_state": self.board.get_board_state()
            }
        
        success = True
        for _ in range(count):
            if not self.board.undo_move():
                success = False
                break
        
        return {
            "status": "success" if success else "partial",
            "message": f"Undid {count} moves" if success else "Could not undo all requested moves",
            "board_state": self.board.get_board_state()
        }
    
    def resign(self) -> Dict[str, Any]:
        """

        Resign the current game

        

        Returns:

            Game result

        """
        if not self.game_active:
            return {
                "status": "error",
                "message": "No active game"
            }
        
        self.game_active = False
        winner = "black" if self.options.player_color == chess.WHITE else "white"
        
        return {
            "status": "game_over",
            "result": f"{winner}_win",
            "winner": winner,
            "reason": "resignation",
            "board_state": self.board.get_board_state()
        }
    
    def get_game_state(self) -> Dict[str, Any]:
        """

        Get current game state

        

        Returns:

            Game state information

        """
        board_state = self.board.get_board_state()
        
        return {
            "status": "active" if self.game_active else "inactive",
            "player_color": "white" if self.options.player_color == chess.WHITE else "black",
            "board_state": board_state,
            "difficulty": self.options.difficulty.name,
            "last_analysis": self.last_analysis
        }
    
    def close(self):
        """Clean up resources"""
        if self.engine:
            self.engine.close()