Skip to content

An In-House Chess Engine designed to help Beginners, Intermediate, and advanced Chess Players with Chess Analysis

License

Notifications You must be signed in to change notification settings

Bempong-Sylvester-Obese/Chess-Engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

โ™Ÿ๏ธ BCF Chess Engine

Python License Chess ML

A comprehensive Python chess engine with machine learning-powered analysis, multiple interfaces, and real-time evaluation

Features โ€ข Installation โ€ข Usage โ€ข Architecture โ€ข API โ€ข Contributing


โš ๏ธ Notice: Web Application in Development
The web interface for this chess engine is currently under active development. While the core engine, GUI, CLI, and API are fully functional, the web application may have limited features or be temporarily unavailable. We recommend using the Pygame GUI or CLI for the best experience while the web interface is being completed.


๐Ÿš€ Features

๐Ÿง  Machine Learning Enhanced Analysis

  • ML-Powered Evaluation: Combines traditional chess evaluation with trained machine learning models
  • Feature Extraction: Advanced position analysis including material, mobility, king safety, and pawn structure
  • Predictive Move Suggestions: AI-driven move recommendations with confidence scores
  • Real-time Learning: Continuously improves analysis through synthetic data training

๐ŸŽฎ Multiple User Interfaces

  • ๐ŸŽจ Pygame GUI: Beautiful graphical interface with real-time evaluation bar and move history
  • ๐Ÿ’ป Command Line Interface: Lightweight CLI for quick analysis and scripting
  • ๐ŸŒ Web API: RESTful API for integration with web applications
  • ๐Ÿ“Š Analysis Dashboard: Comprehensive position analysis and visualization tools

โ™Ÿ๏ธ Advanced Chess Features

  • Real-time Position Evaluation: Live analysis with numerical and visual feedback
  • Blunder Detection: Identifies poor moves and suggests improvements
  • Move Suggestion Engine: Top 5 best moves with detailed explanations
  • Game State Analysis: Check, checkmate, stalemate, and draw detection
  • Opening Database: Support for opening theory and analysis

๐Ÿ”ง Developer Tools

  • Training Pipeline: Complete ML model training and validation system
  • Data Analysis: Comprehensive training data analysis and visualization
  • Testing Suite: Extensive unit tests and integration tests
  • Performance Monitoring: Real-time engine performance metrics

๐Ÿ“ฆ Installation

Prerequisites

  • Python 3.7+
  • Stockfish Chess Engine (optional, for enhanced analysis)

Quick Setup

  1. Clone the repository

    git clone https://github.com/Bempong-Sylvester-Obese/Chess-Engine.git
    cd Chess-Engine
  2. Install dependencies

    pip install -r requirements.txt
  3. Train the ML model (optional)

    python Scripts/train_engine.py
  4. Run the main application

    python main.py

๐ŸŽฏ Usage

๐ŸŽฎ Graphical Interface (Recommended)

Launch Pygame-based chess interface:

python main.py

Features:

  • Interactive chessboard with piece highlighting
  • Real-time evaluation bar showing position strength
  • Move history with last move highlighting
  • Keyboard shortcuts (R: Reset, U: Undo)

๐Ÿ’ป Command Line Interface

For quick analysis and scripting:

python UI/cli.py

Features:

  • ASCII chessboard display
  • UCI move input format
  • Position evaluation output
  • Game state tracking

๐ŸŒ Web API

Start the FastAPI server:

python API/server.py

Available Endpoints:

  • POST /move - Make a move
  • GET /state - Get current game state
  • GET /best-move - Get engine's best move
  • POST /reset - Reset the game

๐Ÿ”ฌ Advanced Analysis

Run comprehensive analysis tools:

# Train ML model
python Scripts/train_engine.py

# Analyze training data
python Scripts/analyze_training_data.py

# Generate synthetic data
python Scripts/generate_synthetic_data.py

# Visualize analysis
python Scripts/visualize_analysis.py

๐Ÿ—๏ธ Architecture

System Overview

graph TB
    subgraph "User Interfaces"
        GUI[๐ŸŽจ Pygame GUI]
        CLI[๐Ÿ’ป Command Line]
        API[๐ŸŒ Web API]
        WEB[๐Ÿ“Š Web Dashboard]
    end
    
    subgraph "Core Engine"
        ENGINE[๐Ÿง  Enhanced Engine]
        EVAL[๐Ÿ“Š Evaluation]
        SUGGEST[๐Ÿ’ก Move Suggester]
        BOARD[โ™Ÿ๏ธ Board Logic]
    end
    
    subgraph "Machine Learning"
        ML[๐Ÿค– ML Model]
        FEATURES[๐Ÿ” Feature Extraction]
        TRAINING[๐Ÿ“ˆ Training Pipeline]
        DATA[๐Ÿ“Š Data Analysis]
    end
    
    subgraph "Data & Storage"
        MODEL[๐Ÿ’พ Trained Model]
        DATASET[๐Ÿ“ Training Data]
        PLOTS[๐Ÿ“ˆ Analysis Plots]
    end
    
    GUI --> ENGINE
    CLI --> ENGINE
    API --> ENGINE
    WEB --> ENGINE
    
    ENGINE --> EVAL
    ENGINE --> SUGGEST
    ENGINE --> BOARD
    
    EVAL --> ML
    SUGGEST --> ML
    ML --> FEATURES
    FEATURES --> TRAINING
    TRAINING --> DATA
    
    ML --> MODEL
    TRAINING --> DATASET
    DATA --> PLOTS
    
    style GUI fill:#e1f5fe
    style CLI fill:#f3e5f5
    style API fill:#e8f5e8
    style WEB fill:#fff3e0
    style ENGINE fill:#ffebee
    style ML fill:#f1f8e9
