A minimal, reproducible reinforcement learning project demonstrating tabular Q-Learning on Tic-Tac-Toe.
Designed for education, experimentation, and interactive play against a trained agent.
- Fully reproducible training using configurable hyperparameters and random seeds
- Modular and clean code structure
- Interactive human vs AI gameplay with multiple difficulty levels
- Save/load Q-tables for reuse or evaluation
- Training progress visualization via win/draw/loss plots
- Live agent evaluation stats during human play
- Lightweight — pure NumPy, no external RL libraries required
This project implements a tabular Q-Learning agent for Tic-Tac-Toe.
The Q-Learning update rule:
Q(s,a) ← Q(s,a) + α [ r + γ max_a' Q(s',a') - Q(s,a) ]
Where:
- s = current state
- a = action taken
- r = reward received
- s' = next state
- α = learning rate
- γ = discount factor
The agent learns optimal moves by playing against a random opponent.
- Clone the repository:
git clone https://github.com/yourusername/tic-tac-toe-qlearning.git
cd tic-tac-toe-qlearning- Install dependencies:
pip install -r requirements.txtDependencies: Python 3.8+, NumPy, Matplotlib
python train.py --episodes 50000 --save q_table.pkl --plot training_plot.png- Trains the agent against a random opponent.
- Saves the learned Q-table to
q_table.pkl. - Generates a plot of win/draw/loss ratios over training episodes.
python train.py --eval --load q_table.pkl- Runs multiple games against a random opponent.
- Prints win/draw/loss statistics.
python play.py --level hard --load q_table.pkl --eval_games 200-
--leveloptions:easy→ agent with high exploration (ε ≈ 0.5)medium→ agent with moderate exploration (ε ≈ 0.1)hard→ fully trained agent (ε ≈ 0.01)
-
CLI will prompt for moves and display the board after each turn.
-
Displays live evaluation stats of the agent’s performance before and after your game.
All hyperparameters are configurable in config.yaml:
seed: 42
episodes: 50000
learning_rate: 0.2
gamma: 0.95
epsilon_start: 1.0
epsilon_min: 0.02
epsilon_decay: 0.99998
eval_interval: 5000- Adjust
episodesfor longer or shorter training. epsilon_*values control exploration/exploitation balance.