A sophisticated 3D chess game with AI capabilities that can learn and improve through self-play. Built as a standalone desktop application that runs locally without requiring a web browser or server.
- 3D Chess Board: Fully interactive 3D chess board with realistic piece models using OpenGL
- AI Opponent: Play against an AI that uses minimax algorithm with alpha-beta pruning
- AI Learning: Neural network-based evaluation that learns from games
- Pre-trained Model: Comes with initial training data from famous grandmaster games
- Automatic Training: AI automatically improves after games
- Flexible Training Modes:
- Train every game (ideal for self-play)
- Train every 5 games (default for regular play)
- Learn from Grandmaster Games: Load PGN files from famous games to train the AI
- Self-Play Mode: Watch the AI play against itself to train and improve
- Game Modes:
- Player vs AI
- AI vs AI (self-play)
- Save/Load Games: Import and export games in PGN (Portable Game Notation) format
- Model Persistence: Save and load trained AI models
- Interactive 3D Controls: Rotate, zoom, and interact with the board
- Standalone Executable: Can be built into a single .exe with all dependencies included
- GUI Framework: PyQt5
- 3D Graphics: PyOpenGL (OpenGL)
- Chess Logic: python-chess library
- AI/ML: TensorFlow/Keras for neural network
- Packaging: PyInstaller for creating standalone executables
- Format Support: PGN (Portable Game Notation)
- Python 3.8 or higher
- pip (Python package manager)
- OpenGL support (usually built into OS)
- Windows: Windows 7 or later with OpenGL support
- No Python installation required when using the built executable
-
Clone the repository:
git clone https://github.com/dotnetappdev/Chessai-.git cd Chessai- -
Install Python dependencies:
pip install -r requirements.txt
This will install:
- PyQt5 (GUI framework)
- PyOpenGL (3D graphics)
- python-chess (chess logic and PGN support)
- numpy (numerical computing)
- tensorflow (machine learning)
- pyinstaller (for building executables)
-
Run the application:
python main.py
-
Install dependencies (if not already done):
pip install -r requirements.txt
-
Build the executable:
pyinstaller ChessAI.spec
-
Find the executable:
- The built application will be in the
dist/ChessAI/directory - Run
ChessAI.exe(Windows) orChessAI(Linux/Mac) - All DLLs and dependencies are included in the dist folder
- You can copy the entire
dist/ChessAI/folder to any computer and run it
- The built application will be in the
-
Optional: Create a single-file executable:
pyinstaller --onefile --windowed main.py
This creates a single .exe file (larger, slower startup, but easier to distribute)
From Source:
python main.pyFrom Executable:
- Double-click
ChessAI.exein thedist/ChessAI/folder
- Select "Player vs AI" from the mode dropdown (default)
- Click on a piece to see legal moves (highlighted in green)
- Click on a highlighted square to make your move
- The AI will automatically respond after your move
- Select "AI vs AI" from the mode dropdown
- Check "Train after each self-play game" to enable training after every game (recommended)
- Click "Start Self-Play" to watch the AI play against itself
- Click "Stop Self-Play" to pause
- This mode is useful for generating training data and improving the AI quickly
- Rotate Camera: Right-click and drag to rotate the view around the board
- Zoom: Use mouse wheel to zoom in/out
- Select Piece: Left-click on a piece
- Make Move: Left-click on a legal move square (highlighted in green)
- Click "Save Game (PGN)" button
- Choose a location and filename
- Games are saved in standard PGN format
- Click "Load Game (PGN)" button
- Select a previously saved .pgn file
- The board will update to show the loaded game position
The AI uses a neural network to evaluate chess positions and can improve through training.
- The AI comes with initial training data from 173 positions extracted from famous grandmaster games
- This provides a solid starting point for the AI's learning
- To create a fully trained model with TensorFlow installed, run:
python pretrain_model.py
- Regular Play: AI trains after every 5 completed games
- Self-Play Mode: Enable "Train after each self-play game" checkbox to train after EVERY game
- Training uses positions encountered during play
- The model is automatically saved after training
- No manual intervention needed - just play!
- Play some games or run self-play mode to generate training data
- Click "Train AI (10 epochs)" to manually train the neural network
- The training data counter shows how many positions are available
- Training improves the AI's position evaluation
- Click "Learn from PGN File" button
- Select a PGN file containing chess games (e.g., from Chess.com, Lichess, or famous game databases)
- The AI will analyze all games in the file and learn from the positions
- Each game adds dozens of training positions
- Click "Train AI" after loading to apply the learning
Where to get PGN files:
- Use the included
example_games.pgn(Fischer, Anderssen famous games) - Download from Chess.com (your own games or master games)
- Export from Lichess (games database)
- Use chess game databases (e.g., Kasparov, Fischer, Carlsen games)
- Any standard PGN format file works
Quick Start with Example Games:
# The repository includes example_games.pgn with 3 famous games
# In the app, click "Learn from PGN File" and select example_games.pgn
# This will add ~90 positions to train from- Save Model: Saves the trained neural network to
models/chess_ai.keras - Load Model: Loads a previously saved model
- Models persist between sessions, so your AI's learning is saved
- Auto-save happens after automatic training
Chessai-/
├── main.py # Main application entry point
├── chess_ai.py # AI implementation with neural network
├── chess_board_3d.py # 3D OpenGL chess board widget
├── pretrain_model.py # Script to pre-train the AI model
├── requirements.txt # Python dependencies
├── ChessAI.spec # PyInstaller build configuration
├── test_components.py # Component testing
├── example_games.pgn # Example grandmaster games for training
├── models/
│ └── initial_training_data.pkl # Pre-loaded training data from example games
├── README.md # Complete documentation
├── QUICKSTART.md # Quick start guide
├── BUILD_INSTRUCTIONS.md # Building executables
└── PROJECT_SUMMARY.md # Project overview
If you have TensorFlow installed, you can create a fully pre-trained model:
# Install TensorFlow (optional)
pip install tensorflow
# Run the pre-training script
python pretrain_model.pyThis will:
- Load positions from
example_games.pgn - Train a neural network for 20 epochs
- Save the trained model to
models/chess_ai.keras
Note: The AI works without TensorFlow using classical evaluation. Pre-training is optional but recommended for better performance. ├── ChessAI.spec # PyInstaller build configuration ├── build.py # Build script helper ├── games/ # Saved games in PGN format (created on first save) └── models/ # Saved AI models (created on first save)
## Building for Distribution
### Windows Executable
To create a distributable Windows executable:
```bash
# Install dependencies
pip install -r requirements.txt
# Build with PyInstaller
pyinstaller ChessAI.spec
# The executable and all DLLs will be in dist/ChessAI/
# Distribute the entire dist/ChessAI/ folder
The built executable includes:
- ChessAI.exe (main executable)
- Python runtime
- PyQt5 DLLs
- OpenGL libraries
- TensorFlow libraries
- All other dependencies
Total size: Approximately 400-600 MB due to TensorFlow
To reduce the size of the distribution:
- Use TensorFlow Lite instead of full TensorFlow
- Remove unnecessary TensorFlow backends
- Use
--exclude-modulewith PyInstaller for unused modules
- Uses the
python-chesslibrary for move generation, validation, and game rules - Supports all chess rules including castling, en passant, and promotion
- Search: Minimax algorithm with alpha-beta pruning (depth 2 by default)
- Evaluation: Combines material count and neural network evaluation
- Learning: Neural network learns position evaluations from played games
- Input: 8x8x12 tensor (board position with 12 piece types)
- Layers:
- 2 Conv2D layers (32 and 64 filters)
- Dense layers with dropout
- Output: Single value (position evaluation)
- Training: Mean Squared Error loss, Adam optimizer
The application uses PGN (Portable Game Notation), the standard format for chess games:
- Import games from other chess software (ChessBase, Lichess, Chess.com, etc.)
- Export games for analysis in other tools
- Games include metadata (date, players, etc.)
- Full move history preserved
- Generate Training Data: Run self-play mode to generate diverse positions
- Train Regularly: After significant games, train the AI to improve evaluation
- Save Models: Don't forget to save after training to preserve improvements
- Multiple Training Sessions: The AI improves incrementally with each training session
- Ensure all dependencies are installed:
pip install -r requirements.txt - Check Python version:
python --version(need 3.8+) - Verify OpenGL support: Most systems have this built-in
- Ensure you have OpenGL support (standard on Windows 7+)
- Run from command line to see error messages
- Check Windows Defender/Antivirus isn't blocking it
- Update your graphics drivers
- Ensure OpenGL is supported on your system
- Try running with administrator privileges
- The AI uses depth-2 search by default
- Lower depth for faster moves (edit
chess_ai.py, line ~165) - Neural network predictions are fast; material evaluation is used as fallback
- Reduce epochs (default is 10)
- Ensure you have enough training data from played games
- TensorFlow will use GPU if available for faster training
- Ensure PyInstaller is installed:
pip install pyinstaller - Use the provided
ChessAI.specfile - Check that all imports work:
python main.pyfirst - Some antivirus software may interfere with PyInstaller
- Startup Time: First launch may be slower (TensorFlow initialization)
- Memory Usage: ~200-400 MB (TensorFlow models)
- CPU Usage: Spikes during AI moves and training
- GPU: TensorFlow will use GPU if available (CUDA)
- OS: Windows 7 / Linux / macOS 10.12+
- RAM: 2 GB
- Disk: 1 GB free space
- Graphics: OpenGL 2.0 support
- OS: Windows 10/11 / Modern Linux / macOS 11+
- RAM: 4 GB or more
- Disk: 2 GB free space
- Graphics: OpenGL 3.0+ support
- CPU: Multi-core processor for faster AI
Possible improvements for future versions:
- Adjustable AI difficulty levels
- Opening book integration
- Endgame tablebase support
- Network multiplayer support (without web browser)
- Move animations
- Sound effects
- Analysis mode with move suggestions
- Custom piece designs and themes
- Installer/Setup wizard
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
- python-chess: Excellent chess library by Niklas Fiekas
- PyQt5: Cross-platform GUI framework
- PyOpenGL: Python OpenGL bindings
- TensorFlow: Machine learning framework
- PyInstaller: Creating standalone executables