Loading

Data Flow

flowchart LR
    subgraph "Input"
        POSITION[Chess Position]
        MOVE[Player Move]
    end
    
    subgraph "Processing"
        FEATURES[Extract Features]
        EVAL[Evaluate Position]
        ML[ML Analysis]
        SUGGEST[Generate Suggestions]
    end
    
    subgraph "Output"
        EVAL_BAR[Evaluation Bar]
        MOVES[Best Moves]
        ANALYSIS[Position Analysis]
    end
    
    POSITION --> FEATURES
    MOVE --> FEATURES
    
    FEATURES --> EVAL
    FEATURES --> ML
    
    EVAL --> SUGGEST
    ML --> SUGGEST
    
    SUGGEST --> MOVES
    EVAL --> EVAL_BAR
    ML --> ANALYSIS
    
    style POSITION fill:#e3f2fd
    style MOVE fill:#e3f2fd
    style FEATURES fill:#f3e5f5
    style EVAL fill:#e8f5e8
    style ML fill:#fff3e0
    style SUGGEST fill:#ffebee
    style EVAL_BAR fill:#e1f5fe
    style MOVES fill:#f1f8e9
    style ANALYSIS fill:#fce4ec
Loading

Training Pipeline

flowchart TD
    subgraph "Data Collection"
        GAMES[Chess Games]
        SYNTHETIC[Synthetic Data]
        ANALYSIS[Position Analysis]
    end
    
    subgraph "Feature Engineering"
        MATERIAL[Material Count]
        MOBILITY[Piece Mobility]
        KING_SAFETY[King Safety]
        PAWN_STRUCT[Pawn Structure]
    end
    
    subgraph "Model Training"
        FEATURES[Feature Matrix]
        LABELS[Position Labels]
        TRAIN[Train Model]
        VALIDATE[Validate Model]
    end
    
    subgraph "Deployment"
        SAVE[Save Model]
        INTEGRATE[Integrate with Engine]
        MONITOR[Monitor Performance]
    end
    
    GAMES --> FEATURES
    SYNTHETIC --> FEATURES
    ANALYSIS --> FEATURES
    
    MATERIAL --> FEATURES
    MOBILITY --> FEATURES
    KING_SAFETY --> FEATURES
    PAWN_STRUCT --> FEATURES
    
    FEATURES --> TRAIN
    LABELS --> TRAIN
    TRAIN --> VALIDATE
    VALIDATE --> SAVE
    SAVE --> INTEGRATE
    INTEGRATE --> MONITOR
    
    style GAMES fill:#e8f5e8
    style SYNTHETIC fill:#e8f5e8
    style ANALYSIS fill:#e8f5e8
    style FEATURES fill:#fff3e0
    style TRAIN fill:#ffebee
    style SAVE fill:#e1f5fe
Loading

Component Interaction

sequenceDiagram
    participant User
    participant GUI
    participant Engine
    participant ML
    participant Board
    
    User->>GUI: Make Move
    GUI->>Engine: Process Move
    Engine->>Board: Update Position
    Board->>Engine: New Position
    
    Engine->>ML: Request Analysis
    ML->>Engine: Evaluation & Suggestions
    
    Engine->>GUI: Return Results
    GUI->>User: Display Analysis
    
    Note over Engine,ML: Real-time evaluation
    Note over GUI,User: Visual feedback
Loading
Chess-Engine/
โ”œโ”€โ”€ ๐ŸŽฎ main.py                 # Main Pygame GUI application
โ”œโ”€โ”€ ๐Ÿง  Engine/                 # Core chess engine components
โ”‚   โ”œโ”€โ”€ enhanced_engine.py     # ML-enhanced chess engine
โ”‚   โ”œโ”€โ”€ chess_suggester.py     # Move suggestion engine
โ”‚   โ”œโ”€โ”€ evaluation.py          # Position evaluation
โ”‚   โ””โ”€โ”€ board.py              # Board representation
โ”œโ”€โ”€ ๐ŸŒ API/                    # Web API components
โ”‚   โ”œโ”€โ”€ server.py             # FastAPI server
โ”‚   โ””โ”€โ”€ endpoints.py          # API endpoints
โ”œโ”€โ”€ ๐Ÿ’ป UI/                     # User interfaces
โ”‚   โ”œโ”€โ”€ cli.py                # Command line interface
โ”‚   โ””โ”€โ”€ chesswebapp/          # Web interface assets
โ”œโ”€โ”€ ๐Ÿ”ฌ Scripts/                # Analysis and training tools
โ”‚   โ”œโ”€โ”€ train_engine.py       # ML model training
โ”‚   โ”œโ”€โ”€ analyze_training_data.py
โ”‚   โ””โ”€โ”€ visualize_analysis.py
โ”œโ”€โ”€ ๐Ÿ“Š Data/                   # Training data and models
โ”‚   โ”œโ”€โ”€ trained_model.pkl     # Trained ML model
โ”‚   โ””โ”€โ”€ training_data.csv     # Training dataset
โ””โ”€โ”€ ๐Ÿงช Tests/                  # Test suite

๐Ÿ”Œ API Reference

Core Engine API

from Engine.enhanced_engine import EnhancedChessSuggester

# Initialize engine
engine = EnhancedChessSuggester()

# Get move suggestions
suggestions = engine.get_move_suggestions(board)
print(f"Evaluation: {suggestions['current_evaluation']}")
print(f"Best moves: {suggestions['suggested_moves']}")

Web API Endpoints

# Make a move
curl -X POST "http://localhost:8000/move" \
     -H "Content-Type: application/json" \
     -d '{"move": "e2e4"}'

# Get game state
curl "http://localhost:8000/state"

# Get best move
curl "http://localhost:8000/best-move?depth=15"

๐Ÿงช Testing

Test Architecture

graph LR
    subgraph "Test Types"
        UNIT[Unit Tests]
        INTEGRATION[Integration Tests]
        API[API Tests]
        ML[ML Tests]
    end
    
    subgraph "Test Coverage"
        ENGINE[Engine Logic]
        EVAL[Evaluation]
        SUGGEST[Move Suggestions]
        GUI[User Interface]
    end
    
    UNIT --> ENGINE
    INTEGRATION --> EVAL
    API --> SUGGEST
    ML --> GUI
    
    style UNIT fill:#e8f5e8
    style INTEGRATION fill:#fff3e0
    style API fill:#ffebee
    style ML fill:#e1f5fe
Loading

Run the comprehensive test suite:

# Run all tests
pytest Tests/

# Run specific test modules
pytest Tests/test_engine.py
pytest Tests/test_api.py
pytest Tests/test_blunder.py

๐Ÿ“ˆ Performance

Performance Metrics

graph LR
    subgraph "Speed"
        EVAL_TIME[< 1s Evaluation]
        MOVE_GEN[Fast Move Generation]
        ML_INFERENCE[Quick ML Inference]
    end
    
    subgraph "Accuracy"
        ML_ACCURACY[70% ML Weight]
        TRADITIONAL[30% Traditional]
        COMBINED[Combined Analysis]
    end
    
    subgraph "Efficiency"
        MEMORY[Memory Optimized]
        SCALABLE[Scalable Design]
        MODULAR[Modular Architecture]
    end
    
    EVAL_TIME --> COMBINED
    MOVE_GEN --> COMBINED
    ML_INFERENCE --> COMBINED
    
    ML_ACCURACY --> COMBINED
    TRADITIONAL --> COMBINED
    
    COMBINED --> MEMORY
    COMBINED --> SCALABLE
    COMBINED --> MODULAR
    
    style EVAL_TIME fill:#e8f5e8
    style ML_ACCURACY fill:#fff3e0
    style MEMORY fill:#ffebee
Loading

The engine features:

  • Real-time Analysis: Sub-second position evaluation
  • ML Enhancement: 70% ML + 30% traditional evaluation
  • Memory Efficient: Optimized for large game trees
  • Scalable Architecture: Modular design for easy extension

๐Ÿค Contributing

Contribution Workflow

flowchart TD
    A[Fork Repository] --> B[Create Feature Branch]
    B --> C[Make Changes]
    C --> D[Run Tests]
    D --> E{Tests Pass?}
    E -->|No| F[Fix Issues]
    F --> D
    E -->|Yes| G[Commit Changes]
    G --> H[Push to Branch]
    H --> I[Create Pull Request]
    I --> J[Code Review]
    J --> K{Approved?}
    K -->|No| L[Address Feedback]
    L --> J
    K -->|Yes| M[Merge to Main]
    
    style A fill:#e8f5e8
    style M fill:#e8f5e8
    style E fill:#ffebee
    style K fill:#ffebee
Loading

We welcome contributions! Please see our contributing guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Setup

# Install development dependencies
pip install -r requirements.txt

# Run linting
black .
pylint Engine/ Tests/ Scripts/

# Run tests
pytest Tests/

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

Made with โ™Ÿ๏ธ and โค๏ธ by the BCF Chess Engine Team

Report Bug โ€ข Request Feature โ€ข Documentation

About

An In-House Chess Engine designed to help Beginners, Intermediate, and advanced Chess Players with Chess Analysis

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published