This repository contains a comprehensive chess engine implemented in Python. The engine combines traditional board evaluation methods with a neural network for move generation and position evaluation. The engine also features a web interface for interactive gameplay.
- Introduction
- Dependencies
- Usage
- Chess Engine
- Dataset
- Training the Model
- Web Interface
- Acknowledgments
This chess engine provides a robust implementation of a chess-playing AI that utilizes a combination of traditional board evaluation methods and a neural network. The engine supports self-play, allowing it to play against itself, and an interactive mode where users can play against the AI through a web interface.
Make sure you have the following dependencies installed before running the engine:
- Python 3.x
- Flask
- PyTorch
- Numpy
- python-chess
Install the dependencies using:
pip install flask torch numpy python-chess
To see the engine play against itself, run the following command:
python chess_engine.py selfplay
To play against the chess engine, run the following command:
python chess_engine.py
This will start a web server, and you can access the chess board through your web browser.
The chess engine is a combination of traditional board evaluation methods and a neural network for move generation and position evaluation. It includes classes and functions for the minimax algorithm, neural network model, and state representation.
The minimax algorithm with alpha-beta pruning is implemented in the computer_minimax
function in chess_engine.py
. It explores possible moves and evaluates the best move using the provided board valuation function.
The neural network model is defined in the Net
class in train.py
. It uses a convolutional neural network (CNN) architecture to evaluate board positions and is trained on a custom chess dataset.
The State
class in state.py
provides methods for representing the state of the chess board. It includes key generation, serialization of the board state into a binary format, and obtaining legal moves for the current board position.
The dataset used for training the neural network is loaded from a preprocessed file (dataset_25M.npz
). It includes board positions and corresponding target values for training.
If you want to train the neural network model yourself, follow these steps:
- Make sure you have the required dependencies installed.
- Run the training script:
python train.py
- The trained model will be saved in the
nets/value.pth
file.
The engine includes a simple web interface using Flask, allowing you to play against the AI in your browser. Access the interface at your localhost address after starting the engine.
This code is inspired by various chess engines and AI implementations available in the open-source community. Special thanks to the creators and contributors of the python-chess library for providing a powerful and user-friendly interface for chess programming in Python.