A web-based chess application that uses Machine Learning to predict and play chess moves. Built with Streamlit and powered by a trained neural network model.
- Interactive Chess Board: Visual chess board with piece movement
- ML-Powered Opponent: ML opponent that uses a trained neural network to make moves
- Automatic Play: ML automatically responds after each user move
- Multiple Input Methods:
- Click on move buttons to select your move
- Enter moves using UCI notation (e.g., e2e4)
- Game Controls: Undo moves, reset game, view move history
- Real-time Game Status: Shows current turn, check/checkmate status
- Move History: Track all moves made during the game
- Frontend: Streamlit
- Chess Logic:
python-chesslibrary - ML Framework: TensorFlow/Keras
- Board Encoding: One-hot encoding for neural network input
- Visualization: SVG-based chess board rendering
streamlit
chess
numpy
tensorflow
keras
pandas
matplotlibInstall all dependencies using:
pip install -r requirements.txtClone the repository:
git clone https://github.com/kavanatn/Chess-using-ML.git
cd Chess-using-MLEnsure you have the trained model:
- Make sure
chess_model.h5is in the project directory - This file contains the trained neural network weights
- Or train it yourself using the provided Google Colab notebook (see below)
Run the application:
streamlit run app.pyOpen your browser and go to:
http://localhost:8501
Start playing chess using machine learning!
- You play as White (first move)
- Make your move by either:
- Clicking on the move buttons displayed
- Entering UCI notation in the text input (e.g., "e2e4")
- ML responds automatically as Black after your move
- Continue playing until checkmate, stalemate, or draw
The chess engine uses a neural network that:
- Input: One-hot encoded chess board positions (8x8x13 tensor)
- Output: Move evaluation scores
- Training: Trained on chess game data to evaluate board positions
- Move Selection: Chooses moves based on position evaluation
Board Encoding:
- Each square encoded as a 13-dimensional one-hot vector
- Represents 12 piece types (6 white + 6 black) + empty square
- Board converted to 8x8x13 tensor for neural network input
Model training is done using Google Colab. The notebook is available here:
train_and_play.ipynb- Requires data files located in the
data/folder:data/train.csvdata/test.csv
- After training,
chess_model.h5will be generated and used by the Streamlit app
To run the notebook in Google Colab:
chess-using-ml/
├── app.py # Main Streamlit application
├── chess_utils.py # Chess utility functions and ML model interface
├── chess_model.h5 # Trained neural network model
├── requirements.txt # Python dependencies
├── train_and_play.ipynb # Google Colab notebook (training)
├── data/ # Training and testing data
│ ├── train.csv
│ └── test.csv
└── README.md # Project documentation
encode_board(): Converts chess board to neural network input formatone_hot_encode_piece(): Encodes individual pieces as one-hot vectorsplay_nn(): Uses ML model to select best move for given positionmake_ai_move(): Handles automatic ML move execution