Skip to content

JamesPChess Game v2.0.0

Latest

Choose a tag to compare

@jperram92 jperram92 released this 22 Dec 23:05
· 4 commits to main since this release

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.json for 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:

  1. Check Moves: Puts opponent's king in check.
  2. Capturing Moves: Prioritizes high-value pieces.
  3. Safe Moves: Moves to non-threatened squares.
  4. 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:

  1. Updates the piece's position.
  2. Captures opponent's pieces if applicable.
  3. Refreshes the game state.
  4. 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, and Green.

4. Text-to-Speech (TTS) Integration

  • Enabled by Default: Uses pyttsx3 for 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 from captured_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 NamePurpose
ai_opening_move_whiteHandles AI's opening moves.
ai_move_blackAI mid-game logic.
record_moveTracks 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