2f56608c54bb6ae2148f2cb394eaa85d57d6ea96
--num was declared as "Number of games to play" but the play() call used a hardcoded 10. Also give --num an int type so it works when passed on the CLI (range() would otherwise crash on the raw string), and make --verbose a store_true flag. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PQS7ZHqxQfy5DhKcz2gHau
Tic Tac Toe
A Tic Tac Toe implementation where two reinforcement-learning agents (temporal-difference / value-based learning) train against each other, and a human can play against a trained agent from the command line.
How it works
state.py— board representation (3x3 numpy array of'X','O','-') and helpers to detect potential moves and winning lines.a_player.py—APlayerbase class shared by all player types (move, win-check, game lifecycle hooks).machine_player.py—MachinePlayer, a TD-learning agent. It keeps a value table (state string -> expected reward) in a dict, picks the move leading to the best-valued next state, occasionally explores a random move (p_explore), and updates values towards the outcome of each game using a learning ratealpha.human_player.py—HumanPlayer, lets a human enter moves via the console (German prompts).player_provider.py—PlayerProviderpairs two players for a game and randomly decides who starts.helper.py— small numeric helpers (softmax/sampling), not currently used by the main game loop.tic_tac_toe.py— entry point: runs training and/or play loops.values_x.json/values_o.json— persisted value tables for the X and O machine players, loaded/saved across runs so training progress carries over.
Requirements
- Python 3.12+
- numpy (see
.venvfor an existing virtual environment, or install withpip install numpy)
Usage
Run from the project root:
python tic_tac_toe.py [options]
Options:
| Flag | Default | Description |
|---|---|---|
--verbose |
False |
Print detailed per-move debug output |
--train |
False |
Train the machine players before playing |
--num |
5 |
Number of training iterations / batches |
--human |
False |
Let a human play as X against the machine player as O |
--load |
True |
Load existing value tables from values_x.json/values_o.json |
Examples:
# Train the two machine players against each other, saving progress
python tic_tac_toe.py --train --num 50
# Play as a human (X) against the trained machine player (O)
python tic_tac_toe.py --human
# Watch two machine players play a few verbose games
python tic_tac_toe.py --verbose
During training, values_x.json and values_o.json are updated after each
batch of games, so training can be interrupted and resumed.
Languages
Python
100%