Skip to main content

Avatar

Build Status Code Scanning Status Dependencies Status Security Status Published Version

TicTacToe Policy

TicTacToe Policy is a deliberately small PyTorch policy for square Tic-Tac-Toe boards from 3×3 through 8×8. It can generate its own teacher data, train and evaluate a neural policy, persist model metadata, and choose legal moves when embedded in another Python application.

For an N×N board, the default winning condition is N identical marks in a horizontal, vertical, or diagonal line. A shorter win_length can also be configured for experiments.

Installation

pip3 install tictactoe-policy

For development from a checkout:

poetry install

Usage

Load a trained policy and choose a move:

from tictactoe_policy import TicTacToePolicy

policy = TicTacToePolicy.load("tictactoe-3x3.pt")
move = policy.choose_move([
    1, -1, 0,
    0,  1, 0,
   -1,  0, 0,
])

choose_move() validates the board, infers whose turn it is, normalizes the position to the current player's perspective, rejects terminal positions, and masks occupied cells so they can never be selected.

The same API applies to larger boards:

policy = TicTacToePolicy.load("tictactoe-5x5.pt")
move = policy.choose_move([0] * 25)

The policy also exposes move_scores(board), parameter_count, board_size, and win_length.

Board Representation

Boards are flat sequences containing:

Value Meaning
1 X
-1 O
0 Empty cell

Moves are zero-based offsets. A 3×3 board is indexed as follows:

0 1 2
3 4 5
6 7 8

An N×N board contains N * N cells, with moves from 0 through N * N - 1.

Train a Policy

Train an exact 3×3 policy:

poetry run tictactoe-policy train \
  --board-size 3 \
  --hidden-size 16 \
  --seed 42 \
  --output tictactoe-3x3.pt

Train a sampled larger-board policy:

poetry run tictactoe-policy train \
  --board-size 6 \
  --hidden-size 128 \
  --samples 100000 \
  --search-depth 3 \
  --seed 42 \
  --output tictactoe-6x6.pt

Data augmentation applies the eight rotations and reflections of a square and transforms target moves with their boards. Use --no-symmetry to disable it. When multiple moves share the best teacher score, all share the target probability.

Evaluate a Policy

poetry run tictactoe-policy evaluate --model tictactoe-3x3.pt

For a larger model, evaluation parameters are configurable:

poetry run tictactoe-policy evaluate \
  --model tictactoe-6x6.pt \
  --samples 2000 \
  --search-depth 3 \
  --games 20 \
  --seed 42

Configuration

Game dimensions are represented by GameConfig:

from tictactoe_policy import GameConfig

standard_game = GameConfig(board_size=5)
experimental_game = GameConfig(board_size=5, win_length=4)
Property Description Default Valid values
board_size Width and height of the square board 3 3 through 8
win_length Identical marks required in a winning line board_size 1 through board_size

Important training options include:

Option Description Default
--hidden-size Hidden units in the feed-forward network 16
--samples Requested sampled training positions for larger boards 10000
--search-depth Bounded teacher search depth 3
--epochs Supervised training epochs 50
--learning-rate Adam optimizer learning rate 0.01
--seed Python and PyTorch random seed 42

Training Strategies

Exact 3×3 Training

Standard 3×3 Tic-Tac-Toe is solved exactly. Training data contains every reachable non-terminal position, and exact minimax supplies every equally good move as a target.

Evaluation covers every legal non-terminal position, reports exact optimal-move and tactical accuracy, and plays as both X and O against perfect minimax.

Sampled 4×4–8×8 Training

Boards from 4×4 through 8×8 are too large for exhaustive enumeration. Their positions are generated from reproducible sampled games and labelled by a bounded alpha-beta teacher with immediate-win and required-block handling.

Evaluation uses a reproducible sampled set and reports teacher-move agreement, immediate-win accuracy, required-block accuracy, illegal-move rate, and games against random and bounded-search opponents.

These larger games are not claimed to be mathematically solved. Their metrics measure agreement with the configured bounded teacher.

Model Architecture

The policy is a one-hidden-layer feed-forward network:

board_size² inputs → hidden_size → ReLU → board_size² outputs

With C = board_size² cells and H = hidden_size, the model contains C×H + H + H×C + C trainable parameters.

Board Hidden units Parameters
3×3 16 313
5×5 64 3,289
8×8 128 16,576

Board size, hidden size, sample count, search depth, and evaluation settings are configurable so model-size experiments can compare hidden sizes such as 2, 4, 8, 16, 32, 64, 128, and 256.

Model Files

Model files are PyTorch dictionaries rather than pickled policy objects. Each file contains:

{
    "format_version": 1,
    "state_dict": ...,
    "board_size": 3,
    "win_length": 3,
    "hidden_size": 16,
}

This metadata allows TicTacToePolicy.load() to reconstruct the correct network automatically. A model rejects boards whose dimensions do not match the dimensions used during training.

Examples

Runnable examples are available in the examples directory:

bash examples/tictactoe-policy-lib.sh
bash examples/tictactoe-policy-cli.sh

The library example demonstrates training, saving, loading, choosing a move, and evaluation. The CLI example demonstrates the train and evaluate commands with a temporary model file.

Colophon

Developer's Guide

Build reports:

Metadata

Release files for tictactoe-policy 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tictactoe-policy 1.0.0
File Size Uploaded
tictactoe_policy-1.0.0.tar.gz 21.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tictactoe-policy 1.0.0
File Interpreter ABI Platform
tictactoe_policy-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 46.1 kB

Release files / tictactoe_policy-1.0.0.tar.gz

Download URL tictactoe_policy-1.0.0.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f57f5b2bf3fffcd02ce7fdea9e59138bd35f414a8baccb3f8a3c1f4ba8153088
BLAKE2b-256 checksum
How to use checksums
1333bca829f2475bed73c54a8be2cf3d9f57c23a01e8a1346ce6b67e37f92416
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / tictactoe_policy-1.0.0-py3-none-any.whl

Download URL tictactoe_policy-1.0.0-py3-none-any.whl
Size 24.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
499435f10b4ed37a4ae585b942dddce8c8ac75c7475b9aedce35d65f0cba0f96
BLAKE2b-256 checksum
How to use checksums
52dfdf8e05ca9abcbf519e9121a93b0ceb8d0c47aea6de821ecc4b30bc1b4dfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.2.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page