Knightmare is a minimal chess engine written by Dennis Vink (drvink.com) combining deep learning and Monte Carlo Tree Search (MCTS) to play full games of chess through a browser interface.
This project features:
- A PyTorch-based neural network that evaluates board positions.
- A Monte Carlo Tree Search (MCTS) planner for strong move selection.
- A Flask web interface to interact with the engine.
- A complete training pipeline to generate and learn from chess data.
- Optional Docker containerization for easy deployment.
- Board Representation: FEN strings are encoded into 17-channel tensors (12 piece types, 4 castling planes, 1 en-passant).
- Neural Network: A deep residual convolutional network estimates:
- Policy: probability distribution over legal moves.
- Value: predicted outcome (win/draw/loss).
- MCTS: The network guides a Monte Carlo Tree Search to simulate positions and return the best move.
- Flask API: Communicates between frontend and engine to serve moves interactively.
git clone https://github.com/dennisvink/knightmare.git
cd knightmarepython -m venv .venv/
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`pip install -r requirements.txtEnsure the following files exist in the project root:
model.pt– trained model weightsmove_to_idx.json– dictionary mapping UCI moves to index positions
python app.pyNavigate to http://localhost:7860 to play!
docker build -t knightmare .docker run -p 7860:7860 knightmareVisit http://localhost:7860 in your browser.
You can train Knightmare from scratch using your own labeled games.
Under the pipeline/ directory:
fens/: Each file contains one game with a list of FENs, one per line. First line must be# Result 1-0,0-1, or1/2-1/2.moves/: Each file has the corresponding best move per position (UCI format), one per line. Usenonefor terminal positions.
Example:
fens/game000000000.txt
moves/game000000000.moves.txt
cd pipeline/
python data_preparation.pyThis creates shards/ with training and validation .pt files.
python train.py- Will resume from
checkpoint_last.ptif available. - Saves best model as
checkpoint_best.pt.
├── app.py
├── model.pt
├── move_to_idx.json
├── requirements.txt
├── Dockerfile
├── templates/
│ └── index.html
├── static/
│ └── favicon.ico
└── pipeline/
├── fens/
├── moves/
├── shards/
├── data_preparation.py
└── train.py
- Python 3.8+
- PyTorch
- Flask
- python-chess
- NumPy
- tqdm
Installable via pip install -r requirements.txt.
Knightmare is an educational chess engine meant to grow. Feel free to fork, modify, and experiment.