Release Notes: Human vs AI Chess Game with Enhanced AI Functionality
1. Overview of the Code
This code creates an interactive chess game using Pygame where a human player competes against an AI opponent. The AI uses opening theory, move prioritization, and piece value assessments to determine its moves.
Key Features:
- Human vs AI gameplay: Turn-based system.
- Opening Theory: Classic chess openings like Ruy Lopez.
- Move Prioritization: Check moves, capturing moves, and safe moves.
- Visual and Audio Feedback: Text-to-speech (TTS) and visual move indicators.
- Dynamic UI: Theme selection, move history, and stats tracker.
2. AI Move Logic (How the AI Tracks and Makes Moves)
2.1 Opening Moves (Early Game Strategy)
- Opening Database: Uses
openings.jsonfor predefined sequences. - Move Matching: Matches human moves to openings.
- Transition to Mid-Game: Falls back to standard strategies when openings are exhausted.
Key Function: ai_opening_move_white(move)
- Matches the opponent's move with known openings.
- Returns the AI's corresponding move or falls back to defaults.
2.2 Mid-Game Strategies
AI prioritizes moves in this order:
- Check Moves: Puts opponent's king in check.
- Capturing Moves: Prioritizes high-value pieces.
- Safe Moves: Moves to non-threatened squares.
- Fallback Random Move: Picks a valid random move.
Key Function: ai_move_black()
- Scans all valid moves.
- Sorts moves into Check, Capturing, and Safe categories.
- Updates game state after the move.
2.3 Move Execution
When the AI selects a move:
- Updates the piece's position.
- Captures opponent's pieces if applicable.
- Refreshes the game state.
- Announces moves using TTS.
Key Function: record_move(player, piece, start_pos, end_pos)
- Tracks moves in
move_history. - Stores the last 5 moves.
3. Human Interaction and UI Features
- Move History Tracking: Displays the last five moves.
- Stats Tracking: Tracks overall wins in
stats.json. - Visual Indicators: Highlights valid moves and king under check.
- Pawn Promotion: Promotes pawns dynamically.
- Theme Selection: Switch between
Classic,Wood,Ocean, andGreen.
4. Text-to-Speech (TTS) Integration
- Enabled by Default: Uses
pyttsx3for speech. - Capture Announcements: Random buzzwords on captures.
- Toggle Functionality: Enable/Disable TTS with a button.
Key Functions:
speak_capture_message(): Announces captured pieces.get_random_captured_word(): Fetches words fromcaptured_words.json.
5. Event Handling and Game State Management
- Turn Management: Alternates between human and AI.
- Game Over Detection: Detects checkmate or forfeits.
- Restart Mechanism: Press ENTER to restart.
Key Function: draw_game_over()
6. Debugging Features
- Debug Mode: Enabled via
debug_opening_mode. - Manual Opening Selection: Test specific openings.
- AI Logging: Debug messages printed in the console.
7. Summary of Key Functions
| Function Name | Purpose |
|---|---|
ai_opening_move_white | Handles AI's opening moves. |
ai_move_black | AI mid-game logic. |
record_move | Tracks move history. |
8. Future Enhancements
- Improved AI depth for multi-turn planning.
- Difficulty settings for AI.
- Online multiplayer support.
9. Known Issues
- Pawn promotion misalignment on rapid clicks.
- Stalemate detection limitations.
- Detection of Check on Black King found