Figure 1.1: Screenshot of index.html
A self-play Connect Four AI written in Python 3.12 with PyTorch. The AI is a small DQN (Deep Q-Network) trained by playing against itself. You can train a new model from scratch or play against a pre-trained one.
View "noCom.py" for the un-commented code.
- Python 3.12+
- PyTorch (CPU or GPU)
Alternatively, you can now open the connect4.html file and play through your browser. Training models still needs to be done through the python interface.
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install torchpython3 main.pyThe terminal will prompt you to:
- Choose an opponent:
PlayerorAI. - If
AI, choosetrain(start from scratch) oruse(loadconnect4_model.pth). - Play by entering a column number 1–7 when prompted.
Training uses self-play: the same network plays both sides, and wins are rewarded back to the network. Progress is printed in batches of 100 games.
To change the number of training games, edit the train_ai(num_games)
call near the bottom of main.py. The model is saved to
connect4_model.pth after training finishes.
Note: the train option overwrites whatever file is in connect4_model.pth.
If you want to keep an existing model, save it elsewhere or change the file name,
before training.
Training on CPU takes roughly 1–2 hours for 100,000 games. GPU (MPS on macOS, CUDA on Linux/Windows) is selected automatically when available.
A pre-trained connect4_model.pth is in the project folder.
You can choose AI → use at the prompt to load it.
If you train your own model, it will overwrite this file unless you change the save path or rename the existing file first.
Search the source for CHANGE ME to find tunable constants:
learning_rate,batch_size— near the top of the fileBATCH_SIZE,GAMMA,EPS_START,EPS_END,EPS_DECAY,TAU,LR— the DQN hyperparametersReplayMemory(100_000)— replay buffer sizetrain_ai(num_games)— number of training games
If training feels too slow, try lowering EPS_DECAY or reducing
train_ai(num_games).
Thank you to the folks who made the PyTorch Tutorial.
This project follows two official PyTorch tutorials:
- Learn the Basics for the general PyTorch workflow: tensors, model definition and optimization.
- Reinforcement Learning (DQN) Tutorial for the DQN architecture: replay memory, target network and the optimization loop (optimize_model).
This project is licensed under the MIT License — see LICENSE for the full text.
Portions of this project are adapted from the official PyTorch tutorials, which are licensed under BSD 3-Clause. Full notice is in the LICENSE file